This fails, unsurprisingly:
struct DynamicallySized([u8]);
trait ByValue {
fn take(self);
}
/// a.rs:10:13: 10:17 error: the trait `core::marker::Sized` is not implemented for the type `[u8]` [E0277]
/// a.rs:10 fn take(self) {}
/// ^~~~
impl ByValue for DynamicallySized {
fn take(self) {}
}
This as well:
/// a.rs:19:13: 19:17 error: the trait `core::marker::Sized` is not implemented for the type `Self` [E0277]
/// a.rs:19 fn take(self) {}
/// ^~~~
trait ByValueDefault {
fn take(self) {}
}
Now this is getting interesting: with a default method and a Self: Sized
bound, we can implement a trait with a method that takes self
by value for an dynamically-sized type. (std::iter::Iterator
does this.) Though of course the type doesn’t have that particular method. (It still has other methods in the trait.) Still, in this case the default method is kinda pointless.
trait ByValueDefaultWhereSized {
fn take(self) where Self: Sized {}
}
impl ByValueDefaultWhereSized for DynamicallySized {
}
Can we get rid of it? Doing so understandably errors, but maybe it shouldn’t.
trait ByValueWhereSized {
fn take(self) where Self: Sized;
}
/// a.rs:30:1: 31:2 error: not all trait items implemented, missing: `take` [E0046]
impl ByValueWhereSized for DynamicallySized {
}
Language change proposal: when a non-default trait method has bounds which are not satisfied for a particular impl, defining that method in that impl should not be required (since it would not be available anyway).