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?
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.