Using try_unfold() got error the trait `std::marker::Sized` is not implemented for `dyn std::error::Error`

Hi there,
I'm trying to use try_unfold() but encountered below error I'm not sure how to fix.
Please help, thank you.

use futures::stream::{self, Stream};
use std::error::Error;

fn main() {
}


fn test_stream() -> Box<dyn Stream<Item = i32>> {
    Box::new(stream::unfold(0, |state| async move {
        Some((10, 1))
    }))
}

fn test_try_stream() -> Box<dyn Stream<Item = Result<i32, dyn Error>>> {
    Box::new(stream::try_unfold(0, |state| async move {
        Ok(Some((10, 1)))
    }))
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
  --> src/main.rs:15:5
   |
15 | /     Box::new(stream::try_unfold(0, |state| async move {
16 | |         Ok(Some((10, 1)))
17 | |     }))
   | |_______^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::error::Error`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required because of the requirements on the impl of `futures_core::future::TryFuture` for `impl core::future::future::Future`
   = note: required for the cast to the object type `dyn futures_core::stream::Stream<Item = std::result::Result<i32, dyn std::error::Error>>`

error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
  --> src/main.rs:15:55
   |
15 |       Box::new(stream::try_unfold(0, |state| async move {
   |  _______________________________________________________^
16 | |         Ok(Some((10, 1)))
17 | |     }))
   | |_____^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::error::Error`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required by `std::result::Result`

error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
  --> src/main.rs:15:14
   |
15 |     Box::new(stream::try_unfold(0, |state| async move {
   |              ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   | 
  ::: /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-util-0.3.4/src/stream/try_stream/try_unfold.rs:60:10
   |
60 |     Fut: TryFuture<Ok = Option<(Item, T)>>,
   |          --------------------------------- required by this bound in `futures_util::stream::try_stream::try_unfold::try_unfold`
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::error::Error`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required because of the requirements on the impl of `futures_core::future::TryFuture` for `impl core::future::future::Future`

error[E0277]: the size for values of type `dyn std::error::Error` cannot be known at compilation time
  --> src/main.rs:16:9
   |
16 |         Ok(Some((10, 1)))
   |         ^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::error::Error`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required by `std::prelude::v1::Ok`

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

I don't think the error is about try_unfold specifically. Rather, dyn Error is not a Sized, and thus can't be stored in Result::Err. Result<i32, dyn Error> doesn't make sense.

I suggest boxing the errors and having your result type be Result<i32, Box<dyn Error>>.

Note that you need two levels of boxing here because there are two dyn abstractions: you're returning an arbitrary stream, but values inside that stream may also then have an arbitrary error type.

1 Like

Thank you @daboross!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.