/// Usually this won't be necessary; covariance is correct for most safe abstractions,
/// such as Box, Rc, Arc, Vec, and LinkedList. This is the case because they
/// provide a public API that follows the normal shared XOR mutable rules of Rust.
-#[unstable(feature = "shared", reason = "needs an RFC to flesh out design",
- issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
pub struct NonNull<T: ?Sized> {
pointer: NonZero<*const T>,
}
-#[unstable(feature = "shared", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<T: ?Sized> fmt::Debug for NonNull<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:p}", self.as_ptr())
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
// NB: This impl is unnecessary, but should provide better error messages.
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<T: ?Sized> !Send for NonNull<T> { }
/// `NonNull` pointers are not `Sync` because the data they reference may be aliased.
// NB: This impl is unnecessary, but should provide better error messages.
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<T: ?Sized> !Sync for NonNull<T> { }
-#[unstable(feature = "nonnull", issue = "27730")]
impl<T: Sized> NonNull<T> {
/// Creates a new `NonNull` that is dangling, but well-aligned.
///
/// This is useful for initializing types which lazily allocate, like
/// `Vec::new` does.
+ #[stable(feature = "nonnull", since = "1.24.0")]
pub fn empty() -> Self {
unsafe {
let ptr = mem::align_of::<T>() as *mut T;
}
}
-#[unstable(feature = "nonnull", issue = "27730")]
impl<T: ?Sized> NonNull<T> {
/// Creates a new `NonNull`.
///
/// # Safety
///
/// `ptr` must be non-null.
- #[unstable(feature = "nonnull", issue = "27730")]
+ #[stable(feature = "nonnull", since = "1.24.0")]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
NonNull { pointer: NonZero::new_unchecked(ptr) }
}
/// Creates a new `NonNull` if `ptr` is non-null.
+ #[stable(feature = "nonnull", since = "1.24.0")]
pub fn new(ptr: *mut T) -> Option<Self> {
NonZero::new(ptr as *const T).map(|nz| NonNull { pointer: nz })
}
/// Acquires the underlying `*mut` pointer.
+ #[stable(feature = "nonnull", since = "1.24.0")]
pub fn as_ptr(self) -> *mut T {
self.pointer.get() as *mut T
}
/// The resulting lifetime is bound to self so this behaves "as if"
/// it were actually an instance of T that is getting borrowed. If a longer
/// (unbound) lifetime is needed, use `&*my_ptr.ptr()`.
+ #[stable(feature = "nonnull", since = "1.24.0")]
pub unsafe fn as_ref(&self) -> &T {
&*self.as_ptr()
}
/// The resulting lifetime is bound to self so this behaves "as if"
/// it were actually an instance of T that is getting borrowed. If a longer
/// (unbound) lifetime is needed, use `&mut *my_ptr.ptr_mut()`.
+ #[stable(feature = "nonnull", since = "1.24.0")]
pub unsafe fn as_mut(&mut self) -> &mut T {
&mut *self.as_ptr()
}
}
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<T: ?Sized> Clone for NonNull<T> {
fn clone(&self) -> Self {
*self
}
}
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<T: ?Sized> Copy for NonNull<T> { }
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<T: ?Sized, U: ?Sized> CoerceUnsized<NonNull<U>> for NonNull<T> where T: Unsize<U> { }
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<T: ?Sized> fmt::Pointer for NonNull<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Pointer::fmt(&self.as_ptr(), f)
}
}
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
fn from(unique: Unique<T>) -> Self {
NonNull { pointer: unique.pointer }
}
}
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
fn from(reference: &'a mut T) -> Self {
NonNull { pointer: NonZero::from(reference) }
}
}
-#[unstable(feature = "nonnull", issue = "27730")]
+#[stable(feature = "nonnull", since = "1.24.0")]
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
fn from(reference: &'a T) -> Self {
NonNull { pointer: NonZero::from(reference) }