Crate fusio

Source
Expand description

Fusio is a library that provides a unified IO interface for different IO backends.

§Example

use fusio::{Error, IoBuf, IoBufMut, Read, Write};

async fn write_without_runtime_awareness<F, B, BM>(
    file: &mut F,
    write_buf: B,
    read_buf: BM,
) -> (Result<(), Error>, B, BM)
where
    F: Read + Write,
    B: IoBuf,
    BM: IoBufMut,
{
    let (result, write_buf) = file.write_all(write_buf).await;
    if result.is_err() {
        return (result, write_buf, read_buf);
    }

    file.close().await.unwrap();

    let (result, read_buf) = file.read_exact_at(read_buf, 0).await;
    if result.is_err() {
        return (result.map(|_| ()), write_buf, read_buf);
    }

    (Ok(()), write_buf, read_buf)
}

#[tokio::main]
async fn main() {
    #[cfg(feature = "tokio")]
    {
        use fusio::{disk::LocalFs, DynFs};
        use tokio::fs::File;

        let fs = LocalFs {};
        let mut file = fs.open(&"foo.txt".into()).await.unwrap();
        let write_buf = "hello, world".as_bytes();
        let mut read_buf = [0; 12];
        let (result, _, read_buf) =
            write_without_runtime_awareness(&mut file, write_buf, &mut read_buf[..]).await;
        result.unwrap();
        assert_eq!(&read_buf, b"hello, world");
    }
}

Re-exports§

pub use buf::IoBuf;
pub use buf::IoBufMut;
pub use dynamic::fs::DynFs;
pub use dynamic::DynRead;
pub use dynamic::DynWrite;
pub use impls::*;

Modules§

buf
Buffer abstraction for I/O operations.
dynamic
Dyn compatible(object safety) version of Read, Write and others.
fs
This module contains the Fs trait, which is used to abstract file system operations across different file systems.
impls
Implementations of the traits in the fusio crate.
path
A path abstraction that can be used to represent paths in a cloud-agnostic way.

Enums§

Error

Traits§

MaybeSend
Safety
MaybeSync
Same as MaybeSend, but for std::marker::Sync.
Read
The core trait for reading data, it is similar to std::io::Read, but it takes the ownership of the buffer, because completion-based IO requires the buffer to be pinned and should be safe to cancellation.
Write
The core trait for writing data, it is similar to std::io::Write, but it takes the ownership of the buffer, because completion-based IO requires the buffer to be pinned and should be safe to cancellation.