Implementing rand::fill for half::f16

⚓ Rust    📅 2025-07-24    👤 surdeus    👁️ 2      

surdeus

Hi,

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

This is in a local copy of the half crate.

Is there a way to implement what I need?

3 posts - 2 participants

Read full topic

🏷️ Rust_feed