Skip to content

Commit 792dc8d

Browse files
committed
Made the ptr::Unique type accept unsized types, to allow for use cases
like sending a raw pointer slice across thread boundaries.
1 parent de8bc44 commit 792dc8d

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/libcore/ptr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -522,21 +522,21 @@ impl<T> PartialOrd for *mut T {
522522
/// Useful for building abstractions like `Vec<T>` or `Box<T>`, which
523523
/// internally use raw pointers to manage the memory that they own.
524524
#[unstable(feature = "core", reason = "recently added to this module")]
525-
pub struct Unique<T>(pub *mut T);
525+
pub struct Unique<T: ?Sized>(pub *mut T);
526526

527527
/// `Unique` pointers are `Send` if `T` is `Send` because the data they
528528
/// reference is unaliased. Note that this aliasing invariant is
529529
/// unenforced by the type system; the abstraction using the
530530
/// `Unique` must enforce it.
531531
#[unstable(feature = "core", reason = "recently added to this module")]
532-
unsafe impl<T:Send> Send for Unique<T> { }
532+
unsafe impl<T: Send + ?Sized> Send for Unique<T> { }
533533

534534
/// `Unique` pointers are `Sync` if `T` is `Sync` because the data they
535535
/// reference is unaliased. Note that this aliasing invariant is
536536
/// unenforced by the type system; the abstraction using the
537537
/// `Unique` must enforce it.
538538
#[unstable(feature = "core", reason = "recently added to this module")]
539-
unsafe impl<T:Sync> Sync for Unique<T> { }
539+
unsafe impl<T: Sync + ?Sized> Sync for Unique<T> { }
540540

541541
impl<T> Unique<T> {
542542
/// Returns a null Unique.

src/libcoretest/ptr.rs

+9
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,12 @@ fn test_set_memory() {
167167
unsafe { set_memory(ptr, 5u8, xs.len()); }
168168
assert!(xs == [5u8; 20]);
169169
}
170+
171+
#[test]
172+
fn test_unsized_unique() {
173+
let xs: &mut [_] = &mut [1, 2, 3];
174+
let ptr = Unique(xs as *mut [_]);
175+
let ys = unsafe { &mut *ptr.0 };
176+
let zs: &mut [_] = &mut [1, 2, 3];
177+
assert!(ys == zs);
178+
}

0 commit comments

Comments
 (0)