Skip to main content

SequenceWriter

Struct SequenceWriter 

Source
pub struct SequenceWriter<W: Write> { /* private fields */ }
Expand description

Streaming writer for CBOR sequences in binary, hex, or diagnostic notation.

Construct with SequenceWriter::new, call write_item, write_items, or write_pairs to emit content, and then into_inner to get the wrapped writer back.

Format semantics mirror the reader side:

  • Format::Binary and Format::Hex: items are concatenated with no separator. Each item’s bytes form a self-delimiting CBOR value.
  • Format::Diagnostic: items are separated by , (comma and a space). The first item is written without a leading separator; no trailing comma is emitted.

The writer is format-agnostic to the caller: the same code path works for all three formats, and switching format is a single constructor argument.

§Examples

Binary sequence:

use cbor_core::{Format, SequenceWriter, Value};

let mut buf = Vec::new();
let mut sw = SequenceWriter::new(&mut buf, Format::Binary);
sw.write_item(&Value::from(1)).unwrap();
sw.write_item(&Value::from(2)).unwrap();
sw.write_item(&Value::from(3)).unwrap();
assert_eq!(buf, [0x01, 0x02, 0x03]);

Diagnostic sequence, with separators inserted automatically:

use cbor_core::{Format, SequenceWriter, Value};

let mut buf = Vec::new();
let mut sw = SequenceWriter::new(&mut buf, Format::Diagnostic);
sw.write_items([Value::from(1), Value::from("hi"), Value::from(true)].iter()).unwrap();
assert_eq!(String::from_utf8(buf).unwrap(), r#"1, "hi", true"#);

Round-trip through SequenceDecoder:

use cbor_core::{Array, DecodeOptions, Format, SequenceWriter, Value};

let items = [Value::from(1), Value::from("hi"), Value::from(true)];

let mut buf = Vec::new();
let mut sw = SequenceWriter::new(&mut buf, Format::Diagnostic);
sw.write_items(items.iter()).unwrap();

let decoded = Array::try_from_sequence(
    DecodeOptions::new().format(Format::Diagnostic).sequence_decoder(&buf),
).unwrap();
assert_eq!(decoded.get_ref().as_slice(), &items);

Implementations§

Source§

impl<W: Write> SequenceWriter<W>

Source

pub const fn new(writer: W, format: Format) -> Self

Create a new sequence writer wrapping writer and emitting items in the selected format.

use cbor_core::{Format, SequenceWriter};

let sw = SequenceWriter::new(Vec::new(), Format::Hex);
// `sw` now accepts items via `write_item` / `write_items` / `write_pairs`.
Source

pub fn write_item(&mut self, value: &Value) -> Result<()>

Write one item of the sequence.

In Format::Diagnostic a , separator is inserted before every item except the first. In Format::Binary and Format::Hex items are written back-to-back with no separator.

use cbor_core::{Format, SequenceWriter, Value};

let mut buf = Vec::new();
let mut sw = SequenceWriter::new(&mut buf, Format::Hex);
sw.write_item(&Value::from(1)).unwrap();
sw.write_item(&Value::from(2)).unwrap();
assert_eq!(buf, b"0102");
Source

pub fn write_items<'a, I>(&mut self, items: I) -> Result<()>
where I: IntoIterator<Item = &'a Value>,

Write every item produced by items. Equivalent to calling write_item in a loop, but keeps the call site concise.

use cbor_core::{Format, SequenceWriter, Value};

let items = [Value::from(1), Value::from(2), Value::from(3)];
let mut buf = Vec::new();
SequenceWriter::new(&mut buf, Format::Binary)
    .write_items(items.iter())
    .unwrap();
assert_eq!(buf, [0x01, 0x02, 0x03]);
Source

pub fn write_pairs<'a, I>(&mut self, pairs: I) -> Result<()>
where I: IntoIterator<Item = (&'a Value, &'a Value)>,

Write each key/value pair as two consecutive items of the sequence. Useful for emitting the contents of a map as a flat CBOR sequence, mirroring Map::from_sequence on the read side.

The iterator yields borrowed references, which is the natural shape of &BTreeMap<Value, Value>::iter(), so a map held in a Value can be streamed directly:

use cbor_core::{Format, SequenceWriter, Value, map};

let value = map! { "a" => 1, "b" => 2 };
let mut sw = SequenceWriter::new(Vec::new(), Format::Diagnostic);
sw.write_pairs(value.as_map().unwrap()).unwrap();
assert_eq!(sw.into_inner(), br#""a", 1, "b", 2"#);
Source

pub const fn get_ref(&self) -> &W

Borrow the wrapped writer.

Source

pub const fn get_mut(&mut self) -> &mut W

Mutably borrow the wrapped writer. Direct writes bypass the sequence writer’s separator bookkeeping, so use this only for format-specific ornamentation (for example, injecting a comment into diagnostic output) between full items.

Source

pub fn into_inner(self) -> W

Consume the sequence writer and return the wrapped writer.

use cbor_core::{Format, SequenceWriter, Value};

let mut sw = SequenceWriter::new(Vec::new(), Format::Binary);
sw.write_item(&Value::from(1)).unwrap();
sw.write_item(&Value::from(2)).unwrap();
let buf = sw.into_inner();
assert_eq!(buf, [0x01, 0x02]);

Auto Trait Implementations§

§

impl<W> Freeze for SequenceWriter<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for SequenceWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for SequenceWriter<W>
where W: Send,

§

impl<W> Sync for SequenceWriter<W>
where W: Sync,

§

impl<W> Unpin for SequenceWriter<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for SequenceWriter<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for SequenceWriter<W>
where W: UnwindSafe,

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> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> StrictAs for T

Source§

fn strict_as<Dst>(self) -> Dst
where T: StrictCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> StrictCastFrom<Src> for Dst
where Src: StrictCast<Dst>,

Source§

fn strict_cast_from(src: Src) -> Dst

Casts the value.
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> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.