You can in most cases implement the method in a way that is more general than the trait requires. Here's another, more subtle example:
struct S(String);
impl<'a> From<&'a str> for S {
// This accepts a `&'_ str` with any lifetime, not just an `&'a str`
// vvvv
fn from(s: &str) -> Self {
S(s.into())
}
}
But you can't actually call the method; the trait bounds are checked at the call site. RFC 3245 will allow opting in to "refined" methods,[1] in which case, you could call it.
Awesome reply, thank you very much!!
I'd read all RFCs related to where clauses I could find, and couldn't find any information about this.
So, a trait just establishes some "lower-bound", i.e., the minimum requirements implementors must put in, but if we somehow can make the impls more general, Rust will gladly accept them... Hummm, very interesting. Rust is smart in some unimaginable ways sometimes.
Would you say it is "more correct" to keep the exact where clause on all the implementations, or is it also good (or even better), to keep them simplified like this? I could remove them from several other commands...
In the specific case of Sized though, in addition to being highly unlikely, it's a breaking change to become not Sized anyway. So you're not actually opting into more guarantees.
I only just now found that out. I think I'll file an issue... ↩︎
Regarding the RPIT in traits, I've tried to put everything into the same module, but it still triggered a warning. Only when I removed the pub from the trait the warning was gone. An issue seems a nice idea.