From ec870b7986e8294b700a0d576e0179babb512f7c Mon Sep 17 00:00:00 2001 From: Sampson Wong Date: Mon, 17 Nov 2014 16:44:01 +1100 Subject: [PATCH] implement Deref for Box. fixes #18624 --- src/liballoc/boxed.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 26f8522e1c18a..dfb8683a4368d 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -18,6 +18,7 @@ use core::fmt; use core::intrinsics; use core::kinds::Sized; use core::mem; +use core::ops::{Deref, DerefMut}; use core::option::Option; use core::raw::TraitObject; use core::result::{Ok, Err, Result}; @@ -52,6 +53,14 @@ impl Default for Box<[T]> { fn default() -> Box<[T]> { box [] } } +impl Deref for Box { + fn deref(&self) -> &T { &**self } +} + +impl DerefMut for Box { + fn deref_mut(&mut self) -> &mut T { &mut **self } +} + #[unstable] impl Clone for Box { /// Returns a copy of the owned box. @@ -183,4 +192,11 @@ mod test { let s = format!("{}", b); assert_eq!(s.as_slice(), "&Any"); } + + #[test] + fn auto_deref() { + fn test >(_: U) {} + let foo = box 1; + test(foo); + } }