1 个不稳定版本

0.3.0 2022年6月11日
0.2.1 2022年6月11日
0.2.0 2022年6月11日
0.1.0 2022年6月10日

#922 in 异步

MIT/Apache

11KB
137

Enstream

以简单轻量级的方式将 Future 转换为 Stream

crate 完全兼容 #![no_std]

示例用法

#![feature(type_alias_impl_trait)]

use std::future::Future;

use enstream::{HandlerFn, HandlerFnLifetime, Yielder, enstream};
use futures_util::{future::FutureExt, pin_mut, stream::StreamExt};

struct StreamState<'a> {
    val: &'a str
}

// A separate type alias is used to work around TAIT bugs
type Fut<'yielder, 'a: 'yielder> = impl Future<Output = ()>;

impl<'yielder, 'a> HandlerFnLifetime<'yielder, &'a str> for StreamState<'a> {
    type Fut = Fut<'yielder, 'a>;
}

impl<'a> HandlerFn<&'a str> for StreamState<'a> {
    fn call<'yielder>(
        self,
        mut yielder: Yielder<'yielder, &'a str>,
    ) -> <Self as HandlerFnLifetime<'yielder, &'a str>>::Fut {
        async move {
            yielder.yield_item(self.val).await;
        }
    }
}

let owned = String::from("test");

let stream = enstream(StreamState {
    val: &owned
});

pin_mut!(stream);

assert_eq!(stream.next().now_or_never().flatten(), Some("test"));

依赖项

~96KB