The size for values of type `dyn std::error::Error + Send` cannot be known at compilation time

Hi.

I'm trying to make thread pool which can give the Result<> returns of the sub threads.
But I can't compile due to the following error.

Codes:

pub struct ThreadPool2<'scope, T, E>
where
    T: Send + 'scope,
    E: Error + Send + 'scope,
{
    name: &'scope str,
    res: Option<Vec<Result<T, E>>>,
}

let aa: ThreadPool2<(), Box<dyn Error + Send>> = ThreadPool2 {
            name:"S",
            res:None,
        };

Error:

error[E0277]: the size for values of type `dyn std::error::Error + Send` cannot be known at compilation time
   --> src/lib.rs:300:17
    |
300 |         let aa: ThreadPool2<(), Box<dyn Error + Send>> = ThreadPool2 {
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `dyn std::error::Error + Send`
    = help: the trait `std::error::Error` is implemented for `Box<T>`
    = note: required for `Box<dyn std::error::Error + Send>` to implement `std::error::Error`
note: required by a bound in `ThreadPool2`
   --> src/lib.rs:284:8
    |
281 | pub struct ThreadPool2<'scope, T, E>
    |            ----------- required by a bound in this struct
...
284 |     E: Error + Send + 'scope,
    |        ^^^^^ required by this bound in `ThreadPool2`

How could I fix this?

Any help would be appreciated.

You're running into Issue 60759. You could remove the Error bound (you don't need any of the bounds on the struct itself, and will typically just have to repeat them elsewhere). Or you could newtype Box<dyn Error + Send> and implement what you need. Or you could use Arc<dyn Error + Send + Sync>.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.