Skip to main content

Near

Struct Near 

Source
pub struct Near<T> { /* private fields */ }
Expand description

A self-relative pointer stored as a 4-byte NonZero<i32> offset.

§Layout

#[repr(C)] — 4 bytes, align_of::<i32>() alignment:

OffsetFieldType
0offNonZero<i32>

The offset is relative to the address of off itself (not the start of the region). Non-zero guarantee ensures Near<T> never points to itself.

Near<T> is not Copy or Clone — moving it would invalidate the self-relative offset. Use it only inside Region buffers built by the emitter.

§Soundness

Provenance recovery: get computes the target address by adding the stored i32 offset to the address of the offset field itself. Because &self.off has provenance over only 4 bytes (insufficient for the target T), the method uses with_exposed_provenance to recover the full allocation’s provenance — previously exposed by AlignedBuf::grow. Miri validates this pattern with no UB detected.

Non-zero invariant: The offset is NonZero<i32>, so a Near<T> can never point to itself (offset 0). This is enforced by the emitter’s patch_near method which panics on target == at.

Implementations§

Source§

impl<T: Flat> Near<T>

Source

pub fn get(&self) -> &T

Resolve the self-relative offset to a reference.

§Examples
use nearest::{Flat, Near, Region, near};

#[derive(Flat, Debug)]
struct Wrapper { inner: Near<u32> }

let region = Region::new(Wrapper::make(near(42u32)));
assert_eq!(*region.inner.get(), 42);

// Deref also works (Near<T> implements Deref<Target = T>).
assert_eq!(*region.inner, 42);

Trait Implementations§

Source§

impl<T: Flat + Debug> Debug for Near<T>

Source§

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

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

impl<T: Flat> Deref for Near<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: Flat + Display> Display for Near<T>

Source§

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

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

impl<T: Flat> Flat for Near<T>

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Deep-copy self into the buffer at position at, patching all self-relative pointers so they remain valid in the new location. Read more
Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Validate that buf[addr..] contains a valid representation of Self. Read more
Source§

fn validate_option(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Validate an Option<Self> at addr in buf. Read more
Source§

impl<T: Flat + PartialEq> PartialEq for Near<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: Flat + Eq> Eq for Near<T>

Auto Trait Implementations§

§

impl<T> Freeze for Near<T>

§

impl<T> RefUnwindSafe for Near<T>
where T: RefUnwindSafe,

§

impl<T> Send for Near<T>
where T: Send,

§

impl<T> Sync for Near<T>
where T: Sync,

§

impl<T> Unpin for Near<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Near<T>

§

impl<T> UnwindSafe for Near<T>
where T: UnwindSafe,

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> Emit<T> for T
where T: Flat,

Source§

unsafe fn write_at(self, p: &mut impl Patch, at: Pos)

Write this builder’s data at position at. Read more
Source§

fn emit(self, p: &mut impl Patch) -> Pos
where T: Flat,

Reserve space for T, write this builder’s data, and return the position.
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.