Skip to content

Commit ec870b7

Browse files
committed
implement Deref for Box.
fixes #18624
1 parent 0b7b4f0 commit ec870b7

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/liballoc/boxed.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use core::fmt;
1818
use core::intrinsics;
1919
use core::kinds::Sized;
2020
use core::mem;
21+
use core::ops::{Deref, DerefMut};
2122
use core::option::Option;
2223
use core::raw::TraitObject;
2324
use core::result::{Ok, Err, Result};
@@ -52,6 +53,14 @@ impl<T> Default for Box<[T]> {
5253
fn default() -> Box<[T]> { box [] }
5354
}
5455

56+
impl<Sized? T> Deref<T> for Box<T> {
57+
fn deref(&self) -> &T { &**self }
58+
}
59+
60+
impl<Sized? T> DerefMut<T> for Box<T> {
61+
fn deref_mut(&mut self) -> &mut T { &mut **self }
62+
}
63+
5564
#[unstable]
5665
impl<T: Clone> Clone for Box<T> {
5766
/// Returns a copy of the owned box.
@@ -183,4 +192,11 @@ mod test {
183192
let s = format!("{}", b);
184193
assert_eq!(s.as_slice(), "&Any");
185194
}
195+
196+
#[test]
197+
fn auto_deref() {
198+
fn test<U: Deref<int> >(_: U) {}
199+
let foo = box 1;
200+
test(foo);
201+
}
186202
}

0 commit comments

Comments
 (0)