You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
where Self: Sized on trait method doesn't work as expected when implementing for unsized types. #57900
That said, I still feel that the approximation of using Sized instead of some NotDyn trait should be fixed; said trait could be a supertrait of Sized for backwards compatibility. The reason (well, beyond semantics) being, sometimes you do want to call some generic method on a str implementation (imagine the method was -> () in the example).
That said, I still feel that the approximation of using Sized instead of some NotDyn trait should be fixed; said trait could be a supertrait of Sized for backwards compatibility. The reason (well, beyond semantics) being, sometimes you do want to call some generic method on a str implementation (imagine the method was -> () in the example).
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 Self: Sized and trait objects. It's not like I did not understand why and how trait objects are unsized, but rather that I had the feeling that requiring Self: Sized is too strong when all one wants is excluding trait objects. However, since this point was never discussed, I thought that it is my understanding that must be flawed.
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.
Activity
RustyYato commentedon Jan 27, 2019
As a work around, you can split off everything that needs
Sizedinto another trait. like socrlf0710 commentedon Jan 29, 2019
Yes, i think this is a reasonable workaround. But it will be nice to get this limitation removed
RustyYato commentedon Jan 29, 2019
Yes, I agree, this is an annoying limitation.
crlf0710 commentedon Dec 24, 2019
@rustbot modify labels to +A-traits +C-bug
QuineDot commentedon May 7, 2022
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
Sizedinstead of someNotDyntrait should be fixed; said trait could be a supertrait ofSizedfor backwards compatibility. The reason (well, beyond semantics) being, sometimes you do want to call some generic method on astrimplementation (imagine the method was-> ()in the example).kupiakos commentedon Jun 8, 2022
For reference, you can also work around this on stable with an unused HRTB:
grothesque commentedon Nov 30, 2023
@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
Self: Sizedand trait objects. It's not like I did not understand why and how trait objects are unsized, but rather that I had the feeling that requiringSelf: Sizedis too strong when all one wants is excluding trait objects. However, since this point was never discussed, I thought that it is my understanding that must be flawed.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.
cyrgani commentedon Jun 25, 2025
This was fixed by #135480, meaning that
compiles sucessfully now.