Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

derive_more cannot handle mixed From impls whereas thiserror can #402

Open
U007D opened this issue Aug 27, 2024 · 2 comments
Open

derive_more cannot handle mixed From impls whereas thiserror can #402

U007D opened this issue Aug 27, 2024 · 2 comments
Assignees
Labels
Milestone

Comments

@U007D
Copy link

U007D commented Aug 27, 2024

Seeing if I can replace thiserror with derive_more:

use derive_more::{Display, Error, From};

#[derive(Debug, Display, Error, From)]
enum Error {
    SomeError(#[from] std::io::Error),
    SomeOtherError(Box<dyn Error + Send + Sync + 'static>),
}

// Perform type-erasure so that `tracing_subscriber` dependency is not leaked to clients
impl From<tracing_subscriber::util::TryInitError> for Error {
    fn from(error: tracing_subscriber::util::TryInitError) -> Self {
        Self::SomeOtherError(Box::new(error))
    }
}

gives a compilation error (conflicting From impls), whereas thiserror has no problem with this:

use thiserror::Error;

#[derive(Debug, Error)]
enum Error {
    #[error("SomeError occurred.")]
    SomeError(#[from] std::io::Error),

    #[error(transparent)]
    SomeOtherError(Box<dyn Error + Send + Sync + 'static>),
}

// Perform type-erasure so that `tracing_subscriber` dependency is not leaked to clients
impl From<tracing_subscriber::util::TryInitError> for Error {
    fn from(error: tracing_subscriber::util::TryInitError) -> Self {
        Self::SomeOtherError(Box::new(error))
    }
}

compiles fine.

@tyranron tyranron self-assigned this Aug 28, 2024
@tyranron tyranron added the bug label Aug 28, 2024
@tyranron tyranron added this to the 1.0.1 milestone Aug 28, 2024
@tyranron
Copy link
Collaborator

@U007D I cannot reproduce, could you clarify?

Here is the cargo-script I'm running:

#!/usr/bin/env run-cargo-script
//! ```cargo
//! [dependencies]
//! derive_more = { version = "1.0.0", features = ["display", "error", "from"] }
//! tracing-subscriber = "0.3.18"
//! ```
extern crate derive_more;
extern crate tracing_subscriber;

use derive_more::{Display, Error, From};

#[derive(Debug, Display, Error, From)]
enum FooError {
    SomeError(std::io::Error),
    SomeOtherError(Box<dyn Error + Send + Sync + 'static>),
}

// Perform type-erasure so that `tracing_subscriber` dependency is not leaked to clients
impl From<tracing_subscriber::util::TryInitError> for FooError {
    fn from(error: tracing_subscriber::util::TryInitError) -> Self {
        Self::SomeOtherError(Box::new(error))
    }
}

fn main() {}

Running

 cargo script repro.rs

gives no errors.

@JelteF
Copy link
Owner

JelteF commented Sep 8, 2024

Okay, afaict this is another case of: #405

If I change @U007D his example to use use derive_more::derive::{Display, Error, From} instead of use derive_more::{Display, Error, From}, then it compiles fine for me (after also changing dyn Error to dyn std::error::Error.

@U007D if that doesn't fix your issue could you give an example that fails in the way you are describing?

@JelteF JelteF added moreinfo and removed bug labels Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants