Deref and DerefMut for UniquePtr
diff --git a/src/unique_ptr.rs b/src/unique_ptr.rs
index c8c5a67..ab8b9ef 100644
--- a/src/unique_ptr.rs
+++ b/src/unique_ptr.rs
@@ -3,6 +3,7 @@
use std::fmt::{self, Debug, Display};
use std::marker::PhantomData;
use std::mem;
+use std::ops::{Deref, DerefMut};
use std::ptr;
/// Binding to C++ `std::unique_ptr<T, std::default_delete<T>>`.
@@ -94,6 +95,32 @@
}
}
+impl<T> Deref for UniquePtr<T>
+where
+ T: UniquePtrTarget,
+{
+ type Target = T;
+
+ fn deref(&self) -> &Self::Target {
+ match self.as_ref() {
+ Some(target) => target,
+ None => panic!("called deref on a null UniquePtr"),
+ }
+ }
+}
+
+impl<T> DerefMut for UniquePtr<T>
+where
+ T: UniquePtrTarget,
+{
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ match self.as_mut() {
+ Some(target) => target,
+ None => panic!("called deref_mut on a null UniquePtr"),
+ }
+ }
+}
+
impl<T> Debug for UniquePtr<T>
where
T: Debug + UniquePtrTarget,