ConstPtr

Struct ConstPtr 

Source
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>

Source

pub const unsafe fn new_unchecked(ptr: *const T) -> Self

Creates a new ConstPtr. This is a const fn.

§Safety

ptr must be non-null.

§Examples
use constptr::ConstPtr;

static x: u32 = 0u32;
let ptr = unsafe { ConstPtr::new_unchecked(&x) };
Source

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);
Source

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!();
}
Source

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.

Source

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);
Source

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 'a is 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 inside UnsafeCell).

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}");
Source

pub const fn cast<U>(self) -> ConstPtr<U>

Casts to a pointer of another type.

§Examples
use constptr::ConstPtr;

let  x = 0u32;
let ptr = ConstPtr::new(&x).expect("null pointer");

let casted_ptr = ptr.cast::<i8>();
let raw_ptr: *const i8 = casted_ptr.as_ptr();

Trait Implementations§

Source§

impl<T: ?Sized> Clone for ConstPtr<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: ?Sized> Debug for ConstPtr<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> From<&T> for ConstPtr<T>

Source§

fn from(reference: &T) -> Self

Converts a &T to a ConstPtr<T>.

This conversion is safe and infallible since references cannot be null.

Source§

impl<T: ?Sized> From<&mut T> for ConstPtr<T>

Source§

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> Hash for ConstPtr<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: ?Sized> Ord for ConstPtr<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: ?Sized> PartialEq for ConstPtr<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: ?Sized> PartialOrd for ConstPtr<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: ?Sized> Pointer for ConstPtr<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ?Sized> Copy for ConstPtr<T>

Source§

impl<T: ?Sized> Eq for ConstPtr<T>

Source§

impl<T: ?Sized + Send> Send for ConstPtr<T>

ConstPtr pointers can be Send because they are constant.

Source§

impl<T: ?Sized + Send + Sync> Sync for ConstPtr<T>

ConstPtr pointers can be Sync because they are constant.

Auto Trait Implementations§

§

impl<T> Freeze for ConstPtr<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for ConstPtr<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Unpin for ConstPtr<T>
where T: ?Sized,

§

impl<T> UnwindSafe for ConstPtr<T>
where T: RefUnwindSafe + ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.