# I/O Stream [![Documentation](https://img.shields.io/docsrs/io-stream)](https://docs.rs/io-stream/latest/io_stream) [![Matrix](https://img.shields.io/matrix/pimalaya:matrix.org?color=success&label=chat)](https://matrix.to/#/#pimalaya:matrix.org)

Set of **I/O-free** Rust coroutines and runtimes to manage streams.

This library allows you to manage streams using an I/O-agnostic approach, based on 3 concepts:

### Coroutine

A coroutine is an *I/O-free*, *resumable* and *composable* state machine that **emits I/O requests**. A coroutine is considered *terminated* when it does not emit I/O requests anymore.

*See available coroutines at [./src/coroutines](https://github.com/pimalaya/io-stream/tree/master/src/coroutines).*

### Runtime

A runtime contains all the I/O logic, and is responsible for **processing I/O requests** emitted by coroutines.

*See available runtimes at [./src/runtimes](https://github.com/pimalaya/io-stream/tree/master/src/runtimes).*

### Loop

The loop is the glue between coroutines and runtimes. It makes the coroutine progress while allowing runtime to process I/O.

## Examples

### Read stdin synchronously

```rust,ignore
use io_stream::{coroutines::Read, runtimes::std::handle};

let mut stdin = std::io::stdin();

let mut arg = None;
let mut read = Read::new();

let output = loop {
    match read.resume(arg) {
        Ok(output) => break output,
        Err(io) => arg = Some(handle(&mut stdin, io).unwrap()),
    }
};

// same as &output.buffer[..output.bytes_count]
let bytes = output.bytes();
```

### Write TCP stream asynchronously

```rust,ignore
use tokio::net::TcpStream;
use io_stream::{coroutines::Write, runtimes::tokio::handle};

let mut tcp = TcpStream::connect("127.0.0.1:1234").await.unwrap();

let mut arg = None;
let mut write = Write::new(b"bytes".iter().cloned());

while let Err(io) = write.resume(arg) {
    arg = Some(handle(&mut tcp, io).await.unwrap());
}
```

*See complete examples at [./examples](https://github.com/pimalaya/io-stream/blob/master/examples).*

## More examples

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

- [io-addressbook](https://github.com/pimalaya/io-addressbook): Set of I/O-free coroutines to manage contacts
- [io-http](https://github.com/pimalaya/io-http): Set of I/O-free Rust coroutines to manage HTTP streams
- [io-oauth](https://github.com/pimalaya/io-oauth): Set of I/O-free Rust coroutines to manage OAuth flows
- [io-starttls](https://github.com/pimalaya/io-starttls): I/O-free coroutine to upgrade any plain stream to a secure one
- [io-timer](https://github.com/pimalaya/io-timer): Set of I/O-free coroutines to manage timers

- [Cardamum](https://github.com/pimalaya/cardamum): CLI to manage contacts
- [Comodoro](https://github.com/pimalaya/comodoro): CLI to manage timers
- [Ortie](https://github.com/pimalaya/ortie): CLI to manage OAuth access tokens

## License

This project is licensed under either of:

- [MIT license](LICENSE-MIT)
- [Apache License, Version 2.0](LICENSE-APACHE)

at your option.

## Sponsoring

[![nlnet](https://nlnet.nl/logo/banner-160x60.png)](https://nlnet.nl/)

Special thanks to the [NLnet foundation](https://nlnet.nl/) and the [European Commission](https://www.ngi.eu/) that helped the project to receive financial support from various programs:

- [NGI Assure](https://nlnet.nl/project/Himalaya/) in 2022
- [NGI Zero Entrust](https://nlnet.nl/project/Pimalaya/) in 2023
- [NGI Zero Core](https://nlnet.nl/project/Pimalaya-PIM/) in 2024 *(still ongoing)*

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

[![GitHub](https://img.shields.io/badge/-GitHub%20Sponsors-fafbfc?logo=GitHub%20Sponsors)](https://github.com/sponsors/soywod)
[![Ko-fi](https://img.shields.io/badge/-Ko--fi-ff5e5a?logo=Ko-fi&logoColor=ffffff)](https://ko-fi.com/soywod)
[![Buy Me a Coffee](https://img.shields.io/badge/-Buy%20Me%20a%20Coffee-ffdd00?logo=Buy%20Me%20A%20Coffee&logoColor=000000)](https://www.buymeacoffee.com/soywod)
[![Liberapay](https://img.shields.io/badge/-Liberapay-f6c915?logo=Liberapay&logoColor=222222)](https://liberapay.com/soywod)
[![thanks.dev](https://img.shields.io/badge/-thanks.dev-000000?logo=data:image/svg+xml;base64, )](https://thanks.dev/soywod)
[![PayPal](https://img.shields.io/badge/-PayPal-0079c1?logo=PayPal&logoColor=ffffff)](https://www.paypal.com/paypalme/soywod)
