Hi!
I have a read guard:
pub struct Ref<'a, T> {
inner: &'a T,
borrow: SharedBorrow<'a>,
}
SharedBorrow
is a reference to the lock and implements Drop
.
I'm trying to change Ref
's definition to:
pub struct Ref<'a, T> {
inner: T,
borrow: SharedBorrow<'a>,
}
This way I could map
it to something like RwLockReadGuard
for example.
The issue is the compiler is too cooperative. I'm able to deref
the guard, keep the reference and drop
the guard.
If I switch back to &T
the borrow checker stop me and tell me I can't move out of a borrowed value.
I'm using a "basic Deref
implementation", I also tried with a method.
impl<T> core::ops::Deref for Ref<'_, T> {
type Target = T;
fn deref(&self) -> &T {
&self.inner
}
}
impl<'a, T> Ref<'a, T> {
pub fn inner(&self) -> &'_ T {
&self.inner
}
}
I'd like to know why this is happening and if there is a way to have the expected behavior. Thanks