pub struct TypedPackedPtr<T, C: PtrCfg, D: Packable>(/* private fields */);
Expand description

A PackedPtr with a type-safe packed data value.

A TypedPackedPtr will always be the same size as a raw pointer, and can contain a platform-specific amount of additional data. The amount of additional data is platform-specific, and may be zero.

D must implement Packable, which allows it to be packed into the pointer. Packable an unsafe trait & requires Copy. See Packable for more information about its safety requirements.

Because Packable is Copy, TypedPackedPtr is Copy as well.

§Safety

A TypedPackedPtr is still a pointer, and has the same safety requirements as a raw pointer. This struct does not enforce any guarantees about the validity or ownership of the pointed-to data. It is the responsibility of the user to ensure that the underlying data remains valid and accessible for the lifetime of this pointer.

Additionally, TypedPackedPtr has the same safety requirements as PackedPtr:

  • The pointer must be aligned to the alignment of T.
  • The packed data must be less than 2^Self::BITS.

And also has its own safety requirements:

  • D must adhere to the safety requirements of Packable.
  • Data packed into the pointer must be created from Packable::pack.
  • Data unpacked from the pointer must be retrieved via Packable::unpack.

Implementations§

source§

impl<T, C: PtrCfg, D: Packable> TypedPackedPtr<T, C, D>

source

pub fn new(ptr: *const T, data: D, cfg: C) -> Result<Self, PackedPtrError>

Create a new TypedPackedPtr with the given pointer and data.

§Arguments
  • ptr - A raw pointer to the underlying data.
  • data - The data to be packed into the pointer.
§Returns

Returns a Result containing either the new TypedPackedPtr instance or an error if the data overflows the available bits for packing or the pointer is unaligned.

§Errors
§Examples
use packed_ptr::TypedPackedPtr;
use packed_ptr::config::AlignOnly;

let data = 0xdeadbeefu32;
let packed = [true, false];
let ptr = TypedPackedPtr::new(&data, packed, AlignOnly).unwrap();
assert_eq!(data, unsafe { *ptr.ptr() });
assert_eq!(packed, ptr.data());
source

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

Creates a new instance of Self without performing any safety checks.

§Safety

This function is unsafe because the caller assumes the responsibility of ensuring that the provided ptr and data are valid and that they are compatible with the configuration.

If the provided ptr is a unaligned, or if the data is too big to fit in the pointer, undefined behavior may occur.

If the ptr is incompatible with the configuration, then the ptr may be corrupted, resulting in UB.

§Arguments
  • ptr: A raw pointer to the data of type T.
  • data: The data to be packed into the pointer.
§Returns

A new instance of Self with the given ptr and data.

source

pub fn ptr(self) -> *const T

Returns the raw pointer value of the Packed Pointer.

source

pub fn data(self) -> D

Returns the packed data value of the Packed Pointer.

source

pub fn get(self) -> (*const T, D)

Returns a tuple containing the raw pointer value and the packed data value.

source

pub fn set_data(&mut self, data: D)

Sets the packed data value of the pointer.

§Arguments
  • data: The data to pack into the pointer.

Trait Implementations§

source§

impl<T, C: PtrCfg, D: Packable> Clone for TypedPackedPtr<T, C, D>

source§

fn clone(&self) -> Self

Returns a copy 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, C: PtrCfg, D: Packable + Debug> Debug for TypedPackedPtr<T, C, D>

source§

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

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

impl<T, C: PtrCfg, D: Packable> From<TypedPackedPtr<T, C, D>> for *const T

source§

fn from(value: TypedPackedPtr<T, C, D>) -> Self

Converts to this type from the input type.
source§

impl<T, C: PtrCfg, D: Packable + Hash> Hash for TypedPackedPtr<T, C, D>

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, C: PtrCfg, D: Packable + PartialEq> PartialEq for TypedPackedPtr<T, C, D>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T, C: PtrCfg, D: Packable> Pointer for TypedPackedPtr<T, C, D>

source§

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

Formats the value using the given formatter.
source§

impl<T, C: PtrCfg, D: Packable> TryFrom<(*const T, D)> for TypedPackedPtr<T, C, D>

§

type Error = PackedPtrError

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

fn try_from(value: (*const T, D)) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T, C: PtrCfg, D: Packable + Default> TryFrom<*const T> for TypedPackedPtr<T, C, D>

§

type Error = PackedPtrError

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

fn try_from(value: *const T) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<T, C: PtrCfg, D: Packable> Copy for TypedPackedPtr<T, C, D>

source§

impl<T, C: PtrCfg, D: Packable + Eq> Eq for TypedPackedPtr<T, C, D>

Auto Trait Implementations§

§

impl<T, C, D> RefUnwindSafe for TypedPackedPtr<T, C, D>

§

impl<T, C, D> !Send for TypedPackedPtr<T, C, D>

§

impl<T, C, D> !Sync for TypedPackedPtr<T, C, D>

§

impl<T, C, D> Unpin for TypedPackedPtr<T, C, D>
where C: Unpin, D: Unpin,

§

impl<T, C, D> UnwindSafe for TypedPackedPtr<T, C, D>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.