[][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!


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.

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.