Expand description
This crate provides traits and utilities for completion-based IO.
§Contents
§Fundamental
AsyncRead: Async read into a buffer implementsIoBufMutAsyncReadAt: Async read into a buffer implementsIoBufMutwith offsetAsyncWrite: Async write from a buffer implementsIoBufAsyncWriteAt: Async write from a buffer implementsIoBufwith offset
§Buffered IO
AsyncBufRead: Trait of async read with buffered contentBufReader: An async reader with internal bufferBufWriter: An async writer with internal buffer
§Extension
AsyncReadExt: Extension trait forAsyncReadAsyncReadAtExt: Extension trait forAsyncReadAtAsyncWriteExt: Extension trait forAsyncWriteAsyncWriteAtExt: Extension trait forAsyncWriteAt
§Adapters
framed::Framed: AdaptsAsyncReadtoStreamandAsyncWritetoSink, with framed de/encoding.- [
compat::SyncStream]: Adapts async IO to std blocking io (requirescompatfeature) - [
compat::AsyncStream]: Adapts async IO to [futures_util::io] traits (requirescompatfeature)
§Examples
§Read
use compio_buf::BufResult;
use compio_io::AsyncRead;
let mut reader = "Hello, world!".as_bytes();
let (res, buf) = reader.read(Vec::with_capacity(20)).await.unwrap();
assert_eq!(buf.as_slice(), b"Hello, world!");
assert_eq!(res, 13);§Write
Writing to a fixed-size buffer wrapped by Cursor. The
implementation will write the content start at the current
position:
use std::io::Cursor;
use compio_buf::BufResult;
use compio_io::AsyncWrite;
let mut writer = Cursor::new([0; 6]);
writer.set_position(2);
let (n, buf) = writer.write(vec![1, 1, 1, 1, 1, 1]).await.unwrap();
assert_eq!(n, 4);
assert_eq!(writer.into_inner(), [0, 0, 1, 1, 1, 1]);Writing to Vec<u8>, which is extendable. Notice that the implementation
will append the content to the end:
use compio_buf::BufResult;
use compio_io::AsyncWrite;
let mut writer = vec![1, 2, 3];
let (_, buf) = writer.write(vec![3, 2, 1]).await.unwrap();
assert_eq!(writer, [1, 2, 3, 3, 2, 1]);This crate doesn’t depend on a specific runtime. It can work with tokio
well:
use compio_buf::BufResult;
use compio_io::AsyncWrite;
#[tokio::main(flavor = "current_thread")]
async fn main() {
let mut writer = vec![1, 2, 3];
let (_, buf) = writer.write(vec![3, 2, 1]).await.unwrap();
assert_eq!(writer, [1, 2, 3, 3, 2, 1]);
}Modules§
Structs§
- BufReader
- Wraps a reader and buffers input from
AsyncRead - BufWriter
- Wraps a writer and buffers its output.
Traits§
- Async
BufRead - AsyncBufRead
- Async
Read - AsyncRead
- Async
Read At - AsyncReadAt
- Async
Read AtExt - Implemented as an extension trait, adding utility methods to all
AsyncReadAttypes. Callers will tend to import this trait instead ofAsyncReadAt. - Async
Read Ext - Implemented as an extension trait, adding utility methods to all
AsyncReadtypes. Callers will tend to import this trait instead ofAsyncRead. - Async
Read Managed - AsyncReadManaged
- Async
Read Managed At - AsyncReadAtManaged
- Async
Write - AsyncWrite
- Async
Write At - AsyncWriteAt
- Async
Write AtExt - Implemented as an extension trait, adding utility methods to all
AsyncWriteAttypes. Callers will tend to import this trait instead ofAsyncWriteAt. - Async
Write Ext - Implemented as an extension trait, adding utility methods to all
AsyncWritetypes. Callers will tend to import this trait instead ofAsyncWrite.
Functions§
- copy
- Asynchronously copies the entire contents of a reader into a writer.
- null
- Create a new
Nullreader and writer which acts like a black hole. - repeat
- Creates a reader that infinitely repeats one byte.
- split
- Splits a single value implementing
AsyncRead + AsyncWriteinto separateAsyncReadandAsyncWritehandles.with internal synchronization. - split_
unsync - Splits a single value implementing
AsyncRead + AsyncWriteinto separateAsyncReadandAsyncWritehandles without internal synchronization (notSendandSync).