[go: up one dir, main page]

Implement Arbitrary trait for all Fixed types (optional feature)

When writing fuzz tests to test the stability of a crate, it is required that data types used within the fuzz test must implement the Arbitrary trait. The arbitrary crate provides a derivation macro #[derive(Arbitrary)] for the data structures struct and enum, but it is necessary that all types within implement the Arbitrary trait. Since the Fixed types don't implement this, they can become the single point of failure that denies the automatic implementation of the trait. This forces the developers to manually implement the trait on their data structures, which can become quite cumbersome. In my case, I have to manually implement the Arbitrary trait for my data structures multiple times for FixedI32<Frac>, FixedI64<Frac>, FixedI128<Frac> and the same for the unsigned variants. This leads to 6 implementations (using a macro) per data structure, or 3 in case only one variant (signed/unsigned) is used. Currently I have 3 data structures with Fixed Point numbers, one uses signed and unsigned Fixed point types. This leads to 12 implementations I have to implement manually, instead of using the derive macro 3 times.

There is a workaround, for one specific Fixed type one can just use the associated primitive (e.g. u32 for FixedU32) in data structure that is used within the fuzz tests and then initialize the custom structs by converting the primitive to the Fixed point type by using to_ne_bytes and from_ne_bytes. This is not as nice though, as having the Arbitrary type implemented for the custom structs and being able to use it directly within the data structure that is passed to the fuzz tests.

I propose to include an implementation of Arbitrary for any Fixed point type. If this is approved, I can implement it, since I already have the implementation on-hand. In this case, the implementations will only be added if the arbitrary feature is set.