Repository navigation
CHANGE: Allow Fill to be implemented for third-party types #1650
Description
Activity
I did try to PR
impl Fill for f16to half. The error message on the issue description is what the compiler says when building a local copy of the half crate with the impl. As you said, the only way right now is to add aArrayForeignType([ForeignType])if you want to mirror the Fill implementations on rand, which is unfortunate.Sorry, I should have read the URLO thread.
I think in general you may be right, but implementing for
[f16]seems wrong (we don't even implement for[f32], because you can't just bit-cast a randomu32to af32and expect a useful result).In my case it happens to be useful, as I'm intentionally generating garbage data just for performance testing purposes.
I just realized I didn't mention this here or in the URLO thread, but the reason why I wanted specifically the
impl Fill for [f16]and not aimpl Fill for WrapperTypeis that I have in my performance testing harness I have a bunch of functions whose signatures look like this:pub fn test_params(config: &LinearConfig) -> LinearParams<'static, T> where [T]: rand::Fill { }I was trying to ascertain when the
&mut [Self]receiver type was supported; I can't find this documented though1.41has some mention of receiver types and1.43stabilised support for some others. In any case, 1.63 supports this so there is no reason we couldn't switch theFilltrait in the next breaking release.In my case it happens to be useful, as I'm intentionally generating garbage data just for performance testing purposes.
In this case I would expect the
half::f16maintainers to reject such a feature addition.I think what I'm going to do is change the
where [T]: rand::Fillbound towhere StandardUniform: Distribution<T>and I'll fill the data manually.Reacted by Diggory Hardy(we don't even implement for
[f32], because you can't just bit-cast a randomu32to af32and expect a useful result).Reacted by Andres O. VelaFair call-out — and it contradicts the docs on
fn fill. Those impls should be removed in my opinion (they function differently to other impls and are easy to write an ad-hoc impl for).Reacted by Richard Neumann and Benjamin Lieser
Today I learned that it is not possible to implement
Fillfor third-party types, in the form of[T].I ran into this when I tried implementing
Fillfor[half::f16](See f16)Note that I was trying to open a PR on the half crate to implement
Fillforf16andbf16.Details
I opened a thread in URLO and someone suggested that this would be feasible if the function signature of
fillwas:Motivation
It is currently not possible to implement this trait for third party types. If I wanted to have
Fillimplemented forhalf::f16, I'd have to addhalfas a dependency onrand, and gate it with ahalffeature. This doesn't scale.Alternatives
Fill my
[f16]using a different API? However, in my case, I'm working with APIs whose function signature looks like thiswhich still has the same issue, even I filled the
[f16]with a different API.