According to object safety, if a method of a trait has bound
Self: Sized, then this method would be non-dispatchable via a trait object, and such a method won't prevent creating a trait object of that trait.
It seems that
Iterator::take matches the description above, whose signature is:
fn take(self, n: usize) -> Take<Self>
So in theory, we cannot call
Iterator::take on a pointer to
dyn Iterator like
Box<dyn Iterator> or
&mut dyn Iterator.
But actually we can, as the playground shown. So why can we invoke
Iterator::take on its trait object, but can't invoke
I have found a thread in this forum, as the answer said:
The stdlib solved this final problem by implementing
&mut dyn Iteratorby reimplementing all the methods in terms of
next, which is object safe and thus included in the trait object.
But I didn't find corresponding implementation in the implementors of Iterator. And directly search for sth like
impl Iterator for &mut dyn matches nothing in the
library/ directory of rust-lang repo. So what is the true answer of this problem? Specifically, if such implementation does exist, where can I find their source code? I'm really interested in how to implement
take and other methods like this without any additional data structures and solely rely on