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
/***************************************************************************
*
* 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
///
/// ```ignore
/// use osal_rs::traits::BytesHasLen;
///
/// let data: [u8; 4] = [1, 2, 3, 4];
/// assert_eq!(data.len(), 4);
/// assert!(!data.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
///
/// ```ignore
/// use osal_rs::traits::Serialize;
///
/// struct SensorData {
/// temperature: i16,
/// humidity: u8,
/// }
///
/// impl Serialize for SensorData {
/// fn to_bytes(&self) -> &[u8] {
/// // Convert struct to bytes
/// // Safety: ensure proper memory layout
/// unsafe {
/// core::slice::from_raw_parts(
/// self as *const Self as *const u8,
/// core::mem::size_of::<Self>()
/// )
/// }
/// }
/// }
/// ```
/// 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
///
/// ```ignore
/// use osal_rs::traits::Deserialize;
/// use osal_rs::utils::Result;
///
/// struct SensorData {
/// temperature: i16,
/// humidity: u8,
/// }
///
/// impl Deserialize for SensorData {
/// fn from_bytes(bytes: &[u8]) -> Result<Self> {
/// if bytes.len() < 3 {
/// return Err(Error::InvalidParameter);
/// }
/// Ok(SensorData {
/// temperature: i16::from_le_bytes([bytes[0], bytes[1]]),
/// humidity: bytes[2],
/// })
/// }
/// }
/// ```