pub struct SelfRef<T: 'static> { /* private fields */ }Expand description
A decoded value T that may borrow from its own backing storage.
Transports decode into storage they own (heap buffer, VarSlot, mmap).
SelfRef keeps that storage alive so T can safely borrow from it.
Uses ManuallyDrop + custom Drop to guarantee drop order: value is
dropped before backing, so borrowed references in T remain valid
through T’s drop.
Implementations§
Source§impl<T: 'static> SelfRef<T>
impl<T: 'static> SelfRef<T>
Sourcepub fn try_new<E>(
backing: Backing,
builder: impl FnOnce(&'static [u8]) -> Result<T, E>,
) -> Result<Self, E>
pub fn try_new<E>( backing: Backing, builder: impl FnOnce(&'static [u8]) -> Result<T, E>, ) -> Result<Self, E>
Construct a SelfRef from backing storage and a builder.
The builder receives a &'static [u8] view of the backing bytes —
sound because the backing is heap-allocated (stable address) and
dropped after the value.
Sourcepub fn new(backing: Backing, builder: impl FnOnce(&'static [u8]) -> T) -> Self
pub fn new(backing: Backing, builder: impl FnOnce(&'static [u8]) -> T) -> Self
Infallible variant of try_new.
Sourcepub fn owning(backing: Backing, value: T) -> Self
pub fn owning(backing: Backing, value: T) -> Self
Wrap an owned value that does NOT borrow from backing.
No variance check — the value is fully owned. The backing is kept alive but the value doesn’t reference it. Useful for in-memory transports (MemoryLink) where no deserialization occurs.
Sourcepub fn try_repack<U: 'static, E>(
self,
f: impl FnOnce(T, &'static [u8]) -> Result<U, E>,
) -> Result<SelfRef<U>, E>
pub fn try_repack<U: 'static, E>( self, f: impl FnOnce(T, &'static [u8]) -> Result<U, E>, ) -> Result<SelfRef<U>, E>
Transform the contained value, keeping the same backing storage.
Useful for projecting through wrapper types:
SelfRef<Frame<T>> → SelfRef<T> by extracting the inner item.
The closure receives the old value by move and returns the new value.
Any references the new value holds into the backing storage (inherited
from fields of T) remain valid — the backing is preserved.
Like try_map, but the closure also receives a &'static [u8]
view of the backing bytes, so the new value U can borrow from them.