1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
use crate::conv::UnsafeFrom;
use core::num::TryFromIntError;
pub struct NonZeroU8(core::num::NonZeroU8);
impl From<NonZeroU8> for u8 {
fn from(other: NonZeroU8) -> Self {
other.0.into()
}
}
impl TryFrom<u8> for NonZeroU8 {
type Error = TryFromIntError;
fn try_from(other: u8) -> Result<Self, Self::Error> {
NonZeroU8(other.try_into())
}
}
impl UnsafeFrom<u8> for NonZeroU8 {
unsafe fn unsafe_from(other: u8) -> Self {
NonZeroU8(core::num::NonZeroU8::new_unchecked(other))
}
}
pub struct U16(pub u16);
impl UnsafeFrom<U16> for u8 {
unsafe fn unsafe_from(other: U16) -> Self {
other.0 as u8
}
}
pub struct U8WithParity {
pub raw: u8,
pub has_even_set_bits: bool,
}
impl From<u8> for U8WithParity {
fn from(other: u8) -> Self {
U8WithParity {
raw: other,
has_even_set_bits: other.count_ones() & 1 == 0,
}
}
}
impl From<U8WithParity> for u8 {
fn from(other: U8WithParity) -> Self {
other.raw
}
}
pub struct SpuriouslyFailingU8(u8);
impl TryFrom<u8> for SpuriouslyFailingU8 {
type Error = ();
fn try_from(other: u8) -> Result<Self, Self::Error> {
Ok(SpuriouslyFailingU8(other))
}
}
impl UnsafeFrom<u8> for SpuriouslyFailingU8 {
unsafe fn unsafe_from(other: u8) -> Self {
SpuriouslyFailingU8(other)
}
}
impl TryFrom<SpuriouslyFailingU8> for u8 {
type Error = ();
fn try_from(other: SpuriouslyFailingU8) -> Result<Self, Self::Error> {
Ok(other.0)
}
}
impl UnsafeFrom<SpuriouslyFailingU8> for u8 {
unsafe fn unsafe_from(other: SpuriouslyFailingU8) -> Self {
other.0
}
}