Skip to content

Commit fda97db

Browse files
Update non_null.rs
1 parent 9196acb commit fda97db

File tree

1 file changed

+12
-48
lines changed

1 file changed

+12
-48
lines changed

library/core/src/ptr/non_null.rs

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,7 @@ impl<T: ?Sized> NonNull<T> {
249249
#[inline]
250250
pub const fn from_ref(r: &T) -> Self {
251251
// SAFETY: A reference cannot be null.
252-
unsafe {
253-
NonNull {
254-
pointer: r as *const T,
255-
}
256-
}
252+
unsafe { NonNull { pointer: r as *const T } }
257253
}
258254

259255
/// Converts a mutable reference to a `NonNull` pointer.
@@ -262,11 +258,7 @@ impl<T: ?Sized> NonNull<T> {
262258
#[inline]
263259
pub const fn from_mut(r: &mut T) -> Self {
264260
// SAFETY: A mutable reference cannot be null.
265-
unsafe {
266-
NonNull {
267-
pointer: r as *mut T,
268-
}
269-
}
261+
unsafe { NonNull { pointer: r as *mut T } }
270262
}
271263

272264
/// Performs the same functionality as [`std::ptr::from_raw_parts`], except that a
@@ -403,7 +395,7 @@ impl<T: ?Sized> NonNull<T> {
403395
#[rustc_const_stable(feature = "const_nonnull_as_ref", since = "1.73.0")]
404396
#[must_use]
405397
#[inline(always)]
406-
#[requires(ub_checks::can_dereference(self.as_ptr() as *const()))] // Ensure input is convertible to a reference
398+
#[requires(ub_checks::can_dereference(self.as_ptr() as *const()))] // Ensure input is convertible to a reference
407399
#[ensures(|result: &&T| core::ptr::eq(*result, self.as_ptr()))] // Ensure returned reference matches pointer
408400
pub const unsafe fn as_ref<'a>(&self) -> &'a T {
409401
// SAFETY: the caller must guarantee that `self` meets all the
@@ -472,11 +464,7 @@ impl<T: ?Sized> NonNull<T> {
472464
#[inline]
473465
pub const fn cast<U>(self) -> NonNull<U> {
474466
// SAFETY: `self` is a `NonNull` pointer which is necessarily non-null
475-
unsafe {
476-
NonNull {
477-
pointer: self.as_ptr() as *mut U,
478-
}
479-
}
467+
unsafe { NonNull { pointer: self.as_ptr() as *mut U } }
480468
}
481469

482470
/// Adds an offset to a pointer.
@@ -534,11 +522,7 @@ impl<T: ?Sized> NonNull<T> {
534522
// Additionally safety contract of `offset` guarantees that the resulting pointer is
535523
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
536524
// construct `NonNull`.
537-
unsafe {
538-
NonNull {
539-
pointer: intrinsics::offset(self.pointer, count),
540-
}
541-
}
525+
unsafe { NonNull { pointer: intrinsics::offset(self.pointer, count) } }
542526
}
543527

544528
/// Calculates the offset from a pointer in bytes.
@@ -562,11 +546,7 @@ impl<T: ?Sized> NonNull<T> {
562546
// Additionally safety contract of `offset` guarantees that the resulting pointer is
563547
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
564548
// construct `NonNull`.
565-
unsafe {
566-
NonNull {
567-
pointer: self.pointer.byte_offset(count),
568-
}
569-
}
549+
unsafe { NonNull { pointer: self.pointer.byte_offset(count) } }
570550
}
571551

572552
/// Adds an offset to a pointer (convenience for `.offset(count as isize)`).
@@ -623,11 +603,7 @@ impl<T: ?Sized> NonNull<T> {
623603
// Additionally safety contract of `offset` guarantees that the resulting pointer is
624604
// pointing to an allocation, there can't be an allocation at null, thus it's safe to
625605
// construct `NonNull`.
626-
unsafe {
627-
NonNull {
628-
pointer: intrinsics::offset(self.pointer, count),
629-
}
630-
}
606+
unsafe { NonNull { pointer: intrinsics::offset(self.pointer, count) } }
631607
}
632608

633609
/// Calculates the offset from a pointer in bytes (convenience for `.byte_offset(count as isize)`).
@@ -651,11 +627,7 @@ impl<T: ?Sized> NonNull<T> {
651627
// Additionally safety contract of `add` guarantees that the resulting pointer is pointing
652628
// to an allocation, there can't be an allocation at null, thus it's safe to construct
653629
// `NonNull`.
654-
unsafe {
655-
NonNull {
656-
pointer: self.pointer.byte_add(count),
657-
}
658-
}
630+
unsafe { NonNull { pointer: self.pointer.byte_add(count) } }
659631
}
660632

661633
/// Subtracts an offset from a pointer (convenience for
@@ -752,11 +724,7 @@ impl<T: ?Sized> NonNull<T> {
752724
// Additionally safety contract of `sub` guarantees that the resulting pointer is pointing
753725
// to an allocation, there can't be an allocation at null, thus it's safe to construct
754726
// `NonNull`.
755-
unsafe {
756-
NonNull {
757-
pointer: self.pointer.byte_sub(count),
758-
}
759-
}
727+
unsafe { NonNull { pointer: self.pointer.byte_sub(count) } }
760728
}
761729

762730
/// Calculates the distance between two pointers within the same allocation. The returned value is in
@@ -1808,7 +1776,7 @@ impl<T> NonNull<[T]> {
18081776
#[requires(self.len().checked_mul(core::mem::size_of::<T>()).is_some() && self.len() * core::mem::size_of::<T>() <= isize::MAX as usize)] // Ensure the slice size does not exceed isize::MAX
18091777
#[requires(kani::mem::same_allocation(self.as_ptr() as *const(), self.as_ptr().byte_add(self.len() * core::mem::size_of::<T>()) as *const()))] // Ensure the slice is contained within a single allocation
18101778
#[ensures(|result: &&mut [MaybeUninit<T>]| result.len() == self.len())] // Length check
1811-
#[ensures(|result: &&mut [MaybeUninit<T>]| core::ptr::eq(result.as_ptr(), self.cast().as_ptr()))] // Address check
1779+
#[ensures(|result: &&mut [MaybeUninit<T>]| core::ptr::eq(result.as_ptr(), self.cast().as_ptr()))] // Address check
18121780
pub const unsafe fn as_uninit_slice_mut<'a>(self) -> &'a mut [MaybeUninit<T>] {
18131781
// SAFETY: the caller must uphold the safety contract for `as_uninit_slice_mut`.
18141782
unsafe { slice::from_raw_parts_mut(self.cast().as_ptr(), self.len()) }
@@ -1951,7 +1919,7 @@ impl<T: ?Sized> From<&T> for NonNull<T> {
19511919
}
19521920

19531921
#[cfg(kani)]
1954-
#[unstable(feature = "kani", issue = "none")]
1922+
#[unstable(feature="kani", issue="none")]
19551923
mod verify {
19561924
use super::*;
19571925
use crate::mem;
@@ -1996,11 +1964,7 @@ mod verify {
19961964
pub fn non_null_check_new() {
19971965
let mut x: i32 = kani::any();
19981966
let xptr = &mut x;
1999-
let maybe_null_ptr = if kani::any() {
2000-
xptr as *mut i32
2001-
} else {
2002-
null_mut()
2003-
};
1967+
let maybe_null_ptr = if kani::any() { xptr as *mut i32 } else { null_mut() };
20041968
let _ = NonNull::new(maybe_null_ptr);
20051969
}
20061970

0 commit comments

Comments
 (0)