Implemented const casts of raw pointers
This adds `as_mut()` method for `*const T` and `as_const()` for `*mut T` which are intended to make casting of consts safer. This was discussed in the [internals discussion][discussion]. [discussion]: https://internals.rust-lang.org/t/casting-constness-can-be-risky-heres-a-simple-fix/15933
This commit is contained in:
parent
e012a191d7
commit
1a96623513
2 changed files with 24 additions and 0 deletions
|
@ -48,6 +48,16 @@ impl<T: ?Sized> *const T {
|
|||
self as _
|
||||
}
|
||||
|
||||
/// Changes constness without changing the type.
|
||||
///
|
||||
/// This is a bit safer than `as` because it wouldn't silently change the type if the code is
|
||||
/// refactored.
|
||||
#[unstable(feature = "ptr_const_cast", issue = "92675")]
|
||||
#[rustc_const_unstable(feature = "ptr_const_cast", issue = "92675")]
|
||||
pub const fn as_mut(self) -> *mut T {
|
||||
self as _
|
||||
}
|
||||
|
||||
/// Casts a pointer to its raw bits.
|
||||
///
|
||||
/// This is equivalent to `as usize`, but is more specific to enhance readability.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue