Skip to main content

NanBox

Struct NanBox 

Source
pub struct NanBox(/* private fields */);
Expand description

A NaN-boxed value: 8 bytes encoding any VM value type.

Floats are stored as-is (their bit pattern). Non-float values are encoded in the NaN payload space.

Implementations§

Source§

impl NanBox

Source

pub const fn null() -> Self

Create a null value.

Source

pub const fn bool(b: bool) -> Self

Create a boolean value.

Source

pub fn int(n: i64) -> Self

Create an integer value preserving the full i64 range.

Values that fit in 48 bits (sign-extended) take the inline fast path — one tagged NaN, no allocation. Values outside that range are boxed as HeapObject::BigInt so the full i64 round-trips without precision loss.

This is the load-bearing contract for CppNix compatibility: i64::MAX - 1 must round-trip exactly, otherwise any flake containing large integer literals (timestamps, byte counts, builtins.bitAnd over wide masks) would diverge.

Source

pub fn float(f: f64) -> Self

Create a float value.

Source

pub fn heap(obj: HeapObject) -> Self

Create a pointer to a heap-allocated object.

Source

pub fn string(s: String) -> Self

Create a string value (heap-allocated).

Source

pub fn path(s: String) -> Self

Create a path value (heap-allocated).

Source

pub fn list(items: Vec<NanBox>) -> Self

Create a list value (heap-allocated).

Source

pub fn attrs(map: BTreeMap<Symbol, NanBox>) -> Self

Create an attrs value (heap-allocated).

Source

pub fn closure(c: VMClosure) -> Self

Create a closure value (heap-allocated).

Source

pub fn builtin(b: VMBuiltin) -> Self

Create a builtin function value (heap-allocated).

Source

pub fn thunk(t: VMThunk) -> Self

Create a thunk value (heap-allocated).

Source

pub fn higher_order_builtin(h: HigherOrderBuiltin) -> Self

Create a higher-order builtin value (heap-allocated).

Source

pub fn is_float(&self) -> bool

Check if this value is a float (not a tagged NaN).

Source

pub fn is_null(&self) -> bool

Source

pub fn is_bool(&self) -> bool

Source

pub fn is_int(&self) -> bool

Source

pub fn is_ptr(&self) -> bool

Source

pub fn as_bool(&self) -> Option<bool>

Extract a boolean. Returns None if not a bool.

Source

pub fn as_int(&self) -> Option<i64>

Extract an integer. Returns None if not an int.

Handles both the inline 48-bit fast path AND the heap-boxed BigInt fallback so the full i64 range round-trips.

Source

pub fn as_float(&self) -> Option<f64>

Extract a float. Returns None if this is a tagged value.

Source

pub fn as_heap(&self) -> Option<&HeapObject>

Extract the heap object. Returns None if not a pointer.

Source

pub fn type_name(&self) -> &'static str

Return the Nix type name for this value (mirrors VMValue::type_name).

Source

pub fn is_truthy(&self) -> Result<bool, VMError>

Check if this value is truthy (for conditionals). Only booleans are valid; everything else is a type error.

Source

pub fn is_string(&self) -> bool

Check if this is a string.

Source

pub fn is_path(&self) -> bool

Check if this is a path.

Source

pub fn is_list(&self) -> bool

Check if this is a list.

Source

pub fn is_attrs(&self) -> bool

Check if this is an attrset.

Source

pub fn is_closure(&self) -> bool

Check if this is a closure.

Source

pub fn is_builtin(&self) -> bool

Check if this is a builtin.

Source

pub fn is_thunk(&self) -> bool

Check if this is a thunk.

Source

pub fn is_higher_order_builtin(&self) -> bool

Check if this is a higher-order builtin.

Source

pub fn as_string(&self) -> Option<&str>

Extract a string reference. Returns None if not a string.

Source

pub fn as_path(&self) -> Option<&str>

Extract a path reference. Returns None if not a path.

Source

pub fn as_list(&self) -> Option<&[NanBox]>

Extract a list reference. Returns None if not a list.

Source

pub fn as_attrs(&self) -> Option<&BTreeMap<Symbol, NanBox>>

Extract an attrs reference. Returns None if not an attrset.

Source

pub fn as_closure(&self) -> Option<&VMClosure>

Extract a closure reference. Returns None if not a closure.

Source

pub fn as_builtin(&self) -> Option<&VMBuiltin>

Extract a builtin reference. Returns None if not a builtin.

Source

pub fn as_thunk(&self) -> Option<&VMThunk>

Extract a thunk reference. Returns None if not a thunk.

Source

pub fn as_higher_order_builtin(&self) -> Option<&HigherOrderBuiltin>

Extract a higher-order builtin reference.

Source

pub fn from_vmvalue(val: &VMValue) -> Self

Convert a VMValue to a NanBox.

Source

pub fn to_vmvalue(&self) -> VMValue

Convert a NanBox back to a VMValue.

Trait Implementations§

Source§

impl Clone for NanBox

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NanBox

Source§

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

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

impl Drop for NanBox

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for NanBox

Source§

impl PartialEq for NanBox

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

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

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.