Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the ptr::Unique type accept unsized types #22142

Merged
merged 1 commit into from
Feb 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/libcoretest/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}