5 个版本

0.1.5 2022年6月25日
0.1.4 2022年6月25日
0.1.3 2022年3月23日
0.1.2 2022年1月14日
0.1.0 2020年5月31日

#578异步

Download history · Rust 包仓库 342/week @ 2024-03-13 · Rust 包仓库 323/week @ 2024-03-20 · Rust 包仓库 318/week @ 2024-03-27 · Rust 包仓库 370/week @ 2024-04-03 · Rust 包仓库 207/week @ 2024-04-10 · Rust 包仓库 316/week @ 2024-04-17 · Rust 包仓库 390/week @ 2024-04-24 · Rust 包仓库 498/week @ 2024-05-01 · Rust 包仓库 539/week @ 2024-05-08 · Rust 包仓库 384/week @ 2024-05-15 · Rust 包仓库 587/week @ 2024-05-22 · Rust 包仓库 402/week @ 2024-05-29 · Rust 包仓库 474/week @ 2024-06-05 · Rust 包仓库 397/week @ 2024-06-12 · Rust 包仓库 755/week @ 2024-06-19 · Rust 包仓库 752/week @ 2024-06-26 · Rust 包仓库

2,425 每月下载量

MIT/Apache

45KB
1K SLoC

GNU Style Assembly 690 SLoC · Rust 包仓库 Rust 498 SLoC // 0.0% comments · Rust 包仓库 C 56 SLoC · Rust 包仓库

stackful

Build Status

stackful 尝试在同步和异步之间建立桥梁并模糊它们之间的差异。

它允许您使用提供的两个函数 waitstackful 轻松地在它们之间进行转换。如果您使用的是一个仅在异步 IO 上提供同步接口的库,这可能会非常有用。

更多详细信息请参阅文档或源代码。

示例

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;
    });
}

依赖项

~0–305KB