I'm trying to design a type that stores a MutexGuard for an Option<Box<T>>
that I can deref to Option<&T>
use std::sync::{Arc, Mutex, LockResult, MutexGuard};
pub struct MutexGuardRef<'a, T> {
mutex_guard: MutexGuard<'a, Option<Box<T>>>
}
impl<'a, T> std::ops::Deref for MutexGuardRef<'a, Option<Box<T>>> {
type Target = Option<&'a mut T>;
fn deref(&self) -> &Self::Target {
&match self.mutex_guard.deref() {
Some(b) => b,
None => None
}
}
}
but I get this error:
Error:
error[E0308]: mismatched types
--> src/lib.rs:13:24
|
13 | Some(b) => b,
| ^ expected enum `Option`, found reference
|
= note: expected enum `Option<&mut T>`
found reference `&Box<Option<Box<T>>>`
Which does not make sense. When I call self.mutex_guard.deref()
, since the internal type of MutexGuardRef
is Option<Box<T>>
, this should deref to &Option<Box<T>>
and thus matching on it should give me either &Box<T>
or None
, but it's giving me Box<Option<Box<T>>>
. What is happening?