Skip to main content

Flex

Struct Flex 

Source
pub struct Flex<T: ?Sized, B: Buffer> { /* private fields */ }
Expand description

Inline storage with heap fallback for ?Sized types.

Stores a trait object (or slice) inline when it fits, otherwise heap-allocates. Use is_inline to query which path was taken.

The total struct size equals size_of::<B>().

§Layout

The heap pointer slot doubles as the inline/heap discriminant (null = inline, non-null = heap address).

  • ?Sized T: [metadata(ptr)][heap_ptr(ptr)][value(B − 2*ptr)]
  • Sized T: [heap_ptr(ptr)][value(B − ptr)]

Use the flex! macro for ?Sized construction, or Flex::new for Sized types.

§Compile-time safety

Buffers too small for the overhead produce a compile error:

nexus_smartptr::define_buffer!(B8, 8);
trait Foo { fn foo(&self); }
struct Bar;
impl Foo for Bar { fn foo(&self) {} }
// B8 can't fit ?Sized overhead (metadata + heap pointer = 16 bytes).
let _: nexus_smartptr::Flex<dyn Foo, B8> = nexus_smartptr::flex!(Bar);

Implementations§

Source§

impl<T: ?Sized, B: Buffer> Flex<T, B>

Source

pub const fn capacity() -> usize

Returns the usable inline value capacity in bytes.

For Sized types: B::CAPACITY - 8 (heap pointer slot). For ?Sized types: B::CAPACITY - 16 (metadata + heap pointer).

Source

pub fn is_inline(&self) -> bool

Returns true if the value is stored inline (no heap allocation).

Source§

impl<T, B: Buffer> Flex<T, B>

Source

pub fn new(val: T) -> Self

Constructs a Flex from a Sized value.

Stores inline if the value fits, otherwise heap-allocates.

§Examples
use nexus_smartptr::{Flex, B32};

let f: Flex<u64, B32> = Flex::new(42);
assert!(f.is_inline());
assert_eq!(*f, 42);

Trait Implementations§

Source§

impl<T: ?Sized, B: Buffer> Deref for Flex<T, B>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T: ?Sized, B: Buffer> DerefMut for Flex<T, B>

Source§

fn deref_mut(&mut self) -> &mut T

Mutably dereferences the value.
Source§

impl<T: ?Sized, B: Buffer> Drop for Flex<T, B>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: ?Sized + Send, B: Buffer> Send for Flex<T, B>

Source§

impl<T: ?Sized + Sync, B: Buffer> Sync for Flex<T, B>

Auto Trait Implementations§

§

impl<T, B> Freeze for Flex<T, B>
where B: Freeze, T: ?Sized,

§

impl<T, B> RefUnwindSafe for Flex<T, B>

§

impl<T, B> Unpin for Flex<T, B>
where T: Unpin + ?Sized, B: Unpin,

§

impl<T, B> UnsafeUnpin for Flex<T, B>
where B: UnsafeUnpin, T: ?Sized,

§

impl<T, B> UnwindSafe for Flex<T, B>
where T: UnwindSafe + ?Sized, B: 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> 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, 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.