]> git.lizzy.rs Git - rust.git/commitdiff
Implement Deref for Box
authorBarosl Lee <vcs@barosl.com>
Fri, 19 Dec 2014 22:44:21 +0000 (07:44 +0900)
committerBarosl Lee <vcs@barosl.com>
Fri, 19 Dec 2014 23:58:33 +0000 (08:58 +0900)
Fixes #18624.

src/liballoc/boxed.rs

index 879a8cc6951a133063fcb91afe0ae22526144fc7..ea7b32ace49de0f01c2bf467bd250a0f440887b8 100644 (file)
@@ -22,6 +22,7 @@
 use core::raw::TraitObject;
 use core::result::Result;
 use core::result::Result::{Ok, Err};
+use core::ops::{Deref, DerefMut};
 
 /// A value that represents the global exchange heap. This is the default
 /// place that the `box` keyword allocates into when no place is supplied.
@@ -147,6 +148,14 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
+impl<Sized? T> Deref<T> for Box<T> {
+    fn deref(&self) -> &T { &**self }
+}
+
+impl<Sized? T> DerefMut<T> for Box<T> {
+    fn deref_mut(&mut self) -> &mut T { &mut **self }
+}
+
 #[cfg(test)]
 mod test {
     #[test]
@@ -193,4 +202,10 @@ fn test_show() {
         let s = format!("{}", b);
         assert_eq!(s, "&Any");
     }
+
+    #[test]
+    fn deref() {
+        fn homura<T: Deref<i32>>(_: T) { }
+        homura(box 765i32);
+    }
 }