pub struct Writer { /* private fields */ }
Expand description
Wrapper for csv_core::Writer
that provides methods for serialization using serde
.
Implementations§
Source§impl Writer
impl Writer
Sourcepub fn from_builder(builder: impl Borrow<WriterBuilder>) -> Self
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'-')
);
Sourcepub fn serialize<T>(&mut self, value: &T, output: &mut [u8]) -> Result<usize>
pub fn serialize<T>(&mut self, value: &T, output: &mut [u8]) -> Result<usize>
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");
Sourcepub fn serialize_to_vec<T, const N: usize>(
&mut self,
value: &T,
) -> Result<Vec<u8, N>>
pub fn serialize_to_vec<T, const N: usize>( &mut self, value: &T, ) -> Result<Vec<u8, 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).
§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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more