Covariant of smart-pointer-wrapped "self"?

I'd like to implement an instance method (aka. self method), but I'm unable to turn RcCell<S> (where S = a structure) into RcCell<dyn T> (where T = a trait), where RcCell is a wrapper for Rc<RefCell>.

I'm implementing trait T with a method like this:

// self: &RcCell<S>
fn f(&self) {
    let s: RcCell<T> = self.clone() as RcCell<T>;
    // do something with s
}

To be more specific, I want to return an object containing self from method f. I know I can use a non-instance method (non-self method) for such purpose, as I even made a module for static methods for manipulating RcCell<T>, but I'd like to know if I can just keep using self, any idea?

Here's another example of what I want to achieve:

impl RcCell<S> {
    fn f(&self) -> RcCell<dyn T> {
        self.clone()
    }
}

:man_shrugging:

type RcCell<T> = std::rc::Rc<std::cell::RefCell<T>>;

trait T {
    fn f(&self) -> RcCell<dyn T>;
}

struct S {}

impl T for S {
    fn f(&self) -> RcCell<dyn T> {
        todo!(); /* what? */
    }
}

impl T for RcCell<S> {
    fn f(&self) -> RcCell<dyn T> {
        self.clone()
    }
}

S, T, f etc. are not very helpful names: they only serve to obscure what it is you're trying to do, and thus make it harder to understand the problem. If you must abbreviate, please at least provide a code snippet that demonstrates the error you encountered.

This is my actual code, where SymbolRef is RcCell<dyn Symbol>:

pub(crate) struct Value {
    ...
}

impl Symbol for RcCell<Value> {
    ...
    fn resolve_name(&self, name: String, type_or_module: bool) -> Option<SymbolRef> {
        let t = self.value_type();
        if t.is_some() && t.unwrap().borrow().is_any_type() {
            return self.host().borrow().factory().create_dynamic_reference_value(self);
        }
        ...
    }
}

Look at create_dynamic_reference_value(self), where self is used. The create_dynamic_reference_value will take a SymbolRef.

Also, RcCell isn't an alias, it's a wrapper:

pub struct RcCell<T: ?Sized>(Rc<RefCell<T>>);

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.