Skip to content

error: the trait core::marker::Sized is not implemented for the type std::error::Error + Sized + 'static #33201

Closed
@julienw

Description

@julienw

The error is not easy to understand for this code:

use std::error::Error;
use std::fmt;

#[derive(Debug, Clone)]
struct SomeError {
  cause: String
}

impl fmt::Display for SomeError {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    write!(f, "SomeError")
  }
}

impl Error for SomeError {
  fn description(&self) -> &str {
    "Some Error"
  }

  fn cause(&self) -> Option<&Error> {
    None
  }
}

#[derive(Debug, Clone)]
enum NewError {
  SomeCause(Error + Sized)
}

fn main() {
    let error = NewError::SomeCause(SomeError { cause: "some cause".to_string() });
    println!("{:?}", error);
}

(it's also in https://play.rust-lang.org/?gist=ec4209f420275037c3f652a7aeae01ff&version=stable&mode=debug)

If I understand properly, the issue is that I should have something like this instead:

enum NewError<T: Error> {
  SomeCause(T)
}

But the error is very misleading.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions