rollup merge of #20052: barosl/deref-for-box

As the previous pull request (#19023) was closed due to inactivity, I steal the chance and open this pull request. 😊

Fixes #18624.
This commit is contained in:
Alex Crichton 2014-12-21 00:04:14 -08:00
commit 40d59e9467

View file

@ -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<Any+'static> {
}
}
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 @@ mod test {
let s = format!("{}", b);
assert_eq!(s, "&Any");
}
#[test]
fn deref() {
fn homura<T: Deref<i32>>(_: T) { }
homura(box 765i32);
}
}