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:
| Offset | Field | Type |
|---|---|---|
| 0 | off | NonZero<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>
impl<T: Flat> Near<T>
Sourcepub fn get(&self) -> &T
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> Flat for Near<T>
impl<T: Flat> Flat for Near<T>
Source§unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)
unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)
self into the buffer at position at, patching all
self-relative pointers so they remain valid in the new location. Read more