Simple

Enum Simple 

Source
pub enum Simple {
    False,
    True,
    Null,
    Float(f64),
}
Expand description

Represents CBOR simple values (major type 7).

In CBOR, simple values are a special category that includes booleans (true and false), null, and floating point numbers.

Per Section 2.4 of the dCBOR specification, only these specific simple values are valid in dCBOR. All other major type 7 values (such as undefined or other simple values) are invalid and will be rejected by dCBOR decoders.

When encoding floating point values, dCBOR follows specific numeric reduction rules detailed in Section 2.3 of the dCBOR specification, including:

  • Integral floating point values must be reduced to integers when possible
  • NaN values must be normalized to the canonical form f97e00

§Note

This type is primarily an implementation detail. Users should generally use Rust’s native types instead:

  • Use Rust’s true and false booleans directly
  • Use the convenience methods CBOR::r#true(), CBOR::r#false(), and CBOR::null()
  • Use Rust’s floating point types like f64 directly

§Examples

use dcbor::prelude::*;

// Use Rust's native boolean and numeric types
let false_cbor = CBOR::from(false);
let true_cbor = CBOR::from(true);
let float_cbor = CBOR::from(3.14159);

// Using convenience methods for common values
let false_value = CBOR::r#false();
let true_value = CBOR::r#true();
let null_value = CBOR::null();

// Verify they produce the same encodings
assert_eq!(false_cbor, false_value);
assert_eq!(true_cbor, true_value);

Variants§

§

False

The boolean value false. Encoded as 0xf4 in CBOR, or 0x14 (20) with major type 7.

§

True

The boolean value true. Encoded as 0xf5 in CBOR, or 0x15 (21) with major type 7.

§

Null

The value representing null (None). Encoded as 0xf6 in CBOR, or 0x16 (22) with major type 7.

§

Float(f64)

A floating point value.

In dCBOR, floating point values follow these encoding rules:

  • Values are encoded in the shortest form that preserves precision
  • Integral floating point values are encoded as integers when in range
  • NaN values are normalized to f97e00

Implementations§

Source§

impl Simple

Source

pub fn name(&self) -> String

Returns the standard name of the simple value as a string.

For False, True, and Null, this returns their lowercase string representation. For Float values, it returns their numeric representation.

§Note

This method is primarily used internally. Users should generally interact with Rust’s native types rather than with Simple values directly.

Source

pub fn is_float(&self) -> bool

Checks if the simple value is a floating point number.

Source

pub fn is_nan(&self) -> bool

Checks if the simple value is the NaN (Not a Number) representation.

Source

pub fn cbor_data(&self) -> Vec<u8>

Encodes the simple value to its raw CBOR byte representation.

Returns the CBOR bytes that represent this simple value according to the dCBOR deterministic encoding rules:

  • False encodes as 0xf4
  • True encodes as 0xf5
  • Null encodes as 0xf6
  • Float values encode according to the IEEE 754 floating point rules, using the shortest representation that preserves precision.
§Note

This method is primarily used internally. For encoding simple values, users should use the to_cbor_data method on CBOR values created from Rust’s native types.

Trait Implementations§

Source§

impl Clone for Simple

Source§

fn clone(&self) -> Simple

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Simple

Implements debug formatting for Simple values.

This is used to generate string representations for debugging purposes. The format matches the standard string representations of these values:

  • false for Simple::False
  • true for Simple::True
  • null for Simple::Null
  • The debug representation of the float for Simple::Float

This implementation is used internally by the name method.

Source§

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

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

impl Display for Simple

Implements string display formatting for Simple values.

This is used when converting a Simple value to a string, such as with to_string(). The format matches the standard string representations:

  • false for Simple::False
  • true for Simple::True
  • null for Simple::Null
  • The debug representation of the float for Simple::Float
Source§

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

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

impl From<Simple> for CBOR

Converts a Simple value into a CBOR representation.

This conversion allows Simple values to be seamlessly used where CBOR values are expected, creating a CBOR value of type CBORCase::Simple that wraps the simple value.

§Note

This conversion is primarily used internally. Users should generally prefer converting from Rust’s native types (bool, f64) directly to CBOR instead of using the Simple type.

Source§

fn from(value: Simple) -> Self

Converts to this type from the input type.
Source§

impl Hash for Simple

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Simple

Implements equality comparison for Simple values.

Two Simple values are equal if they’re the same variant. For Float variants, the contained floating point values are compared for equality according to Rust’s floating point equality rules.

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<CBOR> for Simple

Attempts to convert a CBOR value to a Simple value.

If the CBOR value is a CBORCase::Simple, this conversion will succeed. For any other CBOR type, it will return a WrongType error.

§Note

This conversion is primarily used internally. Users should generally prefer converting CBOR values to Rust’s native types (bool, f64, etc.) instead of to the Simple type.

Source§

type Error = Error

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

fn try_from(cbor: CBOR) -> Result<Self>

Performs the conversion.
Source§

impl Eq for Simple

Auto Trait Implementations§

§

impl Freeze for Simple

§

impl RefUnwindSafe for Simple

§

impl Send for Simple

§

impl Sync for Simple

§

impl Unpin for Simple

§

impl UnwindSafe for Simple

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> CBORDecodable for T
where T: TryFrom<CBOR, Error = Error>,

Source§

fn try_from_cbor(cbor: &CBOR) -> Result<Self>

Source§

impl<T> CBOREncodable for T
where T: Into<CBOR> + Clone,

Source§

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object. Read more
Source§

fn to_cbor_data(&self) -> Vec<u8>

Converts this value directly to binary CBOR data. 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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<T> CBORCodable for T