From 25574e58b68dae94f7d9931b5e648a327a94ecd1 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 9 Jan 2018 11:39:23 -0800 Subject: [PATCH 1/2] Make core::ops::Place an unsafe trait --- src/liballoc/binary_heap.rs | 2 +- src/liballoc/boxed.rs | 2 +- src/liballoc/linked_list.rs | 4 ++-- src/liballoc/vec.rs | 2 +- src/liballoc/vec_deque.rs | 4 ++-- src/libcore/ops/place.rs | 5 ++++- src/libstd/collections/hash/map.rs | 2 +- 7 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/liballoc/binary_heap.rs b/src/liballoc/binary_heap.rs index 94bbaf92ce9..3041f85cd4c 100644 --- a/src/liballoc/binary_heap.rs +++ b/src/liballoc/binary_heap.rs @@ -1211,7 +1211,7 @@ where T: Clone + Ord { #[unstable(feature = "collection_placement", reason = "placement protocol is subject to change", issue = "30172")] -impl<'a, T> Place for BinaryHeapPlace<'a, T> +unsafe impl<'a, T> Place for BinaryHeapPlace<'a, T> where T: Clone + Ord { fn pointer(&mut self) -> *mut T { self.place.pointer() diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 6f125cdba81..c8ab3f681f8 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -142,7 +142,7 @@ pub struct IntermediateBox { #[unstable(feature = "placement_in", reason = "placement box design is still being worked out.", issue = "27779")] -impl Place for IntermediateBox { +unsafe impl Place for IntermediateBox { fn pointer(&mut self) -> *mut T { self.ptr as *mut T } diff --git a/src/liballoc/linked_list.rs b/src/liballoc/linked_list.rs index 3ac5a85d721..ccb2da46f8d 100644 --- a/src/liballoc/linked_list.rs +++ b/src/liballoc/linked_list.rs @@ -1286,7 +1286,7 @@ impl<'a, T> Placer for FrontPlace<'a, T> { #[unstable(feature = "collection_placement", reason = "placement protocol is subject to change", issue = "30172")] -impl<'a, T> Place for FrontPlace<'a, T> { +unsafe impl<'a, T> Place for FrontPlace<'a, T> { fn pointer(&mut self) -> *mut T { unsafe { &mut (*self.node.pointer()).element } } @@ -1341,7 +1341,7 @@ impl<'a, T> Placer for BackPlace<'a, T> { #[unstable(feature = "collection_placement", reason = "placement protocol is subject to change", issue = "30172")] -impl<'a, T> Place for BackPlace<'a, T> { +unsafe impl<'a, T> Place for BackPlace<'a, T> { fn pointer(&mut self) -> *mut T { unsafe { &mut (*self.node.pointer()).element } } diff --git a/src/liballoc/vec.rs b/src/liballoc/vec.rs index 301e44632b8..4a8982bf85c 100644 --- a/src/liballoc/vec.rs +++ b/src/liballoc/vec.rs @@ -2544,7 +2544,7 @@ impl<'a, T> Placer for PlaceBack<'a, T> { #[unstable(feature = "collection_placement", reason = "placement protocol is subject to change", issue = "30172")] -impl<'a, T> Place for PlaceBack<'a, T> { +unsafe impl<'a, T> Place for PlaceBack<'a, T> { fn pointer(&mut self) -> *mut T { unsafe { self.vec.as_mut_ptr().offset(self.vec.len as isize) } } diff --git a/src/liballoc/vec_deque.rs b/src/liballoc/vec_deque.rs index f56aa23a4eb..df49c1df082 100644 --- a/src/liballoc/vec_deque.rs +++ b/src/liballoc/vec_deque.rs @@ -2564,7 +2564,7 @@ impl<'a, T> Placer for PlaceBack<'a, T> { #[unstable(feature = "collection_placement", reason = "placement protocol is subject to change", issue = "30172")] -impl<'a, T> Place for PlaceBack<'a, T> { +unsafe impl<'a, T> Place for PlaceBack<'a, T> { fn pointer(&mut self) -> *mut T { unsafe { self.vec_deque.ptr().offset(self.vec_deque.head as isize) } } @@ -2610,7 +2610,7 @@ impl<'a, T> Placer for PlaceFront<'a, T> { #[unstable(feature = "collection_placement", reason = "placement protocol is subject to change", issue = "30172")] -impl<'a, T> Place for PlaceFront<'a, T> { +unsafe impl<'a, T> Place for PlaceFront<'a, T> { fn pointer(&mut self) -> *mut T { let tail = self.vec_deque.wrap_sub(self.vec_deque.tail, 1); unsafe { self.vec_deque.ptr().offset(tail as isize) } diff --git a/src/libcore/ops/place.rs b/src/libcore/ops/place.rs index 9fb171e7b92..4c8c6e63fc6 100644 --- a/src/libcore/ops/place.rs +++ b/src/libcore/ops/place.rs @@ -27,10 +27,13 @@ /// implementation of Place to clean up any intermediate state /// (e.g. deallocate box storage, pop a stack, etc). #[unstable(feature = "placement_new_protocol", issue = "27779")] -pub trait Place { +pub unsafe trait Place { /// Returns the address where the input value will be written. /// Note that the data at this address is generally uninitialized, /// and thus one should use `ptr::write` for initializing it. + /// + /// This function must return a valid (non-zero) pointer to + /// a location at which a value of type `Data` can be written. fn pointer(&mut self) -> *mut Data; } diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 7a79a472d58..595b01ff77c 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1932,7 +1932,7 @@ impl<'a, K, V> Placer for Entry<'a, K, V> { #[unstable(feature = "collection_placement", reason = "placement protocol is subject to change", issue = "30172")] -impl<'a, K, V> Place for EntryPlace<'a, K, V> { +unsafe impl<'a, K, V> Place for EntryPlace<'a, K, V> { fn pointer(&mut self) -> *mut V { self.bucket.read_mut().1 } From f25f4687093c1c7e69a06fa7fa6cc3cc6f9aa9d1 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Tue, 16 Jan 2018 08:51:24 -0800 Subject: [PATCH 2/2] Adjust wording of Placer trait safety requirements --- src/libcore/ops/place.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/ops/place.rs b/src/libcore/ops/place.rs index 4c8c6e63fc6..b3dcf4e7ee9 100644 --- a/src/libcore/ops/place.rs +++ b/src/libcore/ops/place.rs @@ -32,8 +32,8 @@ pub unsafe trait Place { /// Note that the data at this address is generally uninitialized, /// and thus one should use `ptr::write` for initializing it. /// - /// This function must return a valid (non-zero) pointer to - /// a location at which a value of type `Data` can be written. + /// This function must return a pointer through which a value + /// of type `Data` can be written. fn pointer(&mut self) -> *mut Data; }