CBOR

Struct CBOR 

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

A symbolic representation of CBOR data.

The CBOR type is the central type in the dCBOR library, representing any CBOR data item using a reference-counted wrapper around a CBORCase enum. This design allows efficient sharing of CBOR data structures in memory without excessive copying.

§Features

  • Deterministic encoding: Guarantees that semantically equivalent data structures will always be encoded to identical byte sequences
  • Reference counting: Enables efficient sharing of CBOR structures
  • Type safety: Uses Rust’s type system to safely handle different CBOR data types
  • Conversion traits: Implements Rust’s standard conversion traits for ergonomic use

§Thread Safety

With the multithreaded feature enabled, CBOR uses Arc for reference counting, making it thread-safe. Without this feature, it uses Rc, which is more efficient but not thread-safe.

§Example

use dcbor::prelude::*;

// 1. Create and round-trip a homogeneous array
let array = CBOR::from(vec![1, 2, 3]);

// Encode to bytes
let encoded = array.to_cbor_data();
assert_eq!(hex::encode(&encoded), "83010203");

// Decode from bytes
let decoded = CBOR::try_from_data(&encoded).unwrap();
assert_eq!(decoded, array);

// 2. Create and round-trip a heterogeneous array
let mixed_array: Vec<CBOR> =
    vec![1.into(), "Hello".into(), vec![1, 2, 3].into()];
let mixed = CBOR::from(mixed_array);

// Encode the heterogeneous array to bytes
let mixed_encoded = mixed.to_cbor_data();
assert_eq!(hex::encode(&mixed_encoded), "83016548656c6c6f83010203");

// Decode from bytes
let mixed_decoded = CBOR::try_from_data(&mixed_encoded).unwrap();
assert_eq!(mixed_decoded, mixed);
// Use diagnostic_flat() for a compact single-line representation
assert_eq!(
    mixed_decoded.diagnostic_flat(),
    r#"[1, "Hello", [1, 2, 3]]"#
);

Implementations§

Source§

impl CBOR

Source

pub fn as_case(&self) -> &CBORCase

Source

pub fn into_case(self) -> CBORCase

Source§

impl CBOR

Methods for decoding CBOR from binary representation and encoding to binary.

Source

pub fn try_from_data(data: impl AsRef<[u8]>) -> Result<CBOR>

Decodes binary data into CBOR symbolic representation.

This method parses the provided binary data according to the CBOR and dCBOR specifications, validating that it follows all deterministic encoding rules.

§Arguments
  • data - The binary data to decode, which can be any type that can be referenced as a byte slice (e.g., Vec<u8>, &[u8], etc.)
§Returns
  • Ok(CBOR) - A CBOR value if decoding was successful
  • Err - If the data is not valid CBOR or violates dCBOR encoding rules
§Examples
use dcbor::prelude::*;

// Decode a CBOR array [1, 2, 3]
let data = hex_literal::hex!("83010203");
let cbor = CBOR::try_from_data(&data).unwrap();

// Get the array contents
let array: Vec<u64> = cbor.try_into().unwrap();
assert_eq!(array, vec![1, 2, 3]);
§Errors

This method will return an error if:

  • The data is not valid CBOR
  • The data violates dCBOR encoding rules (e.g., non-canonical integer encoding)
  • The data has content after the end of the CBOR item
Source

pub fn try_from_hex(hex: &str) -> Result<CBOR>

Decodes a hexadecimal string into CBOR symbolic representation.

This is a convenience method that converts a hexadecimal string to binary data and then calls try_from_data.

§Arguments
  • hex - A string containing hexadecimal characters (no spaces or other characters)
§Returns
  • Ok(CBOR) - A CBOR value if decoding was successful
  • Err - If the hex string is invalid or the resulting data is not valid dCBOR
§Examples
use dcbor::prelude::*;

// Decode a CBOR array [1, 2, 3] from hex
let cbor = CBOR::try_from_hex("83010203").unwrap();
assert_eq!(cbor.diagnostic(), "[1, 2, 3]");
§Panics

