From 39f249067a2b0773d887bb311573dc258118d34d Mon Sep 17 00:00:00 2001 From: Barosl Lee Date: Sat, 20 Dec 2014 07:44:21 +0900 Subject: [PATCH] Implement Deref for Box Fixes #18624. --- src/liballoc/boxed.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 879a8cc6951..ea7b32ace49 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -22,6 +22,7 @@ use core::option::Option; 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 @@ impl fmt::Show for Box { } } +impl Deref for Box { + fn deref(&self) -> &T { &**self } +} + +impl DerefMut for Box { + fn deref_mut(&mut self) -> &mut T { &mut **self } +} + #[cfg(test)] mod test { #[test] @@ -193,4 +202,10 @@ mod test { let s = format!("{}", b); assert_eq!(s, "&Any"); } + + #[test] + fn deref() { + fn homura>(_: T) { } + homura(box 765i32); + } }