Trait cbor_data::Encoder

source ·
pub trait Encoder: Writer {
    // Provided methods
    fn encode_undefined(self) -> Self::Output { ... }
    fn encode_null(self) -> Self::Output { ... }
    fn encode_bool(self, value: bool) -> Self::Output { ... }
    fn encode_u64(self, value: u64) -> Self::Output { ... }
    fn encode_i64(self, value: i64) -> Self::Output { ... }
    fn encode_f64(self, value: f64) -> Self::Output { ... }
    fn encode_timestamp(
        self,
        timestamp: Timestamp,
        precision: Precision
    ) -> Self::Output { ... }
    fn encode_number(self, number: &Number<'_>) -> Self::Output { ... }
    fn encode_str(self, value: impl AsRef<str>) -> Self::Output { ... }
    fn encode_bytes(self, value: impl AsRef<[u8]>) -> Self::Output { ... }
    fn encode_array<F>(self, f: F) -> Self::Output
       where F: FnOnce(&mut ArrayWriter<'_>) { ... }
    fn encode_dict<F>(self, f: F) -> Self::Output
       where F: FnOnce(&mut DictWriter<'_>) { ... }
}
Expand description

High-level encoding functions to write values in their canonical format.

use cbor_data::{CborBuilder, Encoder, Writer};

let cbor = CborBuilder::default().encode_u64(12);

let array = CborBuilder::default().encode_array(|builder| {
    builder
        .encode_u64(18)
        .encode_i64(-12);
});

let array2 = CborBuilder::default().with_max_definite_size(Some(1)).write_array(None, |builder| {
    builder
        .encode_u64(18)
        .encode_i64(-12);
});

let dict = CborBuilder::default().encode_dict(|builder| {
    builder
        .with_key("a", |b| b.encode_u64(14))
        .with_key("b", |b| b.encode_i64(-1));
});

let (dict2, ret) = CborBuilder::default().write_dict_ret(None, |builder| {
    builder
        .with_key("a", |b| b.encode_u64(14))
        .with_key("b", |b| b.encode_i64(-1));
    "hello"
});
assert_eq!(ret, "hello");

Provided Methods§

source

fn encode_undefined(self) -> Self::Output

source

fn encode_null(self) -> Self::Output

source

fn encode_bool(self, value: bool) -> Self::Output

source

fn encode_u64(self, value: u64) -> Self::Output

Encode an unsigned integer of at most 64 bit.

Also to be used for smaller unsigned integers:

use cbor_data::{CborBuilder, Encoder};

let short = 12345u16;
let cbor = CborBuilder::default().encode_u64(short.into());
source

fn encode_i64(self, value: i64) -> Self::Output

Encode a signed integer of at most 64 bit.

Also to be used for smaller signed integers:

use cbor_data::{CborBuilder, Encoder};

let short = -12345i16;
let cbor = CborBuilder::default().encode_i64(short.into());
source

fn encode_f64(self, value: f64) -> Self::Output

Encode a floating-point number of at most 64 bit.

Also to be used for smaller formats:

use cbor_data::{CborBuilder, Encoder};

let single = -3.14f32;
let cbor = CborBuilder::default().encode_f64(single.into());
source

fn encode_timestamp( self, timestamp: Timestamp, precision: Precision ) -> Self::Output

Encode a timestamp with given target precision

Since the CBOR-encoding itself does not carry precision information, the result is not guaranteed to round-trip as the exact same timestamp. Encoding with Precision::Seconds will discard the nanos() part.

If the rfc3339 feature flag is enabled, textual representation is chosen for subsecond precision when encoding as a double-precision floating-point number would not be enough (float is sufficient for 285 years around 1970 at microsecond precision). Textual representation retains timezone information in the output.

source

fn encode_number(self, number: &Number<'_>) -> Self::Output

Encode a possibly big number

The number will be encoded as simple integer or float if its mantissa is small enough.

source

fn encode_str(self, value: impl AsRef<str>) -> Self::Output

Encode a string.

use cbor_data::{CborBuilder, Encoder};

let cbor = CborBuilder::default().encode_array(|builder| {
    builder.encode_str("hello");
    builder.encode_str(String::new());
});
source

fn encode_bytes(self, value: impl AsRef<[u8]>) -> Self::Output

Encode a byte string.

use cbor_data::{CborBuilder, Encoder};

let cbor = CborBuilder::default().encode_array(|builder| {
    builder.encode_bytes(b"hello");
});
source

fn encode_array<F>(self, f: F) -> Self::Output
where F: FnOnce(&mut ArrayWriter<'_>),

Write an array that is then filled by the provided closure using the passed builder.

see trait Encoder for usage examples

source

fn encode_dict<F>(self, f: F) -> Self::Output
where F: FnOnce(&mut DictWriter<'_>),

Write a dict that is then filled by the provided closure using the passed builder.

see trait Encoder for usage examples

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: Writer> Encoder for T