pub const fn nonnull_from<T: ?Sized>(
src: impl Reference<Referee = T>,
) -> NonNull<T>
Expand description
Converts &T
or &mut T
into NonNull<T>
.
This function is a const version of the From<&T>
and From<&mut T>
implementations of
NonNull<T>
. The returned pointer will have the same mutability as the argument.
This function is useful to avoid accidentally converting a mutable reference to an immutable one when copying around code.
ยงExample
use const_util::mem::nonnull_from;
use core::ptr::NonNull;
let mut x = 1;
assert_eq!(
nonnull_from(&x),
NonNull::from(&x),
);
assert_eq!(
nonnull_from(&mut x),
NonNull::from(&mut x),
);
unsafe { nonnull_from(&mut x).write(2) };
assert_eq!(unsafe { nonnull_from(&x).read() }, 2);