This method will panic if the hex string is not well-formed hexadecimal (contains non-hex characters or an odd number of digits).

Source

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

Encodes this CBOR value to binary data following dCBOR encoding rules.

This method converts the CBOR value to a byte vector according to the dCBOR specification, ensuring deterministic encoding.

§Returns

A Vec<u8> containing the encoded CBOR data.

§Examples
use dcbor::prelude::*;

// Create a CBOR map
let mut map = Map::new();
map.insert(CBOR::from("key"), CBOR::from(123));
let cbor = CBOR::from(map);

// Encode to binary
let encoded = cbor.to_cbor_data();
assert_eq!(hex::encode(&encoded), "a1636b6579187b");

// Round-trip through encoding and decoding
let decoded = CBOR::try_from_data(&encoded).unwrap();
assert_eq!(decoded, cbor);
Source§

impl CBOR

Affordances for viewing CBOR in diagnostic notation.

Source

pub fn diagnostic_opt(&self, opts: &DiagFormatOpts<'_>) -> String

Returns a representation of this CBOR in diagnostic notation.

Optionally annotates the output, e.g. formatting dates and adding names of known tags.

Source

pub fn diagnostic(&self) -> String

Returns a representation of this CBOR in diagnostic notation.

Source

pub fn diagnostic_annotated(&self) -> String

Returns a representation of this CBOR in diagnostic notation, with annotations.

Source

pub fn diagnostic_flat(&self) -> String

Source

pub fn summary(&self) -> String

Source§

impl CBOR

Affordances for viewing the encoded binary representation of CBOR as hexadecimal.

Source

pub fn hex(&self) -> String

Returns the encoded hexadecimal representation of this CBOR.

Source

pub fn hex_opt(&self, opts: &HexFormatOpts<'_>) -> String

Returns the encoded hexadecimal representation of this CBOR.

Optionally annotates the output, e.g. breaking the output up into semantically meaningful lines, formatting dates, and adding names of known tags.

Source

pub fn hex_annotated(&self) -> String

Returns the encoded hexadecimal representation of this CBOR, with annotations.

Source§

impl CBOR

Functions for traversing and manipulating the CBOR hierarchy.

Source

pub fn walk<State: Clone>(&self, state: State, visit: &Visitor<'_, State>)

Walks the CBOR structure, calling the visitor function for each element.

This function traverses the entire CBOR hierarchy and calls the visitor function on each element. For maps, it first visits key-value pairs as semantic units, then visits keys and values individually.

The traversal includes:

  • Array elements (with their indices)
  • Map key-value pairs (as semantic units)
  • Map keys and values (individually, for all key-value pairs)
  • Tagged values (as semantic units, plus their content if nested)
  • All primitive values

The visitor function can optionally return a context value that is passed to child elements, enabling state to be accumulated or passed down during traversal.

§Type Parameters
  • State - The type of context passed between parent and child elements
§Arguments
  • state - The initial state to pass to the root visitor call
  • visit - The visitor function called for each element
§Examples
use std::cell::RefCell;

use dcbor::{
    prelude::*,
    walk::{EdgeType, Visitor, WalkElement},
};

// Create a CBOR map for key-value pattern matching
let mut map = Map::new();
map.insert("name", "Alice");
map.insert("age", 30);
let cbor = CBOR::from(map);

// Find specific key-value patterns
let matches = RefCell::new(Vec::new());
let visitor = |element: &WalkElement,
               _level: usize,
               _edge: EdgeType,
               state: ()|
 -> ((), bool) {
    if let Some((key, value)) = element.as_key_value() {
        if let (CBORCase::Text(k), CBORCase::Text(v)) =
            (key.as_case(), value.as_case())
        {
            if k == "name" {
                matches.borrow_mut().push(v.clone());
            }
        }
    }
    (state, false)
};

// Walk the CBOR structure
cbor.walk((), &visitor);
assert!(!matches.borrow().is_empty());
Source§

impl CBOR

Conveniences for byte strings.

Source

pub fn to_byte_string(data: impl AsRef<[u8]>) -> CBOR

Creates a new CBOR value representing a byte string.

This method creates a CBOR byte string from any type that can be referenced as a byte slice.

§Arguments
  • data - The bytes to include in the byte string, which can be any type that can be referenced as a byte slice (e.g., Vec<u8>, &[u8], etc.)
§Returns

A new CBOR value representing the byte string.

§Examples
use dcbor::prelude::*;

// Create a CBOR byte string from a byte slice
let bytes = vec![0x01, 0x02, 0x03];
let cbor = CBOR::to_byte_string(&bytes);

// Encode to CBOR binary
let encoded = cbor.to_cbor_data();
assert_eq!(hex::encode(&encoded), "43010203");

// Convert back to bytes
let recovered: Vec<u8> = cbor.try_into().unwrap();
assert_eq!(recovered, vec![0x01, 0x02, 0x03]);
Source

pub fn to_byte_string_from_hex(hex: impl AsRef<str>) -> CBOR

Creates a new CBOR value representing a byte string from a hexadecimal string.

This is a convenience method that converts a hexadecimal string to a byte array and then creates a CBOR byte string value.

§Arguments
  • hex - A string containing hexadecimal characters (no spaces or other characters)
§Returns

A new CBOR value representing the byte string.

§Examples
use dcbor::prelude::*;

// Create a CBOR byte string from a hex string
let cbor = CBOR::to_byte_string_from_hex("010203");

// Get the diagnostic representation
assert_eq!(cbor.diagnostic(), "h'010203'");
§Panics

This method will panic if the hex string is not well-formed hexadecimal (contains non-hex characters or an odd number of digits).

Source

pub fn try_into_byte_string(self) -> Result<Vec<u8>>

Extract the CBOR value as a byte string.

Returns Ok if the value is a byte string, Err otherwise.

Source

pub fn is_byte_string(&self) -> bool

Checks if a CBOR value is a byte string.

§Arguments
  • cbor - A reference to a CBOR value
§Returns
  • true if the value is a byte string
  • false otherwise
§Examples
use dcbor::prelude::*;

let bytes = CBOR::to_byte_string(vec![1, 2, 3]);
assert!(CBOR::is_byte_string(&bytes));

let text: CBOR = "hello".into();
assert!(!CBOR::is_byte_string(&text));
Source

pub fn into_byte_string(self) -> Option<Vec<u8>>

Attempts to convert the CBOR value into a byte string.

§Returns
  • Some(Vec<u8>) if the value is a byte string
  • None otherwise
§Examples
use dcbor::prelude::*;

// Create a CBOR byte string
let cbor = CBOR::to_byte_string(vec![1, 2, 3]);
assert_eq!(cbor.into_byte_string(), Some(vec![1, 2, 3]));

// This will return None since the value is a text string, not a byte string
let text: CBOR = "hello".into();
assert_eq!(text.into_byte_string(), None);
Source

pub fn try_byte_string(&self) -> Result<Vec<u8>>

Tries to convert a reference to a CBOR value into a byte string.

§Arguments
  • cbor - A reference to a CBOR value
§Returns
  • Ok(Vec<u8>) if the value is a byte string
  • Err(Error::WrongType) otherwise
§Examples
use dcbor::prelude::*;

let cbor = CBOR::to_byte_string(vec![1, 2, 3]);
let bytes = CBOR::try_byte_string(&cbor).unwrap();
assert_eq!(bytes, vec![1, 2, 3]);
Source

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

Source§

impl CBOR

Conveniences for tagged values.

Source

pub fn to_tagged_value(tag: impl Into<Tag>, item: impl Into<CBOR>) -> CBOR

Creates a new CBOR value representing a tagged value.

This method creates a CBOR tagged value by applying a tag to another CBOR value. Tags provide semantic information about how the tagged data should be interpreted.

§Arguments
  • tag - The tag to apply, which can be any type that can be converted to a Tag
  • item - The CBOR value to tag, which can be any type that can be converted to CBOR
§Returns

A new CBOR value representing the tagged value.

§Examples
use dcbor::prelude::*;

// Create a CBOR value with tag 42 applied to the string "hello"
let tagged = CBOR::to_tagged_value(42, "hello");

// Get the diagnostic representation
assert_eq!(tagged.diagnostic(), "42(\"hello\")");

// Extract the tag and the tagged value
let (tag, value) = tagged.try_into_tagged_value().unwrap();
assert_eq!(tag.value(), 42);
let s: String = value.try_into().unwrap();
assert_eq!(s, "hello");
Source

pub fn try_into_tagged_value(self) -> Result<(Tag, CBOR)>

Extract the CBOR value as a tagged value.

Returns Ok if the value is a tagged value, Err otherwise.

Source

pub fn is_tagged_value(&self) -> bool

Source

pub fn as_tagged_value(&self) -> Option<(&Tag, &CBOR)>

Source

pub fn try_tagged_value(&self) -> Result<(Tag, CBOR)>

Tries to convert a reference to a CBOR value into a tagged value.

§Arguments
  • cbor - A reference to a CBOR value
§Returns
  • Ok((Tag, CBOR)) - The tag and the tagged value if the CBOR value is a tagged value
  • Err(Error::WrongType) - If the value is not a tagged value
§Examples
use dcbor::prelude::*;

let tagged = CBOR::to_tagged_value(42, "hello");
let (tag, value) = CBOR::try_tagged_value(&tagged).unwrap();
assert_eq!(tag.value(), 42);
let s: String = value.try_into().unwrap();
assert_eq!(s, "hello");
Source

pub fn try_into_expected_tagged_value( self, expected_tag: impl Into<Tag>, ) -> Result<CBOR>

Extract the CBOR value as an expected tagged value.

Returns Ok if the value is a tagged value with the expected tag, Err otherwise.

Source

pub fn try_expected_tagged_value( &self, expected_tag: impl Into<Tag>, ) -> Result<CBOR>

Tries to extract a CBOR value that is tagged with an expected tag from a reference.

§Arguments
  • cbor - A reference to a CBOR value
  • expected_tag - The tag value that is expected
§Returns
  • Ok(CBOR) - The tagged value if the CBOR value is tagged with the expected tag
  • Err(Error::WrongType) - If the value is not a tagged value
  • Err(Error::WrongTag) - If the value is tagged but with a different tag
§Examples
use dcbor::prelude::*;

let tagged = CBOR::to_tagged_value(42, "hello");

// This will succeed because the tag matches
let value = CBOR::try_expected_tagged_value(&tagged, 42).unwrap();
let s: String = value.try_into().unwrap();
assert_eq!(s, "hello");

// This will fail because the tag doesn't match
let result = CBOR::try_expected_tagged_value(&tagged, 43);
assert!(result.is_err());
Source§

impl CBOR

Conveniences for text strings.

Source

pub fn try_into_text(self) -> Result<String>

Extract the CBOR value as a text string.

Returns Ok if the value is a text string, Err otherwise.

Source

pub fn is_text(&self) -> bool

Source

pub fn try_text(&self) -> Result<String>

Source

pub fn into_text(self) -> Option<String>

Source

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

Source§

impl CBOR

Conveniences for arrays.

Source

pub fn try_into_array(self) -> Result<Vec<CBOR>>

Extract the CBOR value as an array.

Returns Ok if the value is an array, Err otherwise.

Source

pub fn is_array(&self) -> bool

Source

pub fn try_array(&self) -> Result<Vec<CBOR>>

Source

pub fn into_array(self) -> Option<Vec<CBOR>>

Source

pub fn as_array(&self) -> Option<&[CBOR]>

Source§

impl CBOR

Conveniences for maps.

Source

pub fn try_into_map(self) -> Result<Map>

Extract the CBOR value as a map.

Returns Ok if the value is a map, Err otherwise.

Source

pub fn is_map(&self) -> bool

Source

pub fn try_map(&self) -> Result<Map>

Source

pub fn into_map(self) -> Option<Map>

Source

pub fn try_into_simple_value(self) -> Result<Simple>

Extract the CBOR value as a simple value.

Returns Ok if the value is a simple value, Err otherwise.

Source

pub fn as_map(&self) -> Option<&Map>

Source§

impl CBOR

Conveniences for booleans.

Source

pub fn false() -> Self

The CBOR simple value representing false.

Source

pub fn true() -> Self

The CBOR simple value representing true.

Source

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

Source

pub fn try_into_bool(self) -> Result<bool>

Extract the CBOR value as a boolean.

Source

pub fn is_bool(&self) -> bool

Source

pub fn try_bool(&self) -> Result<bool>

Source

pub fn is_true(&self) -> bool

Check if the CBOR value is true.

Source

pub fn is_false(&self) -> bool

Check if the CBOR value is false.

Source§

impl CBOR

Conveniences for simple values.

Source

pub fn null() -> Self

Creates a CBOR value representing null.

This is equivalent to the CBOR simple value null.

§Returns

A CBOR value representing null.

§Examples
use dcbor::prelude::*;

let null_value = CBOR::null();
assert!(null_value.is_null());
assert_eq!(null_value.diagnostic(), "null");
Source

pub fn is_null(&self) -> bool

Checks if the CBOR value is null.

§Returns
  • true if the value is the CBOR simple value null
  • false otherwise
§Examples
use dcbor::prelude::*;

let null_value = CBOR::null();
assert!(null_value.is_null());

let other_value: CBOR = 42u64.into();
assert!(!other_value.is_null());
Source§

impl CBOR

Conveniences for numeric values.

Source

pub fn is_number(&self) -> bool

Checks if the CBOR value represents a number.

A CBOR value is considered to be a number if it is:

  • An unsigned integer (major type 0)
  • A negative integer (major type 1)
  • A floating-point value (major type 7, simple values 25, 26, or 27)
§Returns
  • true if the value represents a number
  • false otherwise
§Examples
use dcbor::{CBORCase, Simple, prelude::*};

let unsigned: CBOR = 42u64.into();
assert!(unsigned.is_number());

let negative: CBOR = (-42i64).into();
assert!(negative.is_number());

let float: CBOR = CBORCase::Simple(Simple::Float(3.14)).into();
assert!(float.is_number());

let text: CBOR = "not a number".into();
assert!(!text.is_number());
Source

pub fn is_nan(&self) -> bool

Checks if the CBOR value represents the NaN (Not a Number) value.

§Returns
  • true if the value is the CBOR representation of NaN
  • false otherwise
§Examples
use dcbor::{CBORCase, Simple, prelude::*};

let nan_value = CBOR::nan();
assert!(nan_value.is_nan());

let float: CBOR = CBORCase::Simple(Simple::Float(3.14)).into();
assert!(!float.is_nan());
Source

pub fn nan() -> Self

Creates a CBOR value representing NaN (Not a Number).

§Returns

A CBOR value representing NaN.

§Examples
use dcbor::prelude::*;

let nan_value = CBOR::nan();
assert!(nan_value.is_nan());

Trait Implementations§

Source§

impl Clone for CBOR

Source§

fn clone(&self) -> CBOR

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 CBOR

Source§

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

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

impl Display for CBOR

Source§

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

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

impl<T> From<&[T]> for CBOR
where T: Into<CBOR> + Clone,

Source§

fn from(array: &[T]) -> Self

Converts to this type from the input type.
Source§

impl From<&Map> for CBOR

Source§

fn from(value: &Map) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for CBOR

§Text Strings in dCBOR

dCBOR encodes text strings according to strict deterministic rules:

  • All strings must be in Unicode Normalization Form C (NFC)
  • This ensures that semantically equivalent strings (like composed vs decomposed characters) are always encoded identically

dCBOR provides conversions for both String and &str types through the Into<CBOR> trait, making it easy to create CBOR values from Rust strings.

§Examples
use dcbor::prelude::*;

// Create CBOR from string literals
let cbor_str: CBOR = "Hello, world!".into();

// Create CBOR from owned String
let string = String::from("Hello, dCBOR!");
let cbor_string: CBOR = string.into();

// Use in collections
let mut map = Map::new();
map.insert("key", "value");
map.insert("array", [1, 2, 3]);

// Convert back to String
let value: String = cbor_str.try_into().unwrap();
assert_eq!(value, "Hello, world!");
§Unicode Normalization

dCBOR automatically ensures strings are in NFC form, which guarantees consistent encoding across platforms. This is important for applications that rely on deterministic encoding for hashing or signing operations.

For example, characters like “é” can be represented as either a single code point (U+00E9, NFC) or as “e” followed by the combining acute accent (U+0065 U+0301, NFD). dCBOR ensures these are always encoded consistently in NFC form.

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl<T, const N: usize> From<[T; N]> for CBOR
where T: Into<CBOR>,

Source§

fn from(array: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<BTreeMap<K, V>> for CBOR
where K: Into<CBOR>, V: Into<CBOR>,

Source§

fn from(container: BTreeMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl From<ByteString> for CBOR

Converts a ByteString into a CBOR byte string value.

This conversion creates a CBOR item of major type 2 (byte string) containing the byte string’s data.

§Examples

use dcbor::prelude::*;

let bytes = ByteString::new([1, 2, 3, 4]);
let cbor = CBOR::from(bytes);

// The result is a CBOR byte string
assert_eq!(cbor.diagnostic(), "h'01020304'");

// The encoding follows dCBOR rules
assert_eq!(cbor.to_cbor_data(), vec![0x44, 0x01, 0x02, 0x03, 0x04]);
//           0x44: byte string (major type 2) of length 4
Source§

fn from(value: ByteString) -> Self

Converts to this type from the input type.
Source§

impl From<CBORCase> for CBOR

Source§

fn from(case: CBORCase) -> Self

Converts to this type from the input type.
Source§

impl From<Date> for CBOR

Source§

fn from(value: Date) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<HashMap<K, V>> for CBOR
where K: Into<CBOR>, V: Into<CBOR>,

Source§

fn from(container: HashMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<HashSet<T>> for CBOR
where T: Into<CBOR>,

Source§

fn from(set: HashSet<T>) -> Self

Converts to this type from the input type.
Source§

impl From<Map> for CBOR

Source§

fn from(value: Map) -> Self

Converts to this type from the input type.
Source§

impl From<Set> for CBOR

Source§

fn from(value: Set) -> Self

Converts to this type from the input type.
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 From<String> for CBOR

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for CBOR
where T: Into<CBOR>,

§Array Support in dCBOR

dCBOR provides convenient conversions to and from CBOR arrays through implementation of the From<T> and TryFrom<CBOR> traits for various collection types. This enables idiomatic conversions using Rust’s .into() method.

Supported collection types:

  • Rust arrays ([T; N])
  • Vectors (Vec<T>)
  • VecDeque (VecDeque<T>)
  • HashSet (HashSet<T>)

For all of these, the elements must be convertible to CBOR via the Into<CBOR> trait.

§Examples
use dcbor::prelude::*;

// Create CBOR from Rust array (fixed size)
let array_cbor: CBOR = [1, 2, 3].into();

// Create CBOR from Vec
let vec = vec![1, 2, 3];
let vec_cbor: CBOR = vec.into();

// Mixed types in Vec
let mixed_vec: Vec<CBOR> = vec![
    1.into(),         // Integer
    "hello".into(),   // String
    [1, 2, 3].into(), // Nested array
    true.into(),      // Boolean
];
let mixed_cbor: CBOR = mixed_vec.into();

// Convert back to Vec
let numbers: Vec<i32> = array_cbor.try_into().unwrap();
assert_eq!(numbers, vec![1, 2, 3]);
Source§

fn from(vec: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<VecDeque<T>> for CBOR
where T: Into<CBOR>,

Source§

fn from(deque: VecDeque<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for CBOR

§Boolean Values in dCBOR

dCBOR supports boolean values through the major type 7 (simple values), where false is encoded as 0xf4 and true as 0xf5.

Per the dCBOR specification, only a limited set of simple values are valid:

  • Boolean values (true, false)
  • null
  • Floating point values
§Examples
use dcbor::prelude::*;

// Create CBOR from boolean values using `into()`
let cbor_true: CBOR = true.into();
let cbor_false: CBOR = false.into();

// Use in collections
let array: Vec<CBOR> = vec![true.into(), false.into(), 42.into()];
let cbor_array: CBOR = array.into();

// Maps can use boolean keys
let mut map = Map::new();
map.insert(true, "this is true");
map.insert(false, "this is false");

// Convert back to boolean
let value: bool = cbor_true.try_into().unwrap();
assert_eq!(value, true);
Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f16> for CBOR

Source§

fn from(value: f16) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for CBOR

Source§

fn from(value: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for CBOR

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for CBOR

Source§

fn from(value: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for CBOR

Source§

fn from(value: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for CBOR

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for CBOR

Source§

fn from(value: i8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for CBOR

Source§

fn from(value: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for CBOR

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for CBOR

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for CBOR

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for CBOR

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl Hash for CBOR

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 CBOR

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<K, V> TryFrom<CBOR> for BTreeMap<K, V>
where K: TryFrom<CBOR, Error = Error> + Eq + Ord + Clone, V: TryFrom<CBOR, Error = Error> + Clone,

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 TryFrom<CBOR> for ByteString

Attempts to convert a CBOR value into a ByteString.

This conversion succeeds if the CBOR value is a byte string (major type 2), otherwise it returns a WrongType error.

§Examples

use dcbor::prelude::*;

// Converting from a CBOR byte string works
let cbor = CBOR::to_byte_string([1, 2, 3, 4]);
let bytes: ByteString = cbor.try_into().unwrap();
assert_eq!(bytes.data(), &[1, 2, 3, 4]);

// Converting from a different CBOR type fails
let cbor = CBOR::from(42);
let result: dcbor::Result<ByteString> = cbor.try_into();
assert!(result.is_err());
Source§

type Error = Error

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

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

Performs the conversion.
Source§

impl TryFrom<CBOR> for Date

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 TryFrom<CBOR> for HashMap<CBOR, CBOR>

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<K, V> TryFrom<CBOR> for HashMap<K, V>
where K: TryFrom<CBOR, Error = Error> + Eq + Hash + Clone, V: TryFrom<CBOR, Error = Error> + Clone,

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<T> TryFrom<CBOR> for HashSet<T>
where T: TryFrom<CBOR, Error = Error> + Eq + Hash + Clone,

Source§

type Error = Error

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

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

Performs the conversion.
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 TryFrom<CBOR> for String

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

Source§

type Error = Error

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

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

Performs the conversion.
Source§

impl<T> TryFrom<CBOR> for VecDeque<T>
where T: TryFrom<CBOR, Error = Error> + Clone,

Source§

type Error = Error

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

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

Performs the conversion.
Source§

impl TryFrom<CBOR> for bool

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 TryFrom<CBOR> for f16

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 TryFrom<CBOR> for f32

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 TryFrom<CBOR> for f64

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 TryFrom<CBOR> for i16

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 TryFrom<CBOR> for i32

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 TryFrom<CBOR> for i64

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 TryFrom<CBOR> for i8

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 TryFrom<CBOR> for u16

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 TryFrom<CBOR> for u32

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 TryFrom<CBOR> for u64

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 TryFrom<CBOR> for u8

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 TryFrom<CBOR> for usize

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 CBOR

Auto Trait Implementations§

§

impl Freeze for CBOR

§

impl RefUnwindSafe for CBOR

§

impl !Send for CBOR

§

impl !Sync for CBOR

§

impl Unpin for CBOR

§

impl UnwindSafe for CBOR

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