For some reason, the Rust compiler is complaining that Result
doesn't implement unwrap
, even though the Error type I provided does implement Debug. The code that's error-ing is provided below.
use std::fmt::{Display, Debug};
use std::error::Error;
trait MyError: Error + Display + Debug {}
type MyResult<T> = Result<T, MyError>;
trait Foo: Clone {}
trait MyTrait {
fn my_function<T: Foo>(&self) -> MyResult<T>;
fn unwrap_function<T: Foo>(&self) -> T {
self.my_function().unwrap()
}
}
When you define your type
You actually define your type to be an unsized type as
MyError
is not a concrete type, but a trait. But, the implementation ofResult<T, E>
statesWhich implicitly requires
E
to be a sized type. Thus in your case, as it is not, the implementation is invalid and unavailable (actually, most if not all of the implementation ofResult<T, E>
requiresT
andE
to be sized, so an unsizedResult
is not very useful).The simplest fix in your case is to put your error in a
Box
, like this: