opcua_types/
basic_types.rs

1// OPCUA for Rust
2// SPDX-License-Identifier: MPL-2.0
3// Copyright (C) 2017-2022 Adam Lock
4
5//! Contains definitions of the simple OPC UA scalar types.
6use std::io::{Read, Write};
7
8use crate::encoding::*;
9
10// OPC UA Part 6 - Mappings 1.03 Specification
11
12// Standard UA types onto Rust types:
13
14// Boolean  -> bool
15// SByte    -> i8
16// Byte     -> u8
17// Int16    -> i16
18// UInt16   -> u16
19// Int32    -> i32
20// UInt32   -> u32
21// Int64    -> i64
22// UInt64   -> u64
23// Float    -> f32
24// Double   -> f64
25
26impl 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        // 0, or 1 for true or false, single byte
33        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
55/// An unsigned byt integer value between 0 and 255.
56impl 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
70/// A signed integer value between −32768 and 32767.
71impl 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
85/// An unsigned integer value between 0 and 65535.
86impl 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
100/// A signed integer value between −2147483648 and 2147483647.
101impl 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
115/// An unsigned integer value between 0 and 4294967295.
116impl 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
130/// A signed integer value between −9223372036854775808 and 9223372036854775807.
131impl 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
145/// An unsigned integer value between 0 and 18446744073709551615.
146impl 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
160/// An IEEE single precision (32 bit) floating point value.
161impl 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
175/// An IEEE double precision (64 bit) floating point value.
176impl 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}