My goal is to delegate method calls against my struct to a Trait's methods, where the Trait object is inside an Rc
of RefCell
.
I tried to follow the advice from this question: How can I obtain an &A reference from a Rc<RefCell<A>>?
I get a compile error.
use std::rc::Rc;
use std::cell::RefCell;
use std::fmt::*;
use std::ops::Deref;
pub struct ShyObject {
pub association: Rc<RefCell<dyn Display>>
}
impl Deref for ShyObject {
type Target = dyn Display;
fn deref<'a>(&'a self) -> &(dyn Display + 'static) {
&*self.association.borrow()
}
}
fn main() {}
Here is the error:
error[E0515]: cannot return value referencing temporary value
--> src/main.rs:13:9
|
13 | &*self.association.borrow()
| ^^-------------------------
| | |
| | temporary value created here
| returns a value referencing data owned by the current function
My example uses Display
as the trait; in reality I have a Trait with a dozen methods. I am trying to avoid the boilerplate of having to implement all those methods and just burrow down to the Trait object in each call.
Deref
, not by simply implementingDeref
for the main type (ShyObject
in your case). – Illogical