Why is Iterator object-safe?

Trait objects don't implement Sized, so if you require Self: Sized in a method it won't be included in the trait object. This also means it won't be considered when checking if the trait is object-safe, but note that this also means you won't be able to call it on a trait object. The stdlib solved this final problem by implementing Iterator for Box<dyn Iterator> and &mut dyn Iterator by reimplementing all the methods in terms of next, which is object safe and thus included in the trait object.

4 Likes