Skip to main content

Value

Enum Value 

Source
#[non_exhaustive]
pub enum Value<'a> { Null, Bool(bool), Undefined, Simple(u8), Integer(i128), Float(f64), Bytes(Cow<'a, [u8]>), Text(Cow<'a, str>), Array(Vec<Value<'a>>), Map(BTreeMap<Value<'a>, Value<'a>>), Tag(u64, Box<Value<'a>>), }
Expand description

A dynamically-typed CBOR value.

#[non_exhaustive]: CBOR’s shapes are open, so a downstream match over every variant would need editing for each addition here.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Null

Major type 7, the null simple value.

§

Bool(bool)

Major type 7, true or false.

§

Undefined

Major type 7, simple value 23: undefined. Distinct from Value::Null, which says a value is absent.

§

Simple(u8)

Major type 7, a simple value with no meaning of its own. RFC 8949 Section 3.3 reserves these, so they are carried rather than rejected.

§

Integer(i128)

Major type 0 or 1: a non-negative or negative integer.

§

Float(f64)

Major type 7, a half-, single-, or double-precision float.

§

Bytes(Cow<'a, [u8]>)

Major type 2, a byte string.

§

Text(Cow<'a, str>)

Major type 3, a UTF-8 text string.

§

Array(Vec<Value<'a>>)

Major type 4, an array of values.

§

Map(BTreeMap<Value<'a>, Value<'a>>)

Major type 5, a map from values to values.

§

Tag(u64, Box<Value<'a>>)

Major type 6, a tagged value: the tag number and the value it applies to.

Implementations§

Source§

impl<'a> Value<'a>

Source

pub fn is_null(&self) -> bool

Returns true if the value is Null.

Source

pub fn is_bool(&self) -> bool

Returns true if the value is a boolean.

Source

pub fn is_integer(&self) -> bool

Returns true if the value is an integer.

Source

pub fn is_float(&self) -> bool

Returns true if the value is a float.

Source

pub fn is_bytes(&self) -> bool

Returns true if the value is a byte string.

Source

pub fn is_text(&self) -> bool

Returns true if the value is a text string.

Source

pub fn is_array(&self) -> bool

Returns true if the value is an array.

Source

pub fn is_map(&self) -> bool

Returns true if the value is a map.

Source

pub fn is_tag(&self) -> bool

Returns true if the value is a tag.

Source

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

Returns the value as a boolean if it is one.

Source

pub fn as_integer(&self) -> Option<i128>

Returns the value as an integer if it is one.

Source

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

Returns the value as a float if it is one.

Source

pub fn as_bytes(&self) -> Option<&[u8]>

Returns the value as a byte slice if it is a byte string.

Source

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

Returns the value as a string slice if it is a text string.

Source

pub fn as_array(&self) -> Option<&[Value<'a>]>

Returns the value as a slice if it is an array.

Source

pub fn as_map(&self) -> Option<&BTreeMap<Value<'a>, Value<'a>>>

Returns the value as a map if it is one.

Source

pub fn as_tag(&self) -> Option<(u64, &Value<'a>)>

Returns the tag value and inner value if it is a tag.

Trait Implementations§

Source§

impl<'a> Clone for Value<'a>

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<'a> Debug for Value<'a>

Source§

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

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

impl<'a> Eq for Value<'a>

Source§

impl<'a> FromCbor<'a> for Value<'a>

Source§

fn read<R: Read<'a>>(reader: &mut R) -> Result<Self>

Reads the CBOR representation of this value from the provided reader.
Source§

impl<'a> Ord for Value<'a>

Source§

fn cmp(&self, other: &Self) -> Ordering

The order RFC 8949 Section 4.2.1 defines for map keys: bytewise over their deterministic encodings.

A BTreeMap iterates in this order, so a map of values comes out deterministic with no sorting at write time. Heads go first, which settles major type and float width without walking contents; equal heads mean the same shape, since CBOR is self-delimiting, so the rest is content.

1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<'a> PartialEq for Value<'a>

Source§

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

A NaN equals itself here, unlike the f64 it rides in.

A CBOR NaN has no payload a decoder must keep: RFC 8949 Section 4.1 names one encoding and Section 4.2.1 requires it. Leaving the derived comparison would make Eq unsound and a BTreeMap<Value, _> would treat each NaN as a fresh key.

1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'a> PartialOrd for Value<'a>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> ToCbor for Value<'a>

Source§

fn write<W: Write>(&self, writer: &mut W) -> Result<()>

Writes the CBOR representation of this value into the provided writer.
Source§

fn size_hint(&self) -> Option<TrustedSizeHint>

Returns a trusted upper bound on the bytes the next Self::write will write, or None if it cannot be had cheaply. Read more
Source§

fn write_slice<W: Write>(values: &[Self], writer: &mut W) -> Result<()>
where Self: Sized,

Writes the CBOR representation of a slice of values into the provided writer.
Source§

fn max_size() -> Option<TrustedSizeHint>
where Self: Sized,

Returns a trusted upper bound valid for every value of this type, for O(1) hints on runtime-sized homogeneous containers. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Value<'a>

§

impl<'a> RefUnwindSafe for Value<'a>

§

impl<'a> Send for Value<'a>

§

impl<'a> Sync for Value<'a>

§

impl<'a> Unpin for Value<'a>

§

impl<'a> UnsafeUnpin for Value<'a>

§

impl<'a> UnwindSafe for Value<'a>

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> 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> 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.