Struct csv_async::AsyncWriter[][src]

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

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

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(())
}

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(())
}

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(())
}

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(())
}

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.

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

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.