about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUlrik Sverdrup <root@localhost>2015-06-09 16:14:48 +0200
committerUlrik Sverdrup <root@localhost>2015-06-09 16:15:38 +0200
commit4fdb4cfa896232a2a2beb9d1eace922402b98082 (patch)
treefd2087df17f6d8043a5cb7ad09f0aae2561d0368
parent6daf1dcb80e324df94ad4a1d263c832ab589b33a (diff)
downloadrust-4fdb4cfa896232a2a2beb9d1eace922402b98082.tar.gz
rust-4fdb4cfa896232a2a2beb9d1eace922402b98082.zip
Implement Borrow<T> and BorrowMut<T> for Box<T: ?Sized>
-rw-r--r--src/libcollections/borrow.rs10
-rw-r--r--src/libcollectionstest/btree/map.rs29
2 files changed, 38 insertions, 1 deletions
diff --git a/src/libcollections/borrow.rs b/src/libcollections/borrow.rs
index 8e8fc0bedec..d7242b90775 100644
--- a/src/libcollections/borrow.rs
+++ b/src/libcollections/borrow.rs
@@ -21,7 +21,7 @@ use core::ops::Deref;
 use core::option::Option;
 
 use fmt;
-use alloc::{rc, arc};
+use alloc::{boxed, rc, arc};
 
 use self::Cow::*;
 
@@ -116,6 +116,14 @@ impl<'a, T: ?Sized> BorrowMut<T> for &'a mut T {
     fn borrow_mut(&mut self) -> &mut T { &mut **self }
 }
 
+impl<T: ?Sized> Borrow<T> for boxed::Box<T> {
+    fn borrow(&self) -> &T { &**self }
+}
+
+impl<T: ?Sized> BorrowMut<T> for boxed::Box<T> {
+    fn borrow_mut(&mut self) -> &mut T { &mut **self }
+}
+
 impl<T: ?Sized> Borrow<T> for rc::Rc<T> {
     fn borrow(&self) -> &T { &**self }
 }
diff --git a/src/libcollectionstest/btree/map.rs b/src/libcollectionstest/btree/map.rs
index e617e194d30..62b46433da9 100644
--- a/src/libcollectionstest/btree/map.rs
+++ b/src/libcollectionstest/btree/map.rs
@@ -12,6 +12,7 @@ use std::collections::BTreeMap;
 use std::collections::Bound::{Excluded, Included, Unbounded, self};
 use std::collections::btree_map::Entry::{Occupied, Vacant};
 use std::iter::range_inclusive;
+use std::rc::Rc;
 
 #[test]
 fn test_basic_large() {
@@ -199,6 +200,34 @@ fn test_range() {
 }
 
 #[test]
+fn test_borrow() {
+    // make sure these compile -- using the Borrow trait
+    {
+        let mut map = BTreeMap::new();
+        map.insert("0".to_string(), 1);
+        assert_eq!(map["0"], 1);
+    }
+
+    {
+        let mut map = BTreeMap::new();
+        map.insert(Box::new(0), 1);
+        assert_eq!(map[&0], 1);
+    }
+
+    {
+        let mut map = BTreeMap::new();
+        map.insert(Box::new([0, 1]) as Box<[i32]>, 1);
+        assert_eq!(map[&[0, 1][..]], 1);
+    }
+
+    {
+        let mut map = BTreeMap::new();
+        map.insert(Rc::new(0), 1);
+        assert_eq!(map[&0], 1);
+    }
+}
+
+#[test]
 fn test_entry(){
     let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];