+use crate::cmp::Ordering;
use crate::convert::From;
-use crate::ops::{CoerceUnsized, DispatchFromDyn};
use crate::fmt;
use crate::hash;
use crate::marker::Unsize;
use crate::mem;
+use crate::ops::{CoerceUnsized, DispatchFromDyn};
use crate::ptr::Unique;
-use crate::cmp::Ordering;
+
+// ignore-tidy-undocumented-unsafe
/// `*mut T` but non-zero and covariant.
///
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
// N.B., this impl is unnecessary, but should provide better error messages.
#[stable(feature = "nonnull", since = "1.25.0")]
-impl<T: ?Sized> !Send for NonNull<T> { }
+impl<T: ?Sized> !Send for NonNull<T> {}
/// `NonNull` pointers are not `Sync` because the data they reference may be aliased.
// N.B., this impl is unnecessary, but should provide better error messages.
#[stable(feature = "nonnull", since = "1.25.0")]
-impl<T: ?Sized> !Sync for NonNull<T> { }
+impl<T: ?Sized> !Sync for NonNull<T> {}
impl<T: Sized> NonNull<T> {
/// Creates a new `NonNull` that is dangling, but well-aligned.
/// sentinel value. Types that lazily allocate must track initialization by
/// some other means.
#[stable(feature = "nonnull", since = "1.25.0")]
+ #[cfg_attr(
+ not(bootstrap),
+ rustc_const_stable(feature = "const_nonnull_dangling", since = "1.32.0"),
+ )]
#[inline]
pub const fn dangling() -> Self {
unsafe {
///
/// `ptr` must be non-null.
#[stable(feature = "nonnull", since = "1.25.0")]
+ #[cfg_attr(
+ not(bootstrap),
+ rustc_const_stable(feature = "const_nonnull_new_unchecked", since = "1.32.0"),
+ )]
#[inline]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
NonNull { pointer: ptr as _ }
#[stable(feature = "nonnull", since = "1.25.0")]
#[inline]
pub fn new(ptr: *mut T) -> Option<Self> {
- if !ptr.is_null() {
- Some(unsafe { Self::new_unchecked(ptr) })
- } else {
- None
- }
+ if !ptr.is_null() { Some(unsafe { Self::new_unchecked(ptr) }) } else { None }
}
/// Acquires the underlying `*mut` pointer.
#[stable(feature = "nonnull", since = "1.25.0")]
+ #[cfg_attr(
+ not(bootstrap),
+ rustc_const_stable(feature = "const_nonnull_as_ptr", since = "1.32.0"),
+ )]
#[inline]
pub const fn as_ptr(self) -> *mut T {
self.pointer as *mut T
/// Casts to a pointer of another type.
#[stable(feature = "nonnull_cast", since = "1.27.0")]
+ #[cfg_attr(
+ not(bootstrap),
+ rustc_const_stable(feature = "const_nonnull_cast", since = "1.32.0"),
+ )]
#[inline]
pub const fn cast<U>(self) -> NonNull<U> {
- unsafe {
- NonNull::new_unchecked(self.as_ptr() as *mut U)
- }
+ unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) }
}
}
}
#[stable(feature = "nonnull", since = "1.25.0")]
-impl<T: ?Sized> Copy for NonNull<T> { }
+impl<T: ?Sized> Copy for NonNull<T> {}
#[unstable(feature = "coerce_unsized", issue = "27732")]
-impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> { }
+impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
#[unstable(feature = "dispatch_from_dyn", issue = "0")]
-impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> { }
+impl<T: ?Sized, U: ?Sized> DispatchFromDyn<NonNull<U>> for NonNull<T> where T: Unsize<U> {}
#[stable(feature = "nonnull", since = "1.25.0")]
impl<T: ?Sized> fmt::Debug for NonNull<T> {