pub struct ConstPtr<T: ?Sized>(/* private fields */);Expand description
A pointer that references immutable data and is never Null.
Implementations§
Source§impl<T: ?Sized> ConstPtr<T>
impl<T: ?Sized> ConstPtr<T>
Sourcepub const unsafe fn new_unchecked(ptr: *const T) -> Self
pub const unsafe fn new_unchecked(ptr: *const T) -> Self
Sourcepub const fn from(reference: &T) -> Self
pub const fn from(reference: &T) -> Self
Creates a new ConstPtr from a reference which makes it safe. This is a const fn.
§Examples
use constptr::ConstPtr;
static x: u32 = 0u32;
let ptr = ConstPtr::from(&x);Sourcepub fn new(ptr: *const T) -> Option<Self>
pub fn new(ptr: *const T) -> Option<Self>
Creates a new ConstPtr if ptr is non-null.
§Examples
use constptr::ConstPtr;
let x = 0u32;
let ptr = ConstPtr::new(&x).expect("ptr is null!");
if let Some(ptr) = ConstPtr::new(std::ptr::null::<u32>()) {
unreachable!();
}Sourcepub fn addr(self) -> NonZeroUsize
pub fn addr(self) -> NonZeroUsize
Gets the “address” portion of the pointer.
For more details see the equivalent method on a raw pointer pointer::addr.
Sourcepub const fn as_ptr(self) -> *const T
pub const fn as_ptr(self) -> *const T
Acquires the underlying *const pointer.
§Examples
use constptr::ConstPtr;
let mut x = 0u32;
let ptr = ConstPtr::new(&mut x).expect("ptr is null!");
let x_value = unsafe { *ptr.as_ptr() };
assert_eq!(x_value, 0);Sourcepub const unsafe fn as_ref<'a>(&self) -> &'a T
pub const unsafe fn as_ref<'a>(&self) -> &'a T
Returns a shared reference to the value.
§Safety
When calling this method, you have to ensure that all of the following is true:
-
The pointer must be properly aligned.
-
It must be “dereferenceable”.
-
The pointer must point to an initialized instance of
T. -
You must enforce Rust’s aliasing rules, since the returned lifetime
'ais arbitrarily chosen and does not necessarily reflect the actual lifetime of the data. In particular, while this reference exists, the memory the pointer points to must not get mutated (except insideUnsafeCell).
This applies even if the result of this method is unused! (The part about being initialized is not yet fully decided, but until it is, the only safe approach is to ensure that they are indeed initialized.)
§Examples
use constptr::ConstPtr;
let x = 0u32;
let ptr = ConstPtr::new(&x).expect("ptr is null!");
let ref_x = unsafe { ptr.as_ref() };
println!("{ref_x}");Trait Implementations§
Source§impl<T: ?Sized> From<&mut T> for ConstPtr<T>
impl<T: ?Sized> From<&mut T> for ConstPtr<T>
Source§fn from(reference: &mut T) -> Self
fn from(reference: &mut T) -> Self
Converts a &mut T to a ConstPtr<T>.
This conversion is safe and infallible since references cannot be null.
§Safety
Creating a ConstPtr from a mutable reference is safe. However, you must ensure that
the ConstPtr is not used while the mutable reference is still in use. The other
safety requirements for ConstPtr still apply. Eg the object must still be valid at
the time of dereferencing, even after the mutable reference is dropped.
Source§impl<T: ?Sized> Ord for ConstPtr<T>
impl<T: ?Sized> Ord for ConstPtr<T>
Source§impl<T: ?Sized> PartialOrd for ConstPtr<T>
impl<T: ?Sized> PartialOrd for ConstPtr<T>
impl<T: ?Sized> Copy for ConstPtr<T>
impl<T: ?Sized> Eq for ConstPtr<T>
impl<T: ?Sized + Send> Send for ConstPtr<T>
ConstPtr pointers can be Send because they are constant.
impl<T: ?Sized + Send + Sync> Sync for ConstPtr<T>
ConstPtr pointers can be Sync because they are constant.