Skip to content

pimalaya/io-fs

I/O Filesystem Documentation Matrix Mastodon

I/O-free filesystem client library written in Rust

This library provides an I/O-agnostic abstraction over filesystem operations — reading, writing, copying, renaming, and removing files and directories — based on three concepts:

Coroutine

A coroutine is an I/O-free, resumable and composable state machine. It emits I/O requests without performing any I/O itself, and receives I/O responses to make progress. A coroutine is terminated when it stops emitting I/O requests.

See available coroutines at ./src/coroutines.

Runtime

A runtime contains all the I/O logic. It is responsible for processing I/O requests and returning the corresponding I/O responses. A runtime targets a specific execution model (blocking std, async Tokio).

See available runtimes at ./src/runtimes.

Loop

The loop is the glue between coroutines and runtimes. It drives the coroutine forward by feeding each FsOutput back as the next argument, until the coroutine terminates.

Examples

Read a directory (blocking)

use io_fs::{
    coroutines::dir_read::{FsDirRead, FsDirReadResult},
    runtimes::std::handle,
};

let mut arg = None;
let mut read = FsDirRead::new(["/tmp"]);

let entries = loop {
    match read.resume(arg.take()) {
        FsDirReadResult::Ok(entries) => break entries,
        FsDirReadResult::Io(input) => arg = Some(handle(input).unwrap()),
        FsDirReadResult::Err(err) => panic!("{err}"),
    }
};

for (dir, children) in &entries {
    for path in children {
        println!("{path}");
    }
}

Create a file (async)

use io_fs::{
    coroutines::file_create::{FsFileCreate, FsFileCreateResult},
    runtimes::tokio::handle,
};

let mut arg = None;
let mut create = FsFileCreate::new([("/tmp/hello.txt", *b"Hello, world!")]);

loop {
    match create.resume(arg.take()) {
        FsFileCreateResult::Ok => break,
        FsFileCreateResult::Io(input) => arg = Some(handle(input).await.unwrap()),
        FsFileCreateResult::Err(err) => panic!("{err}"),
    }
}

See complete examples at ./examples.

More examples

Have a look at projects built on top of this library:

License

This project is licensed under either of:

at your option.

Social

Sponsoring

nlnet

Special thanks to the NLnet foundation and the European Commission that have been financially supporting the project for years:

If you appreciate the project, feel free to donate using one of the following providers:

GitHub Ko-fi Buy Me a Coffee Liberapay thanks.dev PayPal

About

I/O-free filesystem client library written in Rust

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Contributing

Security policy

Stars

Watchers

Forks

Contributors