-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
where Self: Sized
on trait method doesn't work as expected when implementing for unsized types.
#57900
Comments
As a work around, you can split off everything that needs trait ObjectSafe {
// some functions
}
trait NotObjectSafe: ObjectSafe + Sized {
// more functions
} |
Yes, i think this is a reasonable workaround. But it will be nice to get this limitation removed |
Yes, I agree, this is an annoying limitation. |
@rustbot modify labels to +A-traits +C-bug |
The original example can be worked around with the trivial bounds RFC (you still cannot call the implementation). That said, I still feel that the approximation of using |
For reference, you can also work around this on stable with an unused HRTB: trait Foo {
fn bar(&self) -> Self where Self: Sized;
}
impl Foo for str {
fn bar(&self) -> Self where for<'a> str: Sized { unimplemented!() }
} |
@QuineDot wrote:
Perhaps this approximation is not a huge problem in practice when using the language, but in my experience it can be a big problem when learning about trait objects in Rust. It was one of the big blockers for me. Although I read all the available official documentation (and many other texts) on trait objects and object safety, I kept being fundamentally confused about the relation of It's only once I found @QuineDot's analysis that my doubts disappeared and I realized that my understanding was actually correct. (Thanks!) I assume that I am not the first or last to suffer from this difficulty, and suggest tackling it in some way. One could start by adding clarification to the definition of object safety. |
I've got this code
the error is:
However, since
str
is notSized
, i don't thinkbar
needs to be implemented. if i remove the "//", i'll got:So although this trait fits object safety rules, i still can't implement it for unsized types.
The text was updated successfully, but these errors were encountered: