Skip to content

Commit 8fd5587

Browse files
authored
Rollup merge of rust-lang#60601 - SimonSapin:cast, r=Kimundi
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.
2 parents ef01f29 + d5e8190 commit 8fd5587

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/libcore/ptr.rs

+14
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,13 @@ impl<T: ?Sized> *const T {
974974
(self as *const u8) == null()
975975
}
976976

977+
/// Cast to a pointer to a different type
978+
#[unstable(feature = "ptr_cast", issue = "60602")]
979+
#[inline]
980+
pub const fn cast<U>(self) -> *const U {
981+
self as _
982+
}
983+
977984
/// Returns `None` if the pointer is null, or else returns a reference to
978985
/// the value wrapped in `Some`.
979986
///
@@ -1593,6 +1600,13 @@ impl<T: ?Sized> *mut T {
15931600
(self as *mut u8) == null_mut()
15941601
}
15951602

1603+
/// Cast to a pointer to a different type
1604+
#[unstable(feature = "ptr_cast", issue = "60602")]
1605+
#[inline]
1606+
pub const fn cast<U>(self) -> *mut U {
1607+
self as _
1608+
}
1609+
15961610
/// Returns `None` if the pointer is null, or else returns a reference to
15971611
/// the value wrapped in `Some`.
15981612
///

0 commit comments

Comments
 (0)