Struct RelPtr

Source
pub struct RelPtr<T: ?Sized + MetaData, I: Delta = isize>(/* private fields */);
Expand description

This represents a relative pointers

A relative pointer stores an offset, and uses its that in combination with its current position in memory to point to a value

See crate documentation for more information

§Safety

When using core::num::NonZero*, it is UB to have the RelPtr point to itself, this could be achieved with

If you use RelPtr::from(offset), then you must ensure that the relative pointer is set with the given functions to avoid UB

When using RelPtr with packed structs it is important to keep in mind that packed struct move fields to align them to drop them. For example, the below example is UB

 #[repr(packed)]
 struct Base(String, UnsafeThing);
 
 struct UnsafeThing(RelPtr<String>); // points into Base
 
 impl Drop for UnsafeThing {
     fn drop(&mut self) {
         // ... accessing `RelPtr<String>` here is UB
     }
 }

This is because when Base drops, all of the fields are moved to align them. So the offset between the String in unsafe thing and the RelPtr<String> in UnsafeThing could be changed. This will result in UB if you try to access String inside of UnsafeThing even if you enforce drop order!

Implementations§

Source§

impl<T: ?Sized + MetaData, I: Nullable> RelPtr<T, I>

Source

pub fn null() -> Self

A null relative pointer has an offset of 0, (points to itself)

Source

pub fn is_null(&self) -> bool

Check if relative pointer is null

Source§

impl<T: ?Sized + MetaData, I: Delta> RelPtr<T, I>

Source

pub fn set(&mut self, value: &mut T) -> Result<(), I::Error>

Set the offset of a relative pointer, if the offset cannot be calculated using the given Delta, then Err will be returned, and there will be no change to the offset

Source

pub unsafe fn set_unchecked(&mut self, value: *mut T)

Set the offset of a relative pointer,

§Safety

if the offset is out of bounds for the given Delta then it’s value is UB

if the given pointer is null, this is UB

Source

pub unsafe fn as_raw_unchecked(&mut self) -> *mut T

Converts the relative pointer into a normal raw pointer

§Safety

You must ensure that the relative pointer was successfully set before calling this function and that the value pointed to does not change it’s offset relative to RelPtr

if relative pointer was never set successfully, this function is UB

Source

pub unsafe fn as_non_null_unchecked(&mut self) -> NonNull<T>

Converts the relative pointer into a normal raw pointer

§Safety

Same as RelPtr::as_raw_unchecked

Source

pub unsafe fn as_ref_unchecked(&self) -> &T

Gets a reference from the relative pointer

§Safety

Same as RelPtr::as_raw_unchecked

Source

pub unsafe fn as_mut_unchecked(&mut self) -> &mut T

Gets a mutable reference from the relative pointer

§Safety

Same as RelPtr::as_raw_unchecked

Source§

impl<T: ?Sized + MetaData, I: Nullable> RelPtr<T, I>

Source

pub unsafe fn as_raw(&mut self) -> *mut T

Converts the relative pointer into a normal raw pointer

Note: if self.is_null() then a null pointer will be returned

§Safety

You must ensure that if the relative pointer was successfully set then the value pointed to does not change it’s offset relative to RelPtr

if the relative pointer was not successfully set RelPtr::as_raw returns null, this function is safe for all types where size_of::<*mut T>() == size_of::<usize>(), otherwise this function is UB

Source

pub unsafe fn as_non_null(&mut self) -> Ptr<T>

Converts the relative pointer into a normal raw pointer

§Safety

You must ensure that if the relative pointer was successfully set then the value pointed to does not change it’s offset relative to RelPtr

if the relative pointer was never successfully set RelPtr::as_non_null returns None,

Source

pub unsafe fn as_ref(&self) -> Option<&T>

Gets a reference from the relative pointer, if the relative pointer is null, then None is returned

§Safety

You are not allows to alias another mutable reference, as per the aliasing rules of references

Same as RelPtr::as_non_null

Source

pub unsafe fn as_mut(&mut self) -> Option<&mut T>

Gets a reference from the relative pointer, if the relative pointer is null, then None is returned

§Safety

You are not allows to alias this mutable reference, as per the aliasing rules of references

Same as RelPtr::as_non_null

Trait Implementations§

Source§

impl<T: ?Sized + MetaData, I: Delta> Clone for RelPtr<T, I>

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 + MetaData, I: Debug + Delta> Debug for RelPtr<T, I>

Source§

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

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

impl<T: ?Sized + MetaData, I: Delta> From<I> for RelPtr<T, I>

Convert an offset into a RelPtr

Source§

fn from(i: I) -> Self

Converts to this type from the input type.
Source§

impl<T: ?Sized + MetaData, I: Delta> PartialEq for RelPtr<T, I>

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 + MetaData, I: Debug + Delta> Pointer for RelPtr<T, I>

Source§

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

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

impl<T: ?Sized + MetaData, I: Delta> Copy for RelPtr<T, I>

Source§

impl<T: ?Sized + MetaData, I: Delta> Eq for RelPtr<T, I>

Auto Trait Implementations§

§

impl<T, I> Freeze for RelPtr<T, I>
where I: Freeze, <T as MetaData>::Data: Freeze, T: ?Sized,

§

impl<T, I> RefUnwindSafe for RelPtr<T, I>

§

impl<T, I = isize> !Send for RelPtr<T, I>

§

impl<T, I = isize> !Sync for RelPtr<T, I>

§

impl<T, I> Unpin for RelPtr<T, I>
where I: Unpin, <T as MetaData>::Data: Unpin, T: ?Sized,

§

impl<T, I> UnwindSafe for RelPtr<T, I>

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> MetaData for T

Source§

type Data = ()

the type of meta data a type carries
Source§

fn data(_: &T) -> <T as MetaData>::Data

decompose a type into a thin pointer and some metadata
Source§

unsafe fn compose( ptr: Option<NonNull<u8>>, _: <T as MetaData>::Data, ) -> Option<NonNull<T>>

recompose a type from a thin pointer and some metadata Read more
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.