about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-02-10 08:43:16 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-02-10 08:43:16 -0800
commitba7b79008b5166b2b1cf043b4533917103d3e06e (patch)
treed0ea738e91eea29e437593b00ee7aaf8c8f2fdf6
parent8b44cf2a40b93c5407b16de4cb988b01d265fa51 (diff)
parent792dc8d067d1f11e08e859ccdd45d59436773fc9 (diff)
downloadrust-ba7b79008b5166b2b1cf043b4533917103d3e06e.tar.gz
rust-ba7b79008b5166b2b1cf043b4533917103d3e06e.zip
rollup merge of #22142: Kimundi/unsized_unique
This is to allow for use cases like sending a raw pointer slice across thread boundaries.
-rw-r--r--src/libcore/ptr.rs6
-rw-r--r--src/libcoretest/ptr.rs9
2 files changed, 12 insertions, 3 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index ba1eae551ff..bf801a88ca5 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -522,21 +522,21 @@ impl<T> PartialOrd for *mut T {
 /// Useful for building abstractions like `Vec<T>` or `Box<T>`, which
 /// internally use raw pointers to manage the memory that they own.
 #[unstable(feature = "core", reason = "recently added to this module")]
-pub struct Unique<T>(pub *mut T);
+pub struct Unique<T: ?Sized>(pub *mut T);
 
 /// `Unique` pointers are `Send` if `T` is `Send` because the data they
 /// reference is unaliased. Note that this aliasing invariant is
 /// unenforced by the type system; the abstraction using the
 /// `Unique` must enforce it.
 #[unstable(feature = "core", reason = "recently added to this module")]
-unsafe impl<T:Send> Send for Unique<T> { }
+unsafe impl<T: Send + ?Sized> Send for Unique<T> { }
 
 /// `Unique` pointers are `Sync` if `T` is `Sync` because the data they
 /// reference is unaliased. Note that this aliasing invariant is
 /// unenforced by the type system; the abstraction using the
 /// `Unique` must enforce it.
 #[unstable(feature = "core", reason = "recently added to this module")]
-unsafe impl<T:Sync> Sync for Unique<T> { }
+unsafe impl<T: Sync + ?Sized> Sync for Unique<T> { }
 
 impl<T> Unique<T> {
     /// Returns a null Unique.
diff --git a/src/libcoretest/ptr.rs b/src/libcoretest/ptr.rs
index 7f0b97c53d4..2365b907b3f 100644
--- a/src/libcoretest/ptr.rs
+++ b/src/libcoretest/ptr.rs
@@ -167,3 +167,12 @@ fn test_set_memory() {
     unsafe { set_memory(ptr, 5u8, xs.len()); }
     assert!(xs == [5u8; 20]);
 }
+
+#[test]
+fn test_unsized_unique() {
+    let xs: &mut [_] = &mut [1, 2, 3];
+    let ptr = Unique(xs as *mut [_]);
+    let ys = unsafe { &mut *ptr.0 };
+    let zs: &mut [_] = &mut [1, 2, 3];
+    assert!(ys == zs);
+}