3 ms·
Note that it's quite possible for a trait to have infinitely many implementations in any given program. For example, Debug is implemented by u64, Box<u64>, Box<
by MHordecki 8y ago
Note that it's quite possible for a trait to have infinitely many implementations in any given program. For example, Debug is implemented by u64, Box<u64>, Box<Box<u64>>, ..., and so on. While these happen to have the same size, it's not hard to imagine a situation where this is not the case.
- majewsky 8y agoYour situation is already sufficiently impossible because it shows that for a given trait, the set of implementors is often (countably) infinite.
- comex 8y agoHowever, the number of implementors actually instantiated in any given program is necessarily finite. You could write a program that would make it infinite, e.g. trait Foo { fn foo(); } impl<T> Foo for T { fn foo() { <Box<T> as Foo>::foo(); } } ...but the compiler can't compile it: since Rust implements generics solely through monomorphization, that would require generating an infinitely large binary :)
- oconnor663 8y ago> Rust implements generics solely through monomorphization That doesn't sound right. Trait objects use dynamic dispatch.