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:
Dmust adhere to the safety requirements ofPackable.- 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>
impl<T, C: PtrCfg, D: Packable> TypedPackedPtr<T, C, D>
Sourcepub fn new(ptr: *const T, data: D, cfg: C) -> Result<Self, PackedPtrError>
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
PackedPtrError::UnalignedAddress: The pointer is not aligned.PackedPtrError::DataOverflow: The data will not fit in the pointer.PackedPtrError::UnsafeConfig: The pointer is not compatible with the configuration.
§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());Sourcepub unsafe fn new_unchecked(ptr: *const T, data: D) -> Self
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 typeT.data: The data to be packed into the pointer.
§Returns
A new instance of Self with the given ptr and data.