I'm doing some experiments with the f16 type from the half crate, and I ended up needing to implement the Fill trait from the rand crate for [f16].
I'm aware that because of the orphan rule, I can't implement this on my own crate, so I was planning to open a PR with the code I needed. I soon ran into this error:
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
--> /home/andy/dev/projects/half-rs/src/rand_distr.rs:96:1
|
96 | impl rand::Fill for [f16] {
| ^^^^^^^^^^^^^^^^^^^^-----
| |
| this is not defined in the current crate because slices are always foreign
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
That doesn't feel very Rusty. So if I want to implement that trait for any arbitrary type I own, I have to go the provider of that trait and add my crate as a feature/dependency on the crate that defines that trait?
Unfortunately rand::Fill just doesn’t seem to be designed to be an extension point for third-party crates to impl it for [CustomType] – it’s only possible for CustomType (which would presumably be some collection-like type). A trait for filling [CustomType] would have to look like something like