proc_bitfield/example/
support.rs1#[cfg(feature = "nightly")]
2use super::UnwrapBitsExample;
3use crate::conv::UnsafeFrom;
4pub use core::num::TryFromIntError;
5
6#[derive(Clone, Copy, Debug)]
9pub struct NonZeroU8(core::num::NonZeroU8);
10
11impl From<NonZeroU8> for u8 {
12 fn from(other: NonZeroU8) -> Self {
13 other.0.into()
14 }
15}
16
17impl TryFrom<u8> for NonZeroU8 {
18 type Error = TryFromIntError;
19
20 fn try_from(other: u8) -> Result<Self, Self::Error> {
21 Ok(NonZeroU8(other.try_into()?))
22 }
23}
24
25impl UnsafeFrom<u8> for NonZeroU8 {
26 unsafe fn unsafe_from(other: u8) -> Self {
27 NonZeroU8(core::num::NonZeroU8::new_unchecked(other))
28 }
29}
30
31#[derive(Clone, Copy, Debug)]
33pub struct U16(pub u16);
34
35impl UnsafeFrom<U16> for u8 {
36 unsafe fn unsafe_from(other: U16) -> Self {
37 other.0 as u8
38 }
39}
40
41impl TryFrom<U16> for u8 {
42 type Error = TryFromIntError;
43
44 fn try_from(other: U16) -> Result<Self, Self::Error> {
45 other.0.try_into()
46 }
47}
48
49#[derive(Clone, Copy, Debug)]
51pub struct U8WithParity {
52 pub raw: u8,
53 pub has_even_set_bits: bool,
54}
55
56impl From<u8> for U8WithParity {
57 fn from(other: u8) -> Self {
58 U8WithParity {
59 raw: other,
60 has_even_set_bits: other.count_ones() & 1 == 0,
61 }
62 }
63}
64
65impl From<U8WithParity> for u8 {
66 fn from(other: U8WithParity) -> Self {
67 other.raw
68 }
69}
70
71#[derive(Clone, Copy, Debug)]
73pub struct SpuriouslyFailingU8(u8);
74
75impl TryFrom<u8> for SpuriouslyFailingU8 {
76 type Error = ();
77
78 fn try_from(other: u8) -> Result<Self, Self::Error> {
79 Ok(SpuriouslyFailingU8(other))
82 }
83}
84
85impl UnsafeFrom<u8> for SpuriouslyFailingU8 {
86 unsafe fn unsafe_from(other: u8) -> Self {
87 SpuriouslyFailingU8(other)
88 }
89}
90
91impl TryFrom<SpuriouslyFailingU8> for u8 {
92 type Error = ();
93
94 fn try_from(other: SpuriouslyFailingU8) -> Result<Self, Self::Error> {
95 Ok(other.0)
97 }
98}
99
100impl UnsafeFrom<SpuriouslyFailingU8> for u8 {
101 unsafe fn unsafe_from(other: SpuriouslyFailingU8) -> Self {
102 other.0
103 }
104}
105
106#[cfg(feature = "nightly")]
107impl TryFrom<u8> for UnwrapBitsExample {
108 type Error = ();
109
110 fn try_from(other: u8) -> Result<Self, Self::Error> {
111 match NonZeroU8::try_from(other) {
112 Ok(v) => Ok(UnwrapBitsExample(v)),
113 Err(_) => Err(()),
114 }
115 }
116}
117
118#[cfg(feature = "nightly")]
119impl From<UnwrapBitsExample> for u8 {
120 fn from(other: UnwrapBitsExample) -> Self {
121 other.0.into()
122 }
123}