[][src]Struct csv_async::AsyncWriter

pub struct AsyncWriter<W: AsyncWrite + Unpin>(_);

A already configured CSV writer.

A CSV writer takes as input Rust values and writes those values in a valid CSV format as output.

While CSV writing is considerably easier than parsing CSV, a proper writer will do a number of things for you:

  1. Quote fields when necessary.
  2. Check that all records have the same number of fields.
  3. Write records with a single empty field correctly.
  4. Use buffering intelligently and otherwise avoid allocation. (This means that callers should not do their own buffering.)

All of the above can be configured using a AsyncWriterBuilder. However, a AsyncWriter has convenient constructor (from_writer`) that use the default configuration.

Note that the default configuration of a AsyncWriter uses \n for record terminators instead of \r\n as specified by RFC 4180. Use the terminator method on AsyncWriterBuilder to set the terminator to \r\n if it's desired.

Implementations

impl<W: AsyncWrite + Unpin> AsyncWriter<W>[src]

pub fn from_writer(wtr: W) -> AsyncWriter<W>[src]

Build a CSV writer with a default configuration that writes data to wtr.

Note that the CSV writer is buffered automatically, so you should not wrap wtr in a buffered writer.

Example

use std::error::Error;
use csv_async::AsyncWriter;

async fn example() -> Result<(), Box<dyn Error>> {
    let mut wtr = AsyncWriter::from_writer(vec![]);
    wtr.write_record(&["a", "b", "c"]).await?;
    wtr.write_record(&["x", "y", "z"]).await?;

    let data = String::from_utf8(wtr.into_inner().await?)?;
    assert_eq!(data, "a,b,c\nx,y,z\n");
    Ok(())
}

pub async fn write_record<I, T, '_>(&'_ mut self, record: I) -> Result<()> where
    I: IntoIterator<Item = T>,
    T: AsRef<[u8]>, 
[src]

Write a single record.

This method accepts something that can be turned into an iterator that yields elements that can be represented by a &[u8].

This may be called with an empty iterator, which will cause a record terminator to be written. If no fields had been written, then a single empty field is written before the terminator.

Example

use std::error::Error;
use csv_async::AsyncWriter;

async fn example() -> Result<(), Box<dyn Error>> {
    let mut wtr = AsyncWriter::from_writer(vec![]);
    wtr.write_record(&["a", "b", "c"]).await?;
    wtr.write_record(&["x", "y", "z"]).await?;

    let data = String::from_utf8(wtr.into_inner().await?)?;
    assert_eq!(data, "a,b,c\nx,y,z\n");
    Ok(())
}

pub async fn write_byte_record<'_, '_>(
    &'_ mut self,
    record: &'_ ByteRecord
) -> Result<()>
[src]

Write a single ByteRecord.

This method accepts a borrowed ByteRecord and writes its contents to the underlying writer.

This is similar to write_record except that it specifically requires a ByteRecord. This permits the writer to possibly write the record more quickly than the more generic write_record.

This may be called with an empty record, which will cause a record terminator to be written. If no fields had been written, then a single empty field is written before the terminator.

Example

use std::error::Error;
use csv_async::{ByteRecord, AsyncWriter};

async fn example() -> Result<(), Box<dyn Error>> {
    let mut wtr = AsyncWriter::from_writer(vec![]);
    wtr.write_byte_record(&ByteRecord::from(&["a", "b", "c"][..])).await?;
    wtr.write_byte_record(&ByteRecord::from(&["x", "y", "z"][..])).await?;

    let data = String::from_utf8(wtr.into_inner().await?)?;
    assert_eq!(data, "a,b,c\nx,y,z\n");
    Ok(())
}

pub async fn write_field<T: AsRef<[u8]>, '_>(
    &'_ mut self,
    field: T
) -> Result<()>
[src]

Write a single field.

One should prefer using write_record over this method. It is provided for cases where writing a field at a time is more convenient than writing a record at a time.

Note that if this API is used, write_record should be called with an empty iterator to write a record terminator.

Example

use std::error::Error;
use csv_async::AsyncWriter;

async fn example() -> Result<(), Box<dyn Error>> {
    let mut wtr = AsyncWriter::from_writer(vec![]);
    wtr.write_field("a").await?;
    wtr.write_field("b").await?;
    wtr.write_field("c").await?;
    wtr.write_record(None::<&[u8]>).await?;
    wtr.write_field("x").await?;
    wtr.write_field("y").await?;
    wtr.write_field("z").await?;
    wtr.write_record(None::<&[u8]>).await?;

    let data = String::from_utf8(wtr.into_inner().await?)?;
    assert_eq!(data, "a,b,c\nx,y,z\n");
    Ok(())
}

pub async fn flush<'_>(&'_ mut self) -> Result<()>[src]

Flush the contents of the internal buffer to the underlying writer.

If there was a problem writing to the underlying writer, then an error is returned.

This finction is also called by writer destructor.

pub async fn into_inner(self) -> Result<W, Error>[src]

Flush the contents of the internal buffer and return the underlying writer.

Trait Implementations

impl<W: Debug + AsyncWrite + Unpin> Debug for AsyncWriter<W>[src]

Auto Trait Implementations

impl<W> RefUnwindSafe for AsyncWriter<W> where
    W: RefUnwindSafe

impl<W> Send for AsyncWriter<W> where
    W: Send

impl<W> Sync for AsyncWriter<W> where
    W: Sync

impl<W> Unpin for AsyncWriter<W>

impl<W> UnwindSafe for AsyncWriter<W> where
    W: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.