Skip to content

Commit 599be0d

Browse files
committed
Auto merge of #43487 - Mark-Simulacrum:rollup, r=Mark-Simulacrum
Rollup of 10 pull requests - Successful merges: #42959, #43447, #43455, #43456, #43458, #43462, #43463, #43465, #43471, #43480 - Failed merges:
2 parents d02fb3b + 959ebd6 commit 599be0d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+631
-236
lines changed

src/doc/nomicon

src/liballoc/allocator.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ pub unsafe trait Alloc {
892892
{
893893
let k = Layout::new::<T>();
894894
if k.size() > 0 {
895-
unsafe { self.alloc(k).map(|p| Unique::new(p as *mut T)) }
895+
unsafe { self.alloc(k).map(|p| Unique::new_unchecked(p as *mut T)) }
896896
} else {
897897
Err(AllocErr::invalid_input("zero-sized type invalid for alloc_one"))
898898
}
@@ -963,7 +963,7 @@ pub unsafe trait Alloc {
963963
unsafe {
964964
self.alloc(layout.clone())
965965
.map(|p| {
966-
Unique::new(p as *mut T)
966+
Unique::new_unchecked(p as *mut T)
967967
})
968968
}
969969
}
@@ -1012,7 +1012,7 @@ pub unsafe trait Alloc {
10121012
match (Layout::array::<T>(n_old), Layout::array::<T>(n_new), ptr.as_ptr()) {
10131013
(Some(ref k_old), Some(ref k_new), ptr) if k_old.size() > 0 && k_new.size() > 0 => {
10141014
self.realloc(ptr as *mut u8, k_old.clone(), k_new.clone())
1015-
.map(|p|Unique::new(p as *mut T))
1015+
.map(|p|Unique::new_unchecked(p as *mut T))
10161016
}
10171017
_ => {
10181018
Err(AllocErr::invalid_input("invalid layout for realloc_array"))

src/liballoc/arc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl<T> Arc<T> {
280280
weak: atomic::AtomicUsize::new(1),
281281
data: data,
282282
};
283-
Arc { ptr: unsafe { Shared::new(Box::into_raw(x)) } }
283+
Arc { ptr: Shared::from(Box::into_unique(x)) }
284284
}
285285

286286
/// Returns the contained value, if the `Arc` has exactly one strong reference.
@@ -382,7 +382,7 @@ impl<T> Arc<T> {
382382
// `data` field from the pointer.
383383
let ptr = (ptr as *const u8).offset(-offset_of!(ArcInner<T>, data));
384384
Arc {
385-
ptr: Shared::new(ptr as *mut u8 as *mut _),
385+
ptr: Shared::new_unchecked(ptr as *mut u8 as *mut _),
386386
}
387387
}
388388
}
@@ -842,7 +842,7 @@ impl<T> Weak<T> {
842842
pub fn new() -> Weak<T> {
843843
unsafe {
844844
Weak {
845-
ptr: Shared::new(Box::into_raw(box ArcInner {
845+
ptr: Shared::from(Box::into_unique(box ArcInner {
846846
strong: atomic::AtomicUsize::new(0),
847847
weak: atomic::AtomicUsize::new(1),
848848
data: uninitialized(),

src/liballoc/boxed.rs

+31
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,37 @@ impl<T: ?Sized> Box<T> {
297297
pub fn into_raw(b: Box<T>) -> *mut T {
298298
unsafe { mem::transmute(b) }
299299
}
300+
301+
/// Consumes the `Box`, returning the wrapped pointer as `Unique<T>`.
302+
///
303+
/// After calling this function, the caller is responsible for the
304+
/// memory previously managed by the `Box`. In particular, the
305+
/// caller should properly destroy `T` and release the memory. The
306+
/// proper way to do so is to convert the raw pointer back into a
307+
/// `Box` with the [`Box::from_raw`] function.
308+
///
309+
/// Note: this is an associated function, which means that you have
310+
/// to call it as `Box::into_unique(b)` instead of `b.into_unique()`. This
311+
/// is so that there is no conflict with a method on the inner type.
312+
///
313+
/// [`Box::from_raw`]: struct.Box.html#method.from_raw
314+
///
315+
/// # Examples
316+
///
317+
/// ```
318+
/// #![feature(unique)]
319+
///
320+
/// fn main() {
321+
/// let x = Box::new(5);
322+
/// let ptr = Box::into_unique(x);
323+
/// }
324+
/// ```
325+
#[unstable(feature = "unique", reason = "needs an RFC to flesh out design",
326+
issue = "27730")]
327+
#[inline]
328+
pub fn into_unique(b: Box<T>) -> Unique<T> {
329+
unsafe { mem::transmute(b) }
330+
}
300331
}
301332

302333
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/btree/node.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -140,24 +140,22 @@ struct BoxedNode<K, V> {
140140

141141
impl<K, V> BoxedNode<K, V> {
142142
fn from_leaf(node: Box<LeafNode<K, V>>) -> Self {
143-
unsafe {
144-
BoxedNode { ptr: Unique::new(Box::into_raw(node)) }
145-
}
143+
BoxedNode { ptr: Box::into_unique(node) }
146144
}
147145

148146
fn from_internal(node: Box<InternalNode<K, V>>) -> Self {
149147
unsafe {
150-
BoxedNode { ptr: Unique::new(Box::into_raw(node) as *mut LeafNode<K, V>) }
148+
BoxedNode { ptr: Unique::new_unchecked(Box::into_raw(node) as *mut LeafNode<K, V>) }
151149
}
152150
}
153151

154152
unsafe fn from_ptr(ptr: NonZero<*const LeafNode<K, V>>) -> Self {
155-
BoxedNode { ptr: Unique::new(ptr.get() as *mut LeafNode<K, V>) }
153+
BoxedNode { ptr: Unique::new_unchecked(ptr.get() as *mut LeafNode<K, V>) }
156154
}
157155

158156
fn as_ptr(&self) -> NonZero<*const LeafNode<K, V>> {
159157
unsafe {
160-
NonZero::new(self.ptr.as_ptr())
158+
NonZero::from(self.ptr.as_ref())
161159
}
162160
}
163161
}
@@ -384,21 +382,19 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
384382
>,
385383
Self
386384
> {
387-
if self.as_leaf().parent.is_null() {
388-
Err(self)
389-
} else {
385+
if let Some(non_zero) = NonZero::new(self.as_leaf().parent as *const LeafNode<K, V>) {
390386
Ok(Handle {
391387
node: NodeRef {
392388
height: self.height + 1,
393-
node: unsafe {
394-
NonZero::new(self.as_leaf().parent as *mut LeafNode<K, V>)
395-
},
389+
node: non_zero,
396390
root: self.root,
397391
_marker: PhantomData
398392
},
399393
idx: self.as_leaf().parent_idx as usize,
400394
_marker: PhantomData
401395
})
396+
} else {
397+
Err(self)
402398
}
403399
}
404400

src/liballoc/linked_list.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<T> LinkedList<T> {
157157
unsafe {
158158
node.next = self.head;
159159
node.prev = None;
160-
let node = Some(Shared::new(Box::into_raw(node)));
160+
let node = Some(Shared::from(Box::into_unique(node)));
161161

162162
match self.head {
163163
None => self.tail = node,
@@ -192,7 +192,7 @@ impl<T> LinkedList<T> {
192192
unsafe {
193193
node.next = None;
194194
node.prev = self.tail;
195-
let node = Some(Shared::new(Box::into_raw(node)));
195+
let node = Some(Shared::from(Box::into_unique(node)));
196196

197197
match self.tail {
198198
None => self.head = node,
@@ -921,7 +921,7 @@ impl<'a, T> IterMut<'a, T> {
921921
Some(prev) => prev,
922922
};
923923

924-
let node = Some(Shared::new(Box::into_raw(box Node {
924+
let node = Some(Shared::from(Box::into_unique(box Node {
925925
next: Some(head),
926926
prev: Some(prev),
927927
element: element,

src/liballoc/raw_vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<T, A: Alloc> RawVec<T, A> {
104104
};
105105

106106
RawVec {
107-
ptr: Unique::new(ptr as *mut _),
107+
ptr: Unique::new_unchecked(ptr as *mut _),
108108
cap: cap,
109109
a: a,
110110
}
@@ -159,7 +159,7 @@ impl<T, A: Alloc> RawVec<T, A> {
159159
/// If the ptr and capacity come from a RawVec created via `a`, then this is guaranteed.
160160
pub unsafe fn from_raw_parts_in(ptr: *mut T, cap: usize, a: A) -> Self {
161161
RawVec {
162-
ptr: Unique::new(ptr),
162+
ptr: Unique::new_unchecked(ptr),
163163
cap: cap,
164164
a: a,
165165
}
@@ -176,7 +176,7 @@ impl<T> RawVec<T, Heap> {
176176
/// If the ptr and capacity come from a RawVec, then this is guaranteed.
177177
pub unsafe fn from_raw_parts(ptr: *mut T, cap: usize) -> Self {
178178
RawVec {
179-
ptr: Unique::new(ptr),
179+
ptr: Unique::new_unchecked(ptr),
180180
cap: cap,
181181
a: Heap,
182182
}

src/liballoc/rc.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,16 @@ impl<T> Rc<T> {
303303
/// ```
304304
#[stable(feature = "rust1", since = "1.0.0")]
305305
pub fn new(value: T) -> Rc<T> {
306-
unsafe {
307-
Rc {
308-
// there is an implicit weak pointer owned by all the strong
309-
// pointers, which ensures that the weak destructor never frees
310-
// the allocation while the strong destructor is running, even
311-
// if the weak pointer is stored inside the strong one.
312-
ptr: Shared::new(Box::into_raw(box RcBox {
313-
strong: Cell::new(1),
314-
weak: Cell::new(1),
315-
value: value,
316-
})),
317-
}
306+
Rc {
307+
// there is an implicit weak pointer owned by all the strong
308+
// pointers, which ensures that the weak destructor never frees
309+
// the allocation while the strong destructor is running, even
310+
// if the weak pointer is stored inside the strong one.
311+
ptr: Shared::from(Box::into_unique(box RcBox {
312+
strong: Cell::new(1),
313+
weak: Cell::new(1),
314+
value: value,
315+
})),
318316
}
319317
}
320318

@@ -418,7 +416,7 @@ impl<T> Rc<T> {
418416

419417
let ptr = (ptr as *const u8).offset(-offset_of!(RcBox<T>, value));
420418
Rc {
421-
ptr: Shared::new(ptr as *mut u8 as *mut _)
419+
ptr: Shared::new_unchecked(ptr as *mut u8 as *mut _)
422420
}
423421
}
424422
}
@@ -443,7 +441,7 @@ impl Rc<str> {
443441
// Combine the allocation address and the string length into a fat pointer to `RcBox`.
444442
let rcbox_ptr: *mut RcBox<str> = mem::transmute([ptr as usize, value.len()]);
445443
assert!(aligned_len * size_of::<usize>() == size_of_val(&*rcbox_ptr));
446-
Rc { ptr: Shared::new(rcbox_ptr) }
444+
Rc { ptr: Shared::new_unchecked(rcbox_ptr) }
447445
}
448446
}
449447
}
@@ -476,7 +474,7 @@ impl<T> Rc<[T]> {
476474
// Free the original allocation without freeing its (moved) contents.
477475
box_free(Box::into_raw(value));
478476

479-
Rc { ptr: Shared::new(ptr as *mut _) }
477+
Rc { ptr: Shared::new_unchecked(ptr as *mut _) }
480478
}
481479
}
482480
}
@@ -1016,7 +1014,7 @@ impl<T> Weak<T> {
10161014
pub fn new() -> Weak<T> {
10171015
unsafe {
10181016
Weak {
1019-
ptr: Shared::new(Box::into_raw(box RcBox {
1017+
ptr: Shared::from(Box::into_unique(box RcBox {
10201018
strong: Cell::new(0),
10211019
weak: Cell::new(1),
10221020
value: uninitialized(),

src/liballoc/vec.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1126,7 +1126,7 @@ impl<T> Vec<T> {
11261126
tail_start: end,
11271127
tail_len: len - end,
11281128
iter: range_slice.iter(),
1129-
vec: Shared::new(self as *mut _),
1129+
vec: Shared::from(self),
11301130
}
11311131
}
11321132
}
@@ -1727,7 +1727,7 @@ impl<T> IntoIterator for Vec<T> {
17271727
let cap = self.buf.cap();
17281728
mem::forget(self);
17291729
IntoIter {
1730-
buf: Shared::new(begin),
1730+
buf: Shared::new_unchecked(begin),
17311731
cap: cap,
17321732
ptr: begin,
17331733
end: end,
@@ -1962,6 +1962,12 @@ impl<T> Vec<T> {
19621962

19631963
}
19641964

1965+
/// Extend implementation that copies elements out of references before pushing them onto the Vec.
1966+
///
1967+
/// This implementation is specialized for slice iterators, where it uses [`copy_from_slice`] to
1968+
/// append the entire slice at once.
1969+
///
1970+
/// [`copy_from_slice`]: ../../std/primitive.slice.html#method.copy_from_slice
19651971
#[stable(feature = "extend_ref", since = "1.2.0")]
19661972
impl<'a, T: 'a + Copy> Extend<&'a T> for Vec<T> {
19671973
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {

src/liballoc/vec_deque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ impl<T> VecDeque<T> {
893893
self.head = drain_tail;
894894

895895
Drain {
896-
deque: unsafe { Shared::new(self as *mut _) },
896+
deque: Shared::from(&mut *self),
897897
after_tail: drain_head,
898898
after_head: head,
899899
iter: Iter {

0 commit comments

Comments
 (0)