I am trying to wrap my head around the new world order of PinMut and dyn Traits in order to generate a stream and return it to a user. Useful for something like returning a request stream to a client so they can't wait on the result (or concat it themselves).
use futures::stream::{ repeat, Repeat };
use std::future::{FutureObj};
struct Response<'a> {
stream: FutureObj<'a, Repeat<u8>>
}
pub fn main() {
let mut s = repeat(1);
let _r = Response {
stream: FutureObj::new(&mut s)
};
}
but a Stream cannot be put in a FutureObj, the error:
error[E0277]: the trait bound `futures::stream::Repeat<{integer}>: futures::Future` is not satisfied
--> src/boop.rs:11:17
|
11 | stream: FutureObj::new(&mut s)
| ^^^^^^^^^^^^^^ the trait `futures::Future` is not implemented for `futures::stream::Repeat<{integer}>`
|
= note: required because of the requirements on the impl of `std::future::UnsafeFutureObj<'_, _>` for `&mut futures::stream::Repeat<{integer}>`
= note: required by `<std::future::FutureObj<'a, T>>::new`
error: aborting due to 2 previous errors
It compiles if I transform the Stream in to a weird-o Future but I don't think this is what I want:
use futures::stream::{ repeat, StreamExt, Repeat }; //, Stream };
use std::future::{FutureObj};
struct Response<'a> {
stream: FutureObj<'a, (Option<u8>, Repeat<u8>)>
}
pub fn main() {
let s = repeat(1);
let mut sf = s.into_future();
let _r = Response {
stream: FutureObj::new(&mut sf)
};
}
I haven't even worked my way to async/await style structures. Has anyone tried that out? Is there any sample code out there?