Skip to content
Permalink
Browse files

Add a `cast` method to raw pointers.

This is similar to `NonNull::cast`.

Compared to the `as` operator (which has a wide range of meanings
depending on the input and output types), a call to this method:

* Can only go from a raw pointer to a raw pointer
* Cannot change the pointer’s `const`ness

… even when the pointed types are inferred based on context.
  • Loading branch information...
SimonSapin committed May 7, 2019
1 parent a19cf18 commit d5e819015f550bedb27bc287f96f3937bb831cd5
Showing with 14 additions and 0 deletions.
  1. +14 −0 src/libcore/ptr.rs
@@ -974,6 +974,13 @@ impl<T: ?Sized> *const T {
(self as *const u8) == null()
}

/// Cast to a pointer to a different type
#[unstable(feature = "ptr_cast", issue = "60602")]
#[inline]
pub const fn cast<U>(self) -> *const U {
self as _
}

/// Returns `None` if the pointer is null, or else returns a reference to
/// the value wrapped in `Some`.
///
@@ -1593,6 +1600,13 @@ impl<T: ?Sized> *mut T {
(self as *mut u8) == null_mut()
}

/// Cast to a pointer to a different type
#[unstable(feature = "ptr_cast", issue = "60602")]
#[inline]
pub const fn cast<U>(self) -> *mut U {
self as _
}

/// Returns `None` if the pointer is null, or else returns a reference to
/// the value wrapped in `Some`.
///

0 comments on commit d5e8190

Please sign in to comment.
You can’t perform that action at this time.