Skip to content

Commit 0a08ad0

Browse files
committed
Rename {NonZero,Shared,Unique}::new to new_unchecked
1 parent e9af03a commit 0a08ad0

File tree

20 files changed

+47
-47
lines changed

20 files changed

+47
-47
lines changed

src/doc/nomicon

src/liballoc/allocator.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 3 deletions
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: unsafe { Shared::new_unchecked(Box::into_raw(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::new_unchecked(Box::into_raw(box ArcInner {
846846
strong: atomic::AtomicUsize::new(0),
847847
weak: atomic::AtomicUsize::new(1),
848848
data: uninitialized(),

src/liballoc/btree/node.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,23 @@ struct BoxedNode<K, V> {
141141
impl<K, V> BoxedNode<K, V> {
142142
fn from_leaf(node: Box<LeafNode<K, V>>) -> Self {
143143
unsafe {
144-
BoxedNode { ptr: Unique::new(Box::into_raw(node)) }
144+
BoxedNode { ptr: Unique::new_unchecked(Box::into_raw(node)) }
145145
}
146146
}
147147

148148
fn from_internal(node: Box<InternalNode<K, V>>) -> Self {
149149
unsafe {
150-
BoxedNode { ptr: Unique::new(Box::into_raw(node) as *mut LeafNode<K, V>) }
150+
BoxedNode { ptr: Unique::new_unchecked(Box::into_raw(node) as *mut LeafNode<K, V>) }
151151
}
152152
}
153153

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

158158
fn as_ptr(&self) -> NonZero<*const LeafNode<K, V>> {
159159
unsafe {
160-
NonZero::new(self.ptr.as_ptr())
160+
NonZero::new_unchecked(self.ptr.as_ptr())
161161
}
162162
}
163163
}
@@ -391,7 +391,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
391391
node: NodeRef {
392392
height: self.height + 1,
393393
node: unsafe {
394-
NonZero::new(self.as_leaf().parent as *mut LeafNode<K, V>)
394+
NonZero::new_unchecked(self.as_leaf().parent as *mut LeafNode<K, V>)
395395
},
396396
root: self.root,
397397
_marker: PhantomData

src/liballoc/linked_list.rs

Lines changed: 3 additions & 3 deletions
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::new_unchecked(Box::into_raw(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::new_unchecked(Box::into_raw(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::new_unchecked(Box::into_raw(box Node {
925925
next: Some(head),
926926
prev: Some(prev),
927927
element: element,

src/liballoc/raw_vec.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<T> Rc<T> {
309309
// pointers, which ensures that the weak destructor never frees
310310
// the allocation while the strong destructor is running, even
311311
// if the weak pointer is stored inside the strong one.
312-
ptr: Shared::new(Box::into_raw(box RcBox {
312+
ptr: Shared::new_unchecked(Box::into_raw(box RcBox {
313313
strong: Cell::new(1),
314314
weak: Cell::new(1),
315315
value: value,
@@ -418,7 +418,7 @@ impl<T> Rc<T> {
418418

419419
let ptr = (ptr as *const u8).offset(-offset_of!(RcBox<T>, value));
420420
Rc {
421-
ptr: Shared::new(ptr as *mut u8 as *mut _)
421+
ptr: Shared::new_unchecked(ptr as *mut u8 as *mut _)
422422
}
423423
}
424424
}
@@ -443,7 +443,7 @@ impl Rc<str> {
443443
// Combine the allocation address and the string length into a fat pointer to `RcBox`.
444444
let rcbox_ptr: *mut RcBox<str> = mem::transmute([ptr as usize, value.len()]);
445445
assert!(aligned_len * size_of::<usize>() == size_of_val(&*rcbox_ptr));
446-
Rc { ptr: Shared::new(rcbox_ptr) }
446+
Rc { ptr: Shared::new_unchecked(rcbox_ptr) }
447447
}
448448
}
449449
}
@@ -476,7 +476,7 @@ impl<T> Rc<[T]> {
476476
// Free the original allocation without freeing its (moved) contents.
477477
box_free(Box::into_raw(value));
478478

479-
Rc { ptr: Shared::new(ptr as *mut _) }
479+
Rc { ptr: Shared::new_unchecked(ptr as *mut _) }
480480
}
481481
}
482482
}
@@ -1016,7 +1016,7 @@ impl<T> Weak<T> {
10161016
pub fn new() -> Weak<T> {
10171017
unsafe {
10181018
Weak {
1019-
ptr: Shared::new(Box::into_raw(box RcBox {
1019+
ptr: Shared::new_unchecked(Box::into_raw(box RcBox {
10201020
strong: Cell::new(0),
10211021
weak: Cell::new(1),
10221022
value: uninitialized(),

src/liballoc/vec.rs

Lines changed: 2 additions & 2 deletions
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::new_unchecked(self as *mut _),
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,

src/liballoc/vec_deque.rs

Lines changed: 1 addition & 1 deletion
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: unsafe { Shared::new_unchecked(self as *mut _) },
897897
after_tail: drain_head,
898898
after_head: head,
899899
iter: Iter {

src/libcore/nonzero.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl<T: Zeroable> NonZero<T> {
6969
/// Creates an instance of NonZero with the provided value.
7070
/// You must indeed ensure that the value is actually "non-zero".
7171
#[inline]
72-
pub const unsafe fn new(inner: T) -> Self {
72+
pub const unsafe fn new_unchecked(inner: T) -> Self {
7373
NonZero(inner)
7474
}
7575

src/libcore/ptr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ impl<T: Sized> Unique<T> {
10981098
pub fn empty() -> Self {
10991099
unsafe {
11001100
let ptr = mem::align_of::<T>() as *mut T;
1101-
Unique::new(ptr)
1101+
Unique::new_unchecked(ptr)
11021102
}
11031103
}
11041104
}
@@ -1110,8 +1110,8 @@ impl<T: ?Sized> Unique<T> {
11101110
/// # Safety
11111111
///
11121112
/// `ptr` must be non-null.
1113-
pub const unsafe fn new(ptr: *mut T) -> Self {
1114-
Unique { pointer: NonZero::new(ptr), _marker: PhantomData }
1113+
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
1114+
Unique { pointer: NonZero::new_unchecked(ptr), _marker: PhantomData }
11151115
}
11161116

11171117
/// Creates a new `Unique` if `ptr` is non-null.
@@ -1217,7 +1217,7 @@ impl<T: Sized> Shared<T> {
12171217
pub fn empty() -> Self {
12181218
unsafe {
12191219
let ptr = mem::align_of::<T>() as *mut T;
1220-
Shared::new(ptr)
1220+
Shared::new_unchecked(ptr)
12211221
}
12221222
}
12231223
}
@@ -1229,8 +1229,8 @@ impl<T: ?Sized> Shared<T> {
12291229
/// # Safety
12301230
///
12311231
/// `ptr` must be non-null.
1232-
pub const unsafe fn new(ptr: *mut T) -> Self {
1233-
Shared { pointer: NonZero::new(ptr), _marker: PhantomData }
1232+
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
1233+
Shared { pointer: NonZero::new_unchecked(ptr), _marker: PhantomData }
12341234
}
12351235

12361236
/// Creates a new `Shared` if `ptr` is non-null.

src/libcore/tests/nonzero.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::mem::size_of;
1616
#[test]
1717
fn test_create_nonzero_instance() {
1818
let _a = unsafe {
19-
NonZero::new(21)
19+
NonZero::new_unchecked(21)
2020
};
2121
}
2222

@@ -28,14 +28,14 @@ fn test_size_nonzero_in_option() {
2828
#[test]
2929
fn test_match_on_nonzero_option() {
3030
let a = Some(unsafe {
31-
NonZero::new(42)
31+
NonZero::new_unchecked(42)
3232
});
3333
match a {
3434
Some(val) => assert_eq!(val.get(), 42),
3535
None => panic!("unexpected None while matching on Some(NonZero(_))")
3636
}
3737

38-
match unsafe { Some(NonZero::new(43)) } {
38+
match unsafe { Some(NonZero::new_unchecked(43)) } {
3939
Some(val) => assert_eq!(val.get(), 43),
4040
None => panic!("unexpected None while matching on Some(NonZero(_))")
4141
}

src/libcore/tests/ptr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ fn test_set_memory() {
167167
#[test]
168168
fn test_unsized_unique() {
169169
let xs: &[i32] = &[1, 2, 3];
170-
let ptr = unsafe { Unique::new(xs as *const [i32] as *mut [i32]) };
170+
let ptr = unsafe { Unique::new_unchecked(xs as *const [i32] as *mut [i32]) };
171171
let ys = unsafe { ptr.as_ref() };
172172
let zs: &[i32] = &[1, 2, 3];
173173
assert!(ys == zs);

src/librustc/ty/subst.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'tcx> From<Ty<'tcx>> for Kind<'tcx> {
4747
let ptr = ty as *const _ as usize;
4848
Kind {
4949
ptr: unsafe {
50-
NonZero::new(ptr | TYPE_TAG)
50+
NonZero::new_unchecked(ptr | TYPE_TAG)
5151
},
5252
marker: PhantomData
5353
}
@@ -62,7 +62,7 @@ impl<'tcx> From<ty::Region<'tcx>> for Kind<'tcx> {
6262
let ptr = r as *const _ as usize;
6363
Kind {
6464
ptr: unsafe {
65-
NonZero::new(ptr | REGION_TAG)
65+
NonZero::new_unchecked(ptr | REGION_TAG)
6666
},
6767
marker: PhantomData
6868
}

src/librustc_data_structures/array_vec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<A: Array> ArrayVec<A> {
146146
tail_start: end,
147147
tail_len: len - end,
148148
iter: range_slice.iter(),
149-
array_vec: Shared::new(self as *mut _),
149+
array_vec: Shared::new_unchecked(self as *mut _),
150150
}
151151
}
152152
}

src/librustc_data_structures/obligation_forest/node_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct NodeIndex {
1919
impl NodeIndex {
2020
pub fn new(value: usize) -> NodeIndex {
2121
assert!(value < (u32::MAX as usize));
22-
unsafe { NodeIndex { index: NonZero::new((value as u32) + 1) } }
22+
unsafe { NodeIndex { index: NonZero::new_unchecked((value as u32) + 1) } }
2323
}
2424

2525
pub fn get(self) -> usize {

src/librustc_mir/dataflow/move_paths/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) mod indexes {
4242

4343
impl Idx for $Index {
4444
fn new(idx: usize) -> Self {
45-
unsafe { $Index(NonZero::new(idx + 1)) }
45+
unsafe { $Index(NonZero::new_unchecked(idx + 1)) }
4646
}
4747
fn index(self) -> usize {
4848
self.0.get() - 1

src/libstd/collections/hash/table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl TaggedHashUintPtr {
4444
#[inline]
4545
unsafe fn new(ptr: *mut HashUint) -> Self {
4646
debug_assert!(ptr as usize & 1 == 0 || ptr as usize == EMPTY as usize);
47-
TaggedHashUintPtr(Unique::new(ptr))
47+
TaggedHashUintPtr(Unique::new_unchecked(ptr))
4848
}
4949

5050
#[inline]
@@ -56,7 +56,7 @@ impl TaggedHashUintPtr {
5656
} else {
5757
usize_ptr &= !1;
5858
}
59-
self.0 = Unique::new(usize_ptr as *mut HashUint)
59+
self.0 = Unique::new_unchecked(usize_ptr as *mut HashUint)
6060
}
6161
}
6262

@@ -877,7 +877,7 @@ impl<K, V> RawTable<K, V> {
877877
elems_left: elems_left,
878878
marker: marker::PhantomData,
879879
},
880-
table: unsafe { Shared::new(self) },
880+
table: unsafe { Shared::new_unchecked(self) },
881881
marker: marker::PhantomData,
882882
}
883883
}

src/test/run-pass/issue-23433.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::ptr::Unique;
1616

1717
fn main() {
1818
let mut a = [0u8; 5];
19-
let b: Option<Unique<[u8]>> = unsafe { Some(Unique::new(&mut a)) };
19+
let b: Option<Unique<[u8]>> = unsafe { Some(Unique::new_unchecked(&mut a)) };
2020
match b {
2121
Some(_) => println!("Got `Some`"),
2222
None => panic!("Unexpected `None`"),

src/test/ui/print_type_sizes/nullable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub struct NestedNonZero<T: Zeroable> {
5757
impl<T: Zeroable+Default> Default for NestedNonZero<T> {
5858
fn default() -> Self {
5959
unsafe {
60-
NestedNonZero { pre: 0, val: NonZero::new(Default::default()), post: 0 }
60+
NestedNonZero { pre: 0, val: NonZero::new_unchecked(Default::default()), post: 0 }
6161
}
6262
}
6363
}

0 commit comments

Comments
 (0)