Being a beginner in Rust programming I'm a bit confused by the Size trait in conjunction with generic traits. My custom trait defines a single method that takes two objects and combines them into a new one. Because the original objects expire after combination, the method should take ownership of the parameters. I also return a result with a custom error type because the combination might fail and I want to give the caller the reason for the failure.
The trait in question is:
pub trait Combinable<T> {
fn combine(self, other: T) -> CombinationResult<T>;
}
struct CombinationError;
type CombinationResult<T> = std::result::Result<dyn Combinable<T>, CombinationError>
When compiling this code I get the following error:
error[E0277]: the size for values of type `(dyn Combinable<T> + 'static)` cannot be known at compilation time
|
7 | fn combine(self, other: T) -> CombinationResult<T>;
| ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
241 | pub enum Result<T, E> {
| - required by this bound in `std::result::Result`
|
= help: the trait `Sized` is not implemented for `(dyn Combinable<T> + 'static)`
I tried constraining the generic type parameter T to Sized
(i.e. pub trait Combinable<T: Sized>
) but that didn't work. I also tried the same at the type alias declaration, also to no avail.
Am I missing/overlooking something?