Problem understanding adding clone trait to custom error type

⚓ Rust    📅 2026-05-31    👤 surdeus    👁️ 3      

surdeus

I have an iced based program that graphs data onto a canvas. I wanted to be able to assemble the data needed to draw the graph in a future async task and be able to use the ? for error handling. So I wrote the future to return a Result with a data structure and a custom error (it is my understanding that all functions in the future must kick out the same error type for ? to work). The custom error needs to have the clone trait so I added it to the #[derive ... line and all but the ChronoError in the custom error enumeration kicked out the same error "the trait std::clone::Clone is not implemented. So I removed "Clone" from the #[derive ... line and treated Clone like it was similar to an error because I did not know what else to do. And for some reason it worked but is this really the way to do it or was I just very lucky. The error code is below. Any assistance will be appreciated.

#[derive(Debug)]
pub enum CustomError  {
    SqlError(rusqlite::Error), 
    CsvError(csv::Error),
    IOError(std::io::Error),
    // if I add clone to #[derive line the above 3 items each kick out the same error
    // the error is: the trait std::clone::Clone in not implemented
    ChronoError(chrono::ParseError),
    Clone(),    
}

impl fmt::Display for CustomError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            CustomError::SqlError(error_msg) => write!(f, "SQL error {}", error_msg),
            CustomError::CsvError(error_msg) => write!(f, "CSV error {}", error_msg),
            CustomError::ChronoError(error_msg) => write!(f, "Chrono error {}", error_msg),
            CustomError::IOError(error_msg) => write!(f, "IO Error {}", error_msg),
            CustomError::Clone() => write!(f, "clone message"),
        }
    }
}
impl Clone for CustomError {
    fn clone(&self) -> Self {
        CustomError::Clone()
    }
}
impl std::error::Error for CustomError {}
impl From<rusqlite::Error> for CustomError {
    fn from(error: rusqlite::Error) -> Self {
        CustomError::SqlError(error)
    }
}
impl From<csv::Error> for CustomError {
    fn from(error: csv::Error) -> Self {
        CustomError::CsvError(error)
    }
}
impl From<chrono::ParseError> for CustomError {
    fn from(error: chrono::ParseError) -> Self {
        CustomError::ChronoError(error)
    }
}
impl From<std::io::Error> for CustomError {
    fn from(error: std::io::Error) -> Self {
        CustomError::IOError(error)
    }
}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed