Commit b5c3c4c
authored
refactor!: update Pixel subtraits (#77)
Supersedes #72 which has become a bit of a mess.
I went to the std documentation to collect the traits implemented on
u8/u16:
- arithmetic: !, +, -, *, /, %
- bitwise: |, &, ^, <<, >>
- comparison: PartialEq, Eq, PartialOrd, Ord
- formatting: {} {:?} {:o} {:x} {:X} {:e} {:E} {:b}
- conversions: various (from/into, tryfrom/tryinto, FromStr)
- common misc: Clone, Default, Copy, Hash, Sum, Step, Product
- marker (nightly): ZeroablePrimitive, SimdElement, RangePattern,
AtomicPrimitive,
- nightly/wip misc: NumBufferTrait, FunnelShift, Distribution
- misc internals: DisjointBitOr, CarrylessMul, CarryingMulAdd
It's notable that a lot of operations on integers (`checked_*`,
`wrapping_*`, `unsigned_abs`, `abs_diff`, `log`, `pow` etc. etc. etc.)
don't live in a trait but are just type-inherent methods. Even
`num_traits` doesn't expose most of these, maybe because `std` has
steadily been growing over the years.
I see two main problems with keeping arithmetics/bitwise operations "in"
Pixel:
- maintenance burden, i.e. keeping up with std/num_traits adding new
stuff
- questionable usability of arithmetics given type size limitations (how
often do you actually want to work in a `u8`? any sum will quickly
overflow)
So I suggest **dropping arithmetic and bitwise operations** from the
Pixel trait entirely. This requires users to convert to some concrete
numeric type before "working" on pixels.
Because there is exactly one of each `TryFrom`, `TryInto`, `From` and
`Into` required by `T: Pixel`, types can be fully inferred even without
outside "help":
```rs
let a = pix.into(); // always u16
let b = pix.try_into().unwrap(); // always u8
let pix = T::from(x); // always u8
let pix = T::try_from(y).unwrap(); // always u16
```
If you want to widen a Pixel into an integer, you can do
`u64::from(pix.into())` (which is infallible so no panics ever). If you
want to narrow an integer into `Pixel`, you have to do it twice though:
`Pixel::try_from(my_u64.try_into().expect("fits into
u16")).expect("Pixel is u16")`.1 parent 88c3e13 commit b5c3c4c
3 files changed
Lines changed: 119 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | 12 | | |
16 | 13 | | |
17 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
24 | 106 | | |
25 | | - | |
26 | | - | |
| 107 | + | |
| 108 | + | |
27 | 109 | | |
28 | 110 | | |
29 | 111 | | |
| |||
41 | 123 | | |
42 | 124 | | |
43 | 125 | | |
| 126 | + | |
| 127 | + | |
44 | 128 | | |
45 | 129 | | |
46 | 130 | | |
| |||
56 | 140 | | |
57 | 141 | | |
58 | 142 | | |
59 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
60 | 173 | | |
61 | 174 | | |
62 | 175 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
331 | 331 | | |
332 | 332 | | |
333 | 333 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
348 | 337 | | |
349 | 338 | | |
350 | 339 | | |
| |||
0 commit comments