https://github.com/ivan770/enstream
Library to convert Future to Stream
https://github.com/ivan770/enstream
Last synced: about 1 year ago
JSON representation
Library to convert Future to Stream
- Host: GitHub
- URL: https://github.com/ivan770/enstream
- Owner: ivan770
- License: apache-2.0
- Created: 2022-06-10T07:34:16.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-11T13:30:45.000Z (about 4 years ago)
- Last Synced: 2024-11-10T22:51:50.383Z (over 1 year ago)
- Language: Rust
- Size: 39.1 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Enstream
Convert `Future` to `Stream` in a simple and lightweight manner.
Crate is fully compatible with `#![no_std]`.
## Example usage
```rust
#![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;
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>,
) -> >::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"));
```