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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use alloc::borrow::ToOwned;
use core::{default::Default, fmt};
use crate::{bytes::Bytes, error::VerificationResult, verification_error};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Byte([u8; 1]);
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ByteReader<'r>(&'r [u8]);
impl fmt::Debug for Byte {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}(0x{:02x})", Self::NAME, self.0[0])
}
}
impl<'r> fmt::Debug for ByteReader<'r> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}(0x{:02x})", Self::NAME, self.0[0])
}
}
impl fmt::Display for Byte {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}(0x{:02x})", Self::NAME, self.0[0])
}
}
impl<'r> fmt::Display for ByteReader<'r> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}(0x{:02x})", Self::NAME, self.0[0])
}
}
impl Default for Byte {
#[inline]
fn default() -> Self {
Self::new(0)
}
}
impl Byte {
pub const NAME: &'static str = "Byte";
#[inline]
pub fn new(v: u8) -> Self {
Byte([v; 1])
}
#[inline]
pub fn new_unchecked(data: Bytes) -> Self {
Byte::new(data[0])
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
&self.0[..]
}
#[inline]
pub fn as_bytes(self) -> Bytes {
self.as_slice().to_owned().into()
}
#[inline]
pub fn from_slice(slice: &[u8]) -> VerificationResult<Self> {
ByteReader::verify(slice, false).map(|_| Self::new(slice[0]))
}
#[inline]
pub fn from_compatible_slice(slice: &[u8]) -> VerificationResult<Self> {
ByteReader::verify(slice, true).map(|_| Self::new(slice[0]))
}
}
impl<'r> ByteReader<'r> {
pub const NAME: &'r str = "ByteReader";
#[inline]
pub fn to_entity(self) -> Byte {
Byte::new(self.0[0])
}
#[inline]
pub fn new_unchecked(slice: &'r [u8]) -> Self {
ByteReader(slice)
}
#[inline]
pub fn as_slice(&self) -> &'r [u8] {
self.0
}
#[inline]
pub fn verify(slice: &[u8], _compatible: bool) -> VerificationResult<()> {
let slice_len = slice.len();
if slice_len != 1 {
return verification_error!(Self, TotalSizeNotMatch, 1, slice_len);
}
Ok(())
}
#[inline]
pub fn from_slice(slice: &'r [u8]) -> VerificationResult<Self> {
Self::verify(slice, false).map(|_| Self::new_unchecked(slice))
}
#[inline]
pub fn from_compatible_slice(slice: &'r [u8]) -> VerificationResult<Self> {
Self::verify(slice, true).map(|_| Self::new_unchecked(slice))
}
}
impl Byte {
#[inline]
pub fn as_reader(&self) -> ByteReader<'_> {
ByteReader::new_unchecked(self.as_slice())
}
}
impl From<u8> for Byte {
#[inline]
fn from(v: u8) -> Self {
Byte::new(v)
}
}
impl From<Byte> for u8 {
#[inline]
fn from(v: Byte) -> Self {
v.0[0]
}
}
impl From<&Byte> for u8 {
#[inline]
fn from(v: &Byte) -> Self {
v.0[0]
}
}
impl From<ByteReader<'_>> for u8 {
#[inline]
fn from(v: ByteReader<'_>) -> Self {
v.0[0]
}
}
impl From<&ByteReader<'_>> for u8 {
#[inline]
fn from(v: &ByteReader<'_>) -> Self {
v.0[0]
}
}