I have a struct that I implement Deref
on:
pub struct Foo {
val: u8,
}
impl Deref for Foo {
type Target = u8;
fn deref(&self) -> &u8 {
&self.val
}
}
I want to change the struct internally so that the value is held in a Cell
:
pub struct Foo {
val: Cell<u8>,
}
I've naively implemented Deref
as follows:
impl Deref for Foo {
type Target = u8;
fn deref(&self) -> &u8 {
&self.val.get()
}
}
The compiler complains saying &self.val.get() does not live long enough
, which mostly makes sense to me (Cell
not being a Copy
type?), but I have no idea how to get it to compile. I've tried annotating with lifetimes, but what I'm doing doesn't feel intuitively correct and I'm just blindly changing stuff at this point.