Skip to content

Commit 9758ff9

Browse files
committed
Auto merge of #47299 - cramertj:unsafe-placer, r=alexcrichton
Make core::ops::Place an unsafe trait Consumers of `Place` would reasonably expect that the `pointer` function returns a valid pointer to memory that can actually be written to.
2 parents a538fe7 + f25f468 commit 9758ff9

File tree

7 files changed

+12
-9
lines changed

7 files changed

+12
-9
lines changed

src/liballoc/binary_heap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ where T: Clone + Ord {
12111211
#[unstable(feature = "collection_placement",
12121212
reason = "placement protocol is subject to change",
12131213
issue = "30172")]
1214-
impl<'a, T> Place<T> for BinaryHeapPlace<'a, T>
1214+
unsafe impl<'a, T> Place<T> for BinaryHeapPlace<'a, T>
12151215
where T: Clone + Ord {
12161216
fn pointer(&mut self) -> *mut T {
12171217
self.place.pointer()

src/liballoc/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub struct IntermediateBox<T: ?Sized> {
142142
#[unstable(feature = "placement_in",
143143
reason = "placement box design is still being worked out.",
144144
issue = "27779")]
145-
impl<T> Place<T> for IntermediateBox<T> {
145+
unsafe impl<T> Place<T> for IntermediateBox<T> {
146146
fn pointer(&mut self) -> *mut T {
147147
self.ptr as *mut T
148148
}

src/liballoc/linked_list.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ impl<'a, T> Placer<T> for FrontPlace<'a, T> {
12861286
#[unstable(feature = "collection_placement",
12871287
reason = "placement protocol is subject to change",
12881288
issue = "30172")]
1289-
impl<'a, T> Place<T> for FrontPlace<'a, T> {
1289+
unsafe impl<'a, T> Place<T> for FrontPlace<'a, T> {
12901290
fn pointer(&mut self) -> *mut T {
12911291
unsafe { &mut (*self.node.pointer()).element }
12921292
}
@@ -1341,7 +1341,7 @@ impl<'a, T> Placer<T> for BackPlace<'a, T> {
13411341
#[unstable(feature = "collection_placement",
13421342
reason = "placement protocol is subject to change",
13431343
issue = "30172")]
1344-
impl<'a, T> Place<T> for BackPlace<'a, T> {
1344+
unsafe impl<'a, T> Place<T> for BackPlace<'a, T> {
13451345
fn pointer(&mut self) -> *mut T {
13461346
unsafe { &mut (*self.node.pointer()).element }
13471347
}

src/liballoc/vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,7 @@ impl<'a, T> Placer<T> for PlaceBack<'a, T> {
25442544
#[unstable(feature = "collection_placement",
25452545
reason = "placement protocol is subject to change",
25462546
issue = "30172")]
2547-
impl<'a, T> Place<T> for PlaceBack<'a, T> {
2547+
unsafe impl<'a, T> Place<T> for PlaceBack<'a, T> {
25482548
fn pointer(&mut self) -> *mut T {
25492549
unsafe { self.vec.as_mut_ptr().offset(self.vec.len as isize) }
25502550
}

src/liballoc/vec_deque.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2565,7 +2565,7 @@ impl<'a, T> Placer<T> for PlaceBack<'a, T> {
25652565
#[unstable(feature = "collection_placement",
25662566
reason = "placement protocol is subject to change",
25672567
issue = "30172")]
2568-
impl<'a, T> Place<T> for PlaceBack<'a, T> {
2568+
unsafe impl<'a, T> Place<T> for PlaceBack<'a, T> {
25692569
fn pointer(&mut self) -> *mut T {
25702570
unsafe { self.vec_deque.ptr().offset(self.vec_deque.head as isize) }
25712571
}
@@ -2611,7 +2611,7 @@ impl<'a, T> Placer<T> for PlaceFront<'a, T> {
26112611
#[unstable(feature = "collection_placement",
26122612
reason = "placement protocol is subject to change",
26132613
issue = "30172")]
2614-
impl<'a, T> Place<T> for PlaceFront<'a, T> {
2614+
unsafe impl<'a, T> Place<T> for PlaceFront<'a, T> {
26152615
fn pointer(&mut self) -> *mut T {
26162616
let tail = self.vec_deque.wrap_sub(self.vec_deque.tail, 1);
26172617
unsafe { self.vec_deque.ptr().offset(tail as isize) }

src/libcore/ops/place.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
/// implementation of Place to clean up any intermediate state
2828
/// (e.g. deallocate box storage, pop a stack, etc).
2929
#[unstable(feature = "placement_new_protocol", issue = "27779")]
30-
pub trait Place<Data: ?Sized> {
30+
pub unsafe trait Place<Data: ?Sized> {
3131
/// Returns the address where the input value will be written.
3232
/// Note that the data at this address is generally uninitialized,
3333
/// and thus one should use `ptr::write` for initializing it.
34+
///
35+
/// This function must return a pointer through which a value
36+
/// of type `Data` can be written.
3437
fn pointer(&mut self) -> *mut Data;
3538
}
3639

src/libstd/collections/hash/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ impl<'a, K, V> Placer<V> for Entry<'a, K, V> {
19721972
#[unstable(feature = "collection_placement",
19731973
reason = "placement protocol is subject to change",
19741974
issue = "30172")]
1975-
impl<'a, K, V> Place<V> for EntryPlace<'a, K, V> {
1975+
unsafe impl<'a, K, V> Place<V> for EntryPlace<'a, K, V> {
19761976
fn pointer(&mut self) -> *mut V {
19771977
self.bucket.read_mut().1
19781978
}

0 commit comments

Comments
 (0)