Skip to content

Commit 39f2490

Browse files
committed
Implement Deref for Box
Fixes #18624.
1 parent 95c2ed3 commit 39f2490

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/liballoc/boxed.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use core::option::Option;
2222
use core::raw::TraitObject;
2323
use core::result::Result;
2424
use core::result::Result::{Ok, Err};
25+
use core::ops::{Deref, DerefMut};
2526

2627
/// A value that represents the global exchange heap. This is the default
2728
/// place that the `box` keyword allocates into when no place is supplied.
@@ -147,6 +148,14 @@ impl fmt::Show for Box<Any+'static> {
147148
}
148149
}
149150

151+
impl<Sized? T> Deref<T> for Box<T> {
152+
fn deref(&self) -> &T { &**self }
153+
}
154+
155+
impl<Sized? T> DerefMut<T> for Box<T> {
156+
fn deref_mut(&mut self) -> &mut T { &mut **self }
157+
}
158+
150159
#[cfg(test)]
151160
mod test {
152161
#[test]
@@ -193,4 +202,10 @@ mod test {
193202
let s = format!("{}", b);
194203
assert_eq!(s, "&Any");
195204
}
205+
206+
#[test]
207+
fn deref() {
208+
fn homura<T: Deref<i32>>(_: T) { }
209+
homura(box 765i32);
210+
}
196211
}

0 commit comments

Comments
 (0)