5 releases

0.1.5 Jun 25, 2022
0.1.4 Jun 25, 2022
0.1.3 Mar 23, 2022
0.1.2 Jan 14, 2022
0.1.0 May 31, 2020

#1097 in Asynchronous

Download history 373/week @ 2024-07-25 337/week @ 2024-08-01 337/week @ 2024-08-08 329/week @ 2024-08-15 310/week @ 2024-08-22 315/week @ 2024-08-29 311/week @ 2024-09-05 311/week @ 2024-09-12 321/week @ 2024-09-19 311/week @ 2024-09-26 330/week @ 2024-10-03 320/week @ 2024-10-10 316/week @ 2024-10-17 308/week @ 2024-10-24 297/week @ 2024-10-31 143/week @ 2024-11-07

1,108 downloads per month

MIT/Apache

45KB
1K SLoC

GNU Style Assembly 690 SLoC Rust 498 SLoC // 0.0% comments C 56 SLoC

stackful

Build Status

stackful attempts to bridge sync and async and blur the difference between them.

It allows you to easily convert between them with two supplied function wait and stackful. It can be quitely useful if you are using a library that only provides sync interface on top of async IO.

More details can be found in the docs or the source code.

Example

use async_std::io::Read as AsyncRead;
use async_std::prelude::*;
use byteorder::{ReadBytesExt, LE};
use stackful::{stackful, wait};
use std::io::Read;
use std::marker::Unpin;

struct Sync<T>(T);

impl<T> Read for Sync<T>
where
    T: AsyncRead + Unpin,
{
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        wait(self.0.read(buf))
    }
}

async fn process(stream: &mut (dyn AsyncRead + Unpin)) -> u32 {
    stackful(|| {
        let mut sync = Sync(stream);
        // Note that this will recursively call into `read` function will
        // calls `wait` to await the future.
        sync.read_u32::<LE>().unwrap()
        // This is just an example, can be complex processing, zipping, etc.
        // If you are calling into a FFI library that uses a callback, you
        // can even `wait()` from that callback and turn the whole FFI library
        // into async!
    })
    .await
}

fn main() {
    async_std::task::block_on(async {
        async_std::task::spawn_local(async {
            // This is just an example, can be any AsyncRead stream
            let mut stream: &[u8] = &[0xef, 0xbe, 0xad, 0xde];
            println!("{:x}", process(&mut stream).await);
        })
        .await;
    });
}

Dependencies

~0–410KB