Skip to content

Refactor CuError to keep a pure Dyn to the Optional Cause to simplify the API #317

@gbin

Description

@gbin

Something like:

#[derive(Debug)]
pub struct CuError {
    message: String,
    cause: Option<Box<dyn std::error::Error + Send + Sync>>,
}

impl CuError {
    pub fn new(message: impl Into<String>) -> Self {
        Self { message: message.into(), cause: None }
    }

    pub fn with_cause<E>(message: impl Into<String>, cause: E) -> Self
    where
        E: Into<Box<dyn std::error::Error + Send + Sync>>,
    {
        Self { message: message.into(), cause: Some(cause.into()) }
    }
}

// plus a free function ...
pub fn with_cause(msg: impl Into<String>, err: impl Into<Box<dyn Error + Send + Sync>>) -> CuError {
    CuError::with_cause(msg, err)
}

and just inline:

use ...::with_cause;

return Err(with_cause("tada", &error));

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions