I need to implement a trait that is returning the futures::StreamExt trait.
In general this sounds easy and there are several answers to this e.g. this here.
I tried this with StreamExt but this does - for some reason - not work. Here my sample code:
// [dependencies] // futures = "0.3.6" // async-std = { version = "1.6.5", features = ["attributes", "unstable"] } // interval function is unstable atm. use std::time::Duration; use futures::StreamExt; trait StreamProvidingTrait { fn returnastream(self: &Self) -> Box<dyn StreamExt<Item=i32>>; } struct StreamProvider {} impl StreamProvidingTrait for StreamProvider { fn returnastream(self: &Self) -> Box<dyn StreamExt<Item=i32>> { return Box::new(async_std::stream::interval(Duration::from_millis(1000)).map(|_| 1)); } } #[async_std::main] async fn main() { let mut counter = 0; let object = StreamProvider {}; // creates a stream let worx = object.returnastream(); // mappes the stream into something.... let mut mapped_stream = worx.map(|_| { counter = counter + 1; counter }); // subscribing to the items while let item = mapped_stream.next().await { match item { Some(value) => println!("{}", value), _ => {} } } } And here the error:
Compiling traittest v0.1.0 (/Users/andre/repos/traittest) warning: the trait `futures_util::stream::stream::StreamExt` cannot be made into an object --> /Users/andre/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.6/src/stream/stream/mod.rs:1326:8 | 1326 | fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> | ^^^^^^^^^^^^^^^ the trait cannot be made into an object because method `poll_next_unpin` references the `Self` type in its `where` clause | = note: `#[warn(where_clauses_object_safety)]` on by default = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #51443 <https://github.com/rust-lang/rust/issues/51443> error[E0038]: the trait `futures_util::stream::stream::StreamExt` cannot be made into an object --> src/main.rs:6:36 | 6 | fn returnastream(self: &Self) -> Box<dyn StreamExt<Item=i32>>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `futures_util::stream::stream::StreamExt` cannot be made into an object | ::: /Users/andre/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.6/src/stream/stream/mod.rs:226:8 | 226 | fn next(&mut self) -> Next<'_, Self> | ---- the trait cannot be made into an object because method `next` references the `Self` type in its return type ... 976 | fn by_ref(&mut self) -> &mut Self { | ------ the trait cannot be made into an object because method `by_ref` references the `Self` type in its return type ... 1381 | fn select_next_some(&mut self) -> SelectNextSome<'_, Self> | ---------------- the trait cannot be made into an object because method `select_next_some` references the `Self` type in its return type error[E0038]: the trait `futures_util::stream::stream::StreamExt` cannot be made into an object --> src/main.rs:12:36 | 12 | fn returnastream(self: &Self) -> Box<dyn StreamExt<Item=i32>> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `futures_util::stream::stream::StreamExt` cannot be made into an object | ::: /Users/andre/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.6/src/stream/stream/mod.rs:226:8 | 226 | fn next(&mut self) -> Next<'_, Self> | ---- the trait cannot be made into an object because method `next` references the `Self` type in its return type ... 976 | fn by_ref(&mut self) -> &mut Self { | ------ the trait cannot be made into an object because method `by_ref` references the `Self` type in its return type ... 1381 | fn select_next_some(&mut self) -> SelectNextSome<'_, Self> | ---------------- the trait cannot be made into an object because method `select_next_some` references the `Self` type in its return type error: aborting due to 2 previous errors; 1 warning emitted For more information about this error, try `rustc --explain E0038`. error: could not compile `traittest`. To learn more, run the command again with --verbose. Process finished with exit code 101 When I exchange the StreamExt trait with my own one, this code works perfectly.
trait SomeTrait { fn returnatrait(self: &Self) -> Box<dyn SomeOtherTrait>; } trait SomeOtherTrait { fn sayHelloWorld(&self); } struct DummyStruct {} impl SomeOtherTrait for DummyStruct { fn sayHelloWorld(&self) { println!("hello world"); } } struct Implementation {} impl SomeTrait for Implementation { fn returnatrait(self: &Self) -> Box<dyn SomeOtherTrait> { return Box::new(DummyStruct{}); } } fn main() { let implementation = Implementation{}; let worx = implementation.returnatrait(); worx.sayHelloWorld(); } What's wrong here? There's obviously something I don't understand. Please help me understand this!
fn get_self(&self) -> &Selfto your trait? This seems to stick out in the error message.