Struct Writer

Source
pub struct Writer { /* private fields */ }
Expand description

Wrapper for csv_core::Writer that provides methods for serialization using serde.

Implementations§

Source§

impl Writer

Source

pub fn new() -> Self

Constructs a new writer.

Source

pub fn from_builder(builder: impl Borrow<WriterBuilder>) -> Self

Constructs a new writer from csv_core::WriterBuilder.

§Example
use serde_csv_core::csv_core;

let writer = serde_csv_core::Writer::from_builder(
    csv_core::WriterBuilder::new()
        .delimiter(b'-')
);
Source

pub fn serialize<T>(&mut self, value: &T, output: &mut [u8]) -> Result<usize>
where T: Serialize + ?Sized,

Serializes the given value as a CSV byte slice.

Inserts record terminator after the serialized value. Flattens compound types (e.g. nested structs, tuples, vectors). On success, it returns the number of bytes written.

§Example
use heapless::String;

#[derive(serde::Serialize)]
struct Record {
    pub country: String<32>,
    pub city: String<32>,
    pub population: u32,
}

let record = Record {
    country: "Poland".into(),
    city: "Cracow".into(),
    population: 766_683,
};

let mut writer = serde_csv_core::Writer::new();
let mut csv = [0; 32];
let nwritten = writer.serialize(&record, &mut csv)?;

assert_eq!(&csv[..nwritten], b"Poland,Cracow,766683\n");
Source

pub fn serialize_to_vec<T, const N: usize>( &mut self, value: &T, ) -> Result<Vec<u8, N>>
where T: Serialize + ?Sized,

Serializes the given value as a CSV byte vector.

Inserts record terminator after the serialized value. Flattens compound types (e.g. nested structs, tuples, vectors).

§Example
use heapless::{String, Vec};

#[derive(serde::Serialize)]
struct Record {
    pub country: String<32>,
    pub city: String<32>,
    pub population: u32
}

let record = Record {
    country: "Poland".into(),
    city: "Cracow".into(),
    population: 766_683
};

let mut writer = serde_csv_core::Writer::new();
let buf: Vec<u8, 32> = writer.serialize_to_vec(&record)?;

assert_eq!(&buf, b"Poland,Cracow,766683\n");

Serializes the given value as a CSV byte vector.

Inserts record terminator after the serialized value. Flattens compound types (e.g. nested structs, tuples, vectors).

Trait Implementations§

Source§

impl Debug for Writer

Source§

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

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

impl Default for Writer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Writer

§

impl RefUnwindSafe for Writer

§

impl Send for Writer

§

impl Sync for Writer

§

impl Unpin for Writer

§

impl UnwindSafe for Writer

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