]> git.lizzy.rs Git - rust.git/commitdiff
Made the `ptr::Unique` type accept unsized types, to allow for use cases
authorMarvin Löbel <loebel.marvin@gmail.com>
Tue, 10 Feb 2015 13:37:44 +0000 (14:37 +0100)
committerMarvin Löbel <loebel.marvin@gmail.com>
Tue, 10 Feb 2015 13:37:44 +0000 (14:37 +0100)
like sending a raw pointer slice across thread boundaries.

src/libcore/ptr.rs
src/libcoretest/ptr.rs

index ba1eae551ff2a15f9dba0fa52253c0b10b346dbd..bf801a88ca5b3e3c92961ae23a8e72f928fa962f 100644 (file)
@@ -522,21 +522,21 @@ fn ge(&self, other: &*mut T) -> bool { *self >= *other }
 /// 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.
index 7f0b97c53d465b371f70331f9bee38d5cbbb9f15..2365b907b3ff5ba4468f951eb4ca7b661b9f8535 100644 (file)
@@ -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);
+}