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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/***************************************************************************
*
* osal-rs
* Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <https://www.gnu.org/licenses/>.
*
***************************************************************************/
//! Byte conversion traits for serialization and deserialization.
//!
//! This module provides traits for converting types to and from byte arrays,
//! enabling type-safe serialization for queue and communication operations.
use Serialize;
use crateResult;
/// Trait for types that have a known byte length.
///
/// Used to determine the size of data structures when working with byte arrays.
///
/// # Examples
///
/// ```
/// use osal_rs::os::*;
///
/// struct Reading {
/// temperature: i16,
/// humidity: u8,
/// }
///
/// impl BytesHasLen for Reading {
/// fn len(&self) -> usize {
/// core::mem::size_of::<i16>() + core::mem::size_of::<u8>()
/// }
/// }
///
/// let reading = Reading { temperature: 235, humidity: 65 };
/// assert_eq!(reading.len(), 3);
/// assert!(!reading.is_empty());
/// ```
/// Automatic implementation of `BytesHasLen` for fixed-size arrays.
///
/// This allows arrays of types implementing `Serialize` to automatically
/// report their size.
/// Trait for converting types to byte slices.
///
/// Enables serialization of structured data for transmission through
/// queues or other byte-oriented communication channels.
///
/// # Safety
///
/// When implementing this trait, ensure that the returned byte slice
/// is a valid representation of the type and lives at least as long
/// as the value itself.
///
/// # Examples
///
/// ```
/// use osal_rs::os::*;
///
/// // `repr(C)` pins the field order, so the bytes handed out below are a
/// // stable representation rather than whatever layout the compiler picks.
/// #[repr(C)]
/// struct SensorData {
/// temperature: i16,
/// humidity: u8,
/// }
///
/// impl Serialize for SensorData {
/// fn to_bytes(&self) -> &[u8] {
/// // Safety: the slice borrows `self`, so it cannot outlive it, and
/// // `size_of::<Self>()` bytes starting at `self` are always readable.
/// unsafe {
/// core::slice::from_raw_parts(
/// self as *const Self as *const u8,
/// core::mem::size_of::<Self>()
/// )
/// }
/// }
/// }
///
/// let data = SensorData { temperature: 235, humidity: 65 };
/// let bytes = data.to_bytes();
///
/// assert_eq!(bytes.len(), core::mem::size_of::<SensorData>());
/// assert_eq!(&bytes[..2], &235i16.to_ne_bytes());
/// assert_eq!(bytes[2], 65);
/// ```
/// Trait for deserializing types from byte slices.
///
/// Enables reconstruction of structured data from byte arrays received
/// from queues or communication channels.
///
/// # Errors
///
/// Implementations should return an error if:
/// - The byte slice is too small or too large
/// - The data is invalid or corrupted
/// - The conversion fails for any other reason
///
/// # Examples
///
/// ```
/// use osal_rs::os::*;
/// use osal_rs::utils::{Error, Result};
///
/// #[derive(Debug, PartialEq)]
/// struct SensorData {
/// temperature: i16,
/// humidity: u8,
/// }
///
/// impl Deserialize for SensorData {
/// fn from_bytes(bytes: &[u8]) -> Result<Self> {
/// if bytes.len() < 3 {
/// return Err(Error::OutOfIndex);
/// }
/// Ok(SensorData {
/// temperature: i16::from_le_bytes([bytes[0], bytes[1]]),
/// humidity: bytes[2],
/// })
/// }
/// }
///
/// let data = SensorData::from_bytes(&[0xEB, 0x00, 65]).unwrap();
/// assert_eq!(data, SensorData { temperature: 235, humidity: 65 });
///
/// // Too short to hold both fields.
/// assert!(SensorData::from_bytes(&[0xEB, 0x00]).is_err());
/// ```