Skip to main content

Writer

Struct Writer 

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

Writes CSV data to a std::io::Write sink.

The writer handles auto-quoting of fields that contain the delimiter, newlines, or double-quotes. It also validates that all rows have the same number of fields (unless flexible mode is enabled).

Internal buffering is used to avoid many small writes to the underlying writer. Callers should not wrap the writer in a BufWriter.

§Example

let mut w = Writer::new(Vec::new());
w.write_row(["name", "age", "city"]).unwrap();
w.write_row(["Alice", "30", "New York, NY"]).unwrap();
let result = String::from_utf8(w.into_inner().unwrap()).unwrap();
assert_eq!(result, "name,age,city\r\nAlice,30,\"New York, NY\"\r\n");

Implementations§

Source§

impl<W: Write> Writer<W>

Source

pub fn new(writer: W) -> Self

Create a new writer wrapping the given output sink.

Source

pub fn delimiter(&mut self, byte: u8) -> &mut Self

Set the field delimiter byte (default is ,).

Source

pub fn flexible(&mut self, yes: bool) -> &mut Self

Enable flexible mode, which skips the field count consistency check.

Source

pub fn write_row<I, T>(&mut self, row: I) -> Result<(), WriteError>
where I: IntoIterator<Item = T>, T: AsRef<[u8]>,

Write a single row. Each element in the iterator is written as a CSV field. Fields are auto-quoted if they contain the delimiter, a newline (\n or \r), or a double-quote (").

Whitespace trimming is never performed. The empty byte slice &[] produces an empty field.

§Errors

Returns WriteError::InconsistentFieldCount if the number of fields differs from previous rows (unless flexible mode is enabled). Returns WriteError::Io if the underlying writer fails.

Source

pub fn flush(&mut self) -> Result<(), WriteError>

Flush the internal buffer to the underlying writer.

Source

pub fn into_inner(self) -> Result<W, WriteError>

Unwrap the writer, flushing any remaining data and returning the underlying writer.

Trait Implementations§

Source§

impl<W: Write> Drop for Writer<W>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<W> UnwindSafe for Writer<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> 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.