[][src]Function smol::writer

pub fn writer(
    writer: impl Write + Send + 'static
) -> impl AsyncWrite + Send + Unpin + 'static

Creates an async writer that runs on a thread.

This adapter converts any kind of synchronous writer into an asynchronous writer by running it on the blocking executor and receiving bytes over a pipe.

Note: Don't forget to flush the writer at the end, or some written bytes might get lost!

Examples

Write into a file:

use futures::prelude::*;
use smol::{blocking, writer};
use std::fs::File;

// Open a file for writing.
let file = blocking!(File::open("foo.txt"))?;
let mut file = writer(file);

// Write some bytes into the file and flush.
file.write_all(b"hello").await?;
file.flush().await?;

Write into standard output:

use futures::prelude::*;
use smol::writer;

// Create an async writer to stdout.
let mut stdout = writer(std::io::stdout());

// Write a message and flush.
stdout.write_all(b"hello").await?;
stdout.flush().await?;