]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #26122 - bluss:borrow-box, r=alexcrichton
authorbors <bors@rust-lang.org>
Thu, 11 Jun 2015 03:25:45 +0000 (03:25 +0000)
committerbors <bors@rust-lang.org>
Thu, 11 Jun 2015 03:25:45 +0000 (03:25 +0000)
Implement Borrow<T> and BorrowMut<T> for Box<T: ?Sized>

src/libcollections/borrow.rs
src/libcollectionstest/btree/map.rs

index 8e8fc0bedec6ac5080aaf82df975424268a73da8..d7242b9077556da112e5bac4bbd9eb40c01485d6 100644 (file)
@@ -21,7 +21,7 @@
 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 }
 }
index e617e194d300a6d348581e86cbf78d89f7c5d9be..62b46433da95748ae6ba7d5c2ea37a5046968f03 100644 (file)
@@ -12,6 +12,7 @@
 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() {
@@ -198,6 +199,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)];