Skip to main content

Value

Enum Value 

Source
pub enum Value {
    Null,
    Bit(bool),
    Number(f64),
    Text(String),
    Link(String),
}
Expand description

Value types supported by BEAM and Gun.js.

These are the five valid leaf types in the distributed graph. Each variant maps to a JSON type, with the exception of Value::Link which represents a Gun.js soul relation ({"#": "node_id"}).

§Wire Format

VariantJSON representationExample
NullnullValue::Null
Bittrue / falseValue::Bit(true)
NumbernumberValue::Number(42.0)
TextstringValue::Text("hello")
Link{"#": "soul"}Value::Link("node/abc")

§NaN / Infinity

Value::Number(f64) can technically hold NaN or Infinity, but these are not valid in the Gun.js wire format. When converting to JSON, NaN/Infinity will produce null (serde_json behavior). Callers should validate before constructing Value::Number from untrusted input.

Variants§

§

Null

Absence of a value (not the same as “deleted”).

§

Bit(bool)

Boolean flag.

§

Number(f64)

Floating-point number. Should be finite (not NaN/Infinity).

§

Text(String)

Unicode text string.

A soul relation — a reference to another node by ID.

Implementations§

Source§

impl Value

Source

pub fn size(&self) -> usize

Returns the approximate byte size of the value’s payload.

For Value::Text, this is the string’s byte length. For all other variants, it is the size_of_val of the enum discriminant + data.

This is used for memory budgeting.

Source

pub fn to_string(&self) -> String

Serializes the value to its Gun.js wire-format string.

Not a [Display] implementation — this produces the exact string representation used on the wire:

  • Null"null"
  • Bit(true)"true"
  • Bit(false)"false"
  • Number(42.0)"42"
  • Text("hello")"hello"
  • Link("node/abc")"node/abc" (the soul string, not JSON)

For JSON serialization with proper typing, use serde_json::to_string(&value) instead.

Source

pub fn is_null(&self) -> bool

Returns true if this value is Value::Null.

Source

pub fn is_bit(&self) -> bool

Returns true if this value is Value::Bit.

Source

pub fn is_number(&self) -> bool

Returns true if this value is Value::Number.

Source

pub fn is_text(&self) -> bool

Returns true if this value is Value::Text.

Returns true if this value is Value::Link.

Source

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

Returns Some(bool) if this value is Value::Bit, else None.

Source

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

Returns Some(f64) if this value is Value::Number, else None.

Source

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

Returns Some(&str) if this value is Value::Text, else None.

Returns Some(&str) if this value is Value::Link, else None.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

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 Value

Source§

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

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

impl<'de> Deserialize<'de> for Value

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&str> for Value

Source§

fn from(s: &str) -> Value

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Value

Converts to this type from the input type.
Source§

impl From<Value> for Value

Converts a BEAM Value into a serde_json::Value.

This is the inverse of [TryFrom<SerdeJsonValue> for Value]. The Value::Link variant serializes to a Gun.js soul relation object ({"#": "soul"}) so it round-trips correctly through JSON.

Source§

fn from(v: Value) -> SerdeJsonValue

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(n: f32) -> Value

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(n: u64) -> Value

Converts to this type from the input type.
Source§

impl From<usize> for Value

Source§

fn from(n: usize) -> Value

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl Serialize for Value

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Value

Source§

impl TryFrom<Value> for Value

Converts a serde_json::Value into a BEAM Value.

This conversion validates that the JSON value is one of the five supported types. Objects are accepted only if they are a Gun.js soul relation ({"#": "soul"}); all other objects are rejected.

§Errors

Returns &'static str if the JSON value cannot be represented as a BEAM Value:

  • Arrays → "cannot convert array into Value"
  • Non-soul objects → "cannot convert json object into Value"
  • Numbers not convertible to f64 → "not convertible to f64"

§Security Note

For production use, consider wrapping this in a custom error type rather than &'static str. The string error type limits programmatic error handling. This is a known limitation to address in a future API revision.

Source§

type Error = &'static str

The type returned in the event of a conversion error.
Source§

fn try_from(v: SerdeJsonValue) -> Result<Value, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V