opcua_types/
basic_types.rs1use std::io::{Read, Write};
7
8use crate::encoding::*;
9
10impl BinaryEncoder<bool> for bool {
27 fn byte_len(&self) -> usize {
28 1
29 }
30
31 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
32 write_u8(stream, if *self { 1 } else { 0 })
34 }
35
36 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
37 Ok(read_u8(stream)? == 1)
38 }
39}
40
41impl BinaryEncoder<i8> for i8 {
42 fn byte_len(&self) -> usize {
43 1
44 }
45
46 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
47 write_u8(stream, *self as u8)
48 }
49
50 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
51 Ok(read_u8(stream)? as i8)
52 }
53}
54
55impl BinaryEncoder<u8> for u8 {
57 fn byte_len(&self) -> usize {
58 1
59 }
60
61 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
62 write_u8(stream, *self)
63 }
64
65 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
66 read_u8(stream)
67 }
68}
69
70impl BinaryEncoder<i16> for i16 {
72 fn byte_len(&self) -> usize {
73 2
74 }
75
76 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
77 write_i16(stream, *self)
78 }
79
80 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
81 read_i16(stream)
82 }
83}
84
85impl BinaryEncoder<u16> for u16 {
87 fn byte_len(&self) -> usize {
88 2
89 }
90
91 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
92 write_u16(stream, *self)
93 }
94
95 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
96 read_u16(stream)
97 }
98}
99
100impl BinaryEncoder<i32> for i32 {
102 fn byte_len(&self) -> usize {
103 4
104 }
105
106 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
107 write_i32(stream, *self)
108 }
109
110 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
111 read_i32(stream)
112 }
113}
114
115impl BinaryEncoder<u32> for u32 {
117 fn byte_len(&self) -> usize {
118 4
119 }
120
121 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
122 write_u32(stream, *self)
123 }
124
125 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
126 read_u32(stream)
127 }
128}
129
130impl BinaryEncoder<i64> for i64 {
132 fn byte_len(&self) -> usize {
133 8
134 }
135
136 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
137 write_i64(stream, *self)
138 }
139
140 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
141 read_i64(stream)
142 }
143}
144
145impl BinaryEncoder<u64> for u64 {
147 fn byte_len(&self) -> usize {
148 8
149 }
150
151 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
152 write_u64(stream, *self)
153 }
154
155 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
156 read_u64(stream)
157 }
158}
159
160impl BinaryEncoder<f32> for f32 {
162 fn byte_len(&self) -> usize {
163 4
164 }
165
166 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
167 write_f32(stream, *self)
168 }
169
170 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
171 read_f32(stream)
172 }
173}
174
175impl BinaryEncoder<f64> for f64 {
177 fn byte_len(&self) -> usize {
178 8
179 }
180
181 fn encode<S: Write>(&self, stream: &mut S) -> EncodingResult<usize> {
182 write_f64(stream, *self)
183 }
184
185 fn decode<S: Read>(stream: &mut S, _: &DecodingOptions) -> EncodingResult<Self> {
186 read_f64(stream)
187 }
188}