fixed/
impl_arbitrary.rs

1// Copyright © 2018–2025 Trevor Spiteri
2
3// This library is free software: you can redistribute it and/or
4// modify it under the terms of either
5//
6//   * the Apache License, Version 2.0 or
7//   * the MIT License
8//
9// at your option.
10//
11// You should have recieved copies of the Apache License and the MIT
12// License along with the library. If not, see
13// <https://www.apache.org/licenses/LICENSE-2.0> and
14// <https://opensource.org/licenses/MIT>.
15
16use crate::types::extra::{LeEqU8, LeEqU16, LeEqU32, LeEqU64, LeEqU128};
17use crate::{
18    FixedI8, FixedI16, FixedI32, FixedI64, FixedI128, FixedU8, FixedU16, FixedU32, FixedU64,
19    FixedU128, Unwrapped, Wrapping,
20};
21use arbitrary::{Arbitrary, Result as ArbitraryResult, Unstructured};
22
23macro_rules! impl_trait {
24    ($Fixed:ident, $LeEqU:ident, $Inner:ident) => {
25        impl<'a, Frac> Arbitrary<'a> for $Fixed<Frac> {
26            #[inline]
27            fn arbitrary(u: &mut Unstructured<'a>) -> ArbitraryResult<Self> {
28                Ok(Self::from_bits(<$Inner as Arbitrary<'a>>::arbitrary(u)?))
29            }
30
31            #[inline]
32            fn size_hint(depth: usize) -> (usize, Option<usize>) {
33                <$Inner as Arbitrary<'a>>::size_hint(depth)
34            }
35        }
36
37        impl<'a, Frac: $LeEqU> Arbitrary<'a> for Wrapping<$Fixed<Frac>> {
38            #[inline]
39            fn arbitrary(u: &mut Unstructured<'a>) -> ArbitraryResult<Self> {
40                Ok(Self::from_bits(<$Inner as Arbitrary<'a>>::arbitrary(u)?))
41            }
42
43            #[inline]
44            fn size_hint(depth: usize) -> (usize, Option<usize>) {
45                <$Inner as Arbitrary<'a>>::size_hint(depth)
46            }
47        }
48
49        impl<'a, Frac: $LeEqU> Arbitrary<'a> for Unwrapped<$Fixed<Frac>> {
50            #[inline]
51            fn arbitrary(u: &mut Unstructured<'a>) -> ArbitraryResult<Self> {
52                Ok(Self::from_bits(<$Inner as Arbitrary<'a>>::arbitrary(u)?))
53            }
54
55            #[inline]
56            fn size_hint(depth: usize) -> (usize, Option<usize>) {
57                <$Inner as Arbitrary<'a>>::size_hint(depth)
58            }
59        }
60    };
61}
62
63impl_trait! { FixedI8, LeEqU8, i8 }
64impl_trait! { FixedI16, LeEqU16, i16 }
65impl_trait! { FixedI32, LeEqU32, i32 }
66impl_trait! { FixedI64, LeEqU64, i64 }
67impl_trait! { FixedI128, LeEqU128, i128 }
68impl_trait! { FixedU8, LeEqU8, u8 }
69impl_trait! { FixedU16, LeEqU16, u16 }
70impl_trait! { FixedU32, LeEqU32, u32 }
71impl_trait! { FixedU64, LeEqU64, u64 }
72impl_trait! { FixedU128, LeEqU128, u128 }