Trait CBOREncodable

Source
pub trait CBOREncodable: Into<CBOR> + Clone {
    // Provided methods
    fn to_cbor(&self) -> CBOR { ... }
    fn to_cbor_data(&self) -> Vec<u8>  { ... }
}
Expand description

§CBOR Encoding and Decoding Traits

These traits provide functionality for converting between Rust types and CBOR data. They form the foundation of the dCBOR serialization infrastructure.

The main traits are:

  • CBOREncodable: For types that can be encoded to CBOR
  • CBORDecodable: For types that can be decoded from CBOR
  • CBORCodable: For types that can do both (a combination of the above)

These traits allow for ergonomic conversions using Rust’s type system and enable seamless integration with dCBOR’s deterministic encoding rules. A trait for types that can be encoded to CBOR.

This trait is automatically implemented for any type that implements Into<CBOR> and Clone. It provides convenient methods for converting instances into CBOR objects and binary data.

§Example

use dcbor::prelude::*;

// Custom type that implements Into<CBOR>
#[derive(Clone)]
struct Person {
    name: String,
    age: u8,
}

// Implement conversion to CBOR
impl From<Person> for CBOR {
    fn from(person: Person) -> Self {
        let mut map = Map::new();
        map.insert("name", person.name);
        map.insert("age", person.age);
        map.into()
    }
}

// The CBOREncodable trait is automatically implemented
let person = Person { name: "Alice".to_string(), age: 30 };

// Convert to CBOR with to_cbor()
let cbor = person.to_cbor();

// Convert directly to binary CBOR data
let data = person.to_cbor_data();

Provided Methods§

Source

fn to_cbor(&self) -> CBOR

Converts this value to a CBOR object.

This is a convenience method that avoids consuming the original value.

Source

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

Converts this value directly to binary CBOR data.

This is a shorthand for self.to_cbor().to_cbor_data().

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

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