konst/ptr.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
//! Const equivalents of raw pointer and [`NonNull`](core::ptr::NonNull) methods.
//!
//! # Removed in 0.3.0
//!
//! These functions were removed in 0.3.0 because there is an equivalent
//! const fn in the standard library:
//!
//! - `deref`: raw pointers can be dereferenced since Rust 1.58.0
//!
//! - `deref_mut`: Rust 1.83.0 allows dereferencing mutable pointers.
//!
use core::ptr::NonNull;
/// Const equivalent of
/// [`<*const>::as_ref`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref)
///
/// # Safety
///
/// This function has the same safety requirements as
/// [`<*const T>::as_ref`](https://doc.rust-lang.org/1.55.0/std/primitive.pointer.html#safety)
///
/// # Example
///
/// ```rust
/// use konst::ptr;
///
/// use core::ptr::null;
///
/// const NONE: Option<&u8> = unsafe{ ptr::as_ref(null()) };
/// const SOME: Option<&u8> = unsafe{ ptr::as_ref(&100) };
///
/// assert_eq!(NONE, None);
/// assert_eq!(SOME, Some(&100));
///
///
/// ```
pub const unsafe fn as_ref<'a, T: ?Sized>(ptr: *const T) -> Option<&'a T> {
core::mem::transmute(ptr)
}
/// Const equivalent of
/// [`<*const>::as_mut`](https://doc.rust-lang.org/std/primitive.pointer.html#method.as_mut)
///
/// # Safety
///
/// This function has the same safety requirements as
/// [`<*mut T>::as_mut`](https://doc.rust-lang.org/1.55.0/std/primitive.pointer.html#safety-13).
///
/// # Example
///
/// ```rust
/// use konst::ptr;
///
/// assert_eq!(ARR, [83, 91, 104]);
///
/// const ARR: [u8; 3] = unsafe {
/// let mut arr = [13, 21, 34];
/// mutate(&mut arr[0]);
/// mutate(&mut arr[1]);
/// mutate(&mut arr[2]);
/// mutate(std::ptr::null_mut()); // no-op
/// arr
/// };
///
/// const unsafe fn mutate(x: *mut u8) {
/// if let Some(mutt) = ptr::as_mut(x) {
/// *mutt += 70;
/// }
/// }
/// ```
#[cfg(feature = "rust_1_83")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_83")))]
pub const unsafe fn as_mut<'a, T: ?Sized>(ptr: *mut T) -> Option<&'a mut T> {
core::mem::transmute(ptr)
}
/// Const equivalent of
/// [`<*const T>::is_null`](https://doc.rust-lang.org/std/primitive.pointer.html#method.is_null)
///
/// # Example
///
/// ```rust
/// use konst::ptr;
///
/// use core::ptr::null;
///
/// const NULL_IS_NULL: bool = ptr::is_null(null::<u8>());
/// const REFF_IS_NULL: bool = ptr::is_null(&100);
///
/// assert_eq!(NULL_IS_NULL, true);
/// assert_eq!(REFF_IS_NULL, false);
///
///
/// ```
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub const fn is_null<T: ?Sized>(ptr: *const T) -> bool {
unsafe {
matches!(
core::mem::transmute::<*const T, Option<NonNull<T>>>(ptr),
None
)
}
}
/// Const equivalents of [`NonNull`](core::ptr::NonNull) methods.
pub mod nonnull {
use core::ptr::NonNull;
/// Const equivalent of [`NonNull::new`](core::ptr::NonNull::new).
///
/// # Example
///
/// ```rust
/// use konst::ptr::nonnull;
///
/// use core::ptr::{NonNull, null_mut};
///
/// const NONE: Option<NonNull<u8>> = nonnull::new(null_mut());
/// const SOME: Option<NonNull<u8>> = nonnull::new(&100 as *const _ as *mut _);
///
/// assert!(NONE.is_none());
/// assert_eq!(SOME.map(|x|unsafe{*x.as_ptr()}), Some(100));
///
///
/// ```
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub const fn new<T: ?Sized>(ptr: *mut T) -> Option<NonNull<T>> {
unsafe { core::mem::transmute(ptr) }
}
/// Const equivalent of [`NonNull::as_ref`](core::ptr::NonNull::as_ref).
///
/// # Safety
///
/// This has [the same safety requirements as `NonNull::as_ref`
/// ](https://doc.rust-lang.org/1.55.0/core/ptr/struct.NonNull.html#safety-3)
///
/// # Const stabilization
///
/// The equivalent std function was const-stabilized in Rust 1.73.0.
///
/// # Example
///
/// ```rust
/// use konst::ptr::nonnull;
///
/// use core::{
/// ptr::NonNull,
/// marker::PhantomData,
/// };
///
/// const A: NonNull<u8> = nonnull::from_ref(&3);
/// const A_REF: &u8 = unsafe{ nonnull::as_ref(A) };
/// assert_eq!(A_REF, &3);
///
/// const B: NonNull<str> = nonnull::from_ref("hello");
/// const B_REF: &str = unsafe{ nonnull::as_ref(B) };
/// assert_eq!(B_REF, "hello");
///
/// ```
///
pub const unsafe fn as_ref<'a, T: ?Sized>(ptr: NonNull<T>) -> &'a T {
&*(ptr.as_ptr() as *const T)
}
/// Const equivalent of [`NonNull::as_mut`](core::ptr::NonNull::as_mut).
///
/// # Safety
///
/// This has [the same safety requirements as `NonNull::as_mut`
/// ](https://doc.rust-lang.org/1.55.0/std/ptr/struct.NonNull.html#safety-4)
///
/// # Const stabilization
///
/// The equivalent std function was const-stabilized in Rust 1.83.0.
///
/// # Example
///
/// ```rust
/// use konst::ptr::nonnull;
///
/// use core::ptr::NonNull;
///
/// assert_eq!(TUP, (13, 15, 18));
///
/// const TUP: (u8, u8, u8) = unsafe {
/// let mut tuple = (3, 5, 8);
/// mutate(nonnull::from_mut(&mut tuple.0));
/// mutate(nonnull::from_mut(&mut tuple.1));
/// mutate(nonnull::from_mut(&mut tuple.2));
/// tuple
/// };
///
/// const unsafe fn mutate(x: NonNull<u8>) {
/// *nonnull::as_mut(x) += 10;
/// }
///
/// ```
///
#[cfg(feature = "rust_1_83")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_83")))]
pub const unsafe fn as_mut<'a, T: ?Sized>(ptr: NonNull<T>) -> &'a mut T {
&mut *ptr.as_ptr()
}
/// Const equivalent of
/// [`<NonNull<T> as From<&T>>::from`
/// ](https://doc.rust-lang.org/1.55.0/std/ptr/struct.NonNull.html#impl-From%3C%26%27_%20T%3E)
///
/// # Example
///
/// ```rust
/// use konst::ptr::nonnull;
///
/// use core::ptr::NonNull;
///
/// const H: NonNull<str> = nonnull::from_ref("hello");
/// const W: NonNull<str> = nonnull::from_ref("world");
///
/// unsafe{
/// assert_eq!(H.as_ref(), "hello");
/// assert_eq!(W.as_ref(), "world");
/// }
/// ```
pub const fn from_ref<T: ?Sized>(reff: &T) -> NonNull<T> {
// SAFETY: `&T` is non-null, which is all that `NonNull::new_unchecked` requires
unsafe { NonNull::new_unchecked(reff as *const _ as *mut _) }
}
/// Const equivalent of
/// [`<NonNull<T> as From<&mut T>>::from`
/// ](https://doc.rust-lang.org/1.55.0/std/ptr/struct.NonNull.html#impl-From%3C%26%27_%20mut%20T%3E)
///
/// # Example
///
/// ```rust
/// use konst::ptr::nonnull as nn;
///
/// use core::ptr::NonNull;
///
/// assert_eq!(ARR, (5, 8, 3));
///
/// const ARR: (u8, u8, u8) = unsafe {
/// let mut tup = (3, 5, 8);
/// swap(nn::from_mut(&mut tup.0), nn::from_mut(&mut tup.1));
/// swap(nn::from_mut(&mut tup.1), nn::from_mut(&mut tup.2));
/// tup
/// };
///
/// const unsafe fn swap(x: NonNull<u8>, y: NonNull<u8>) {
/// let xm = nn::as_mut(x);
/// let ym = nn::as_mut(y);
/// let tmp = *xm;
/// *xm = *ym;
/// *ym = tmp;
/// }
///
/// ```
///
#[cfg(feature = "rust_1_83")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "rust_1_83")))]
pub const fn from_mut<T: ?Sized>(mutt: &mut T) -> NonNull<T> {
// SAFETY: `&mut T` is non-null, which is all that `NonNull::new_unchecked` requires
unsafe { NonNull::new_unchecked(mutt) }
}
}