Combining Err with associated type


#1

I want to combine two errors into one: one is from an associated type, the other one from a concrete function result.

I have a trait definition similar to this pseudo-rusty one below:

struct Error_1 {};

trait WithMixedErrors {
    type Error_2;
    
    fn impl_defined<U>(&self, val: U) -> Result<(), Self::Error>;
    
    // How can I express this ?E?
    fn operation<T>(&self, var: T) -> Result<(), ?E?> {
        // Some operation returning a Result<U, Error_1>
        let val = f(var)?;
        // The implementor defined function returning Error_2
        self.impl_defined(val)?
    }
}

I have a feeling the only way to get this to work is to get the implementor to take care of this and provide a From<Error_1> for SuperError. Unfortunately, my implementors are currently also generics with T: SomeTrait, where SomeTrait has that Error_2 above. :persevere:

EDIT: Thinking about this some more, I believe I should be good with this one:

enum CombinedError<T: SomeTrait> {
    Error_1(Error_1),
    Error_2(T::Error),
}

#2

Your options are:

trait WithMixedErrors<E>

which makes type of the trait dependent on the type of the error. There can be many variants of the trait, but WithMixedErrors<UserError1> will be incompatible with WithMixedErrors<UserError2>.

or

 fn operation<T, E>(&self, var: T) -> Result<(), E>

which makes it independent of the trait, but OTOH keeps E completely generic, so the type will be unusable in the function.

The enum is good.