asbytes/
as_bytes.rs

1/// Define a private namespace for all its items.
2mod private
3{
4
5  pub use bytemuck::
6  {
7    Pod,
8  };
9
10  /// Trait for borrowing data as byte slices.
11  /// This trait abstracts the conversion of types that implement Pod (or collections thereof)
12  /// into their raw byte representation as a slice (`&[u8]`).
13
14  pub trait AsBytes
15  {
16
17    /// Returns the underlying byte slice of the data.
18    fn as_bytes( &self ) -> &[ u8 ]
19    ;
20
21    /// Returns an owned vector containing a copy of the bytes of the data.
22    /// The default implementation clones the bytes from `as_bytes()`.
23    #[ inline ]
24    fn to_bytes_vec( &self ) -> Vec< u8 >
25    {
26      self.as_bytes().to_vec()
27    }
28
29    /// Returns the size in bytes of the data.
30    #[ inline ]
31    fn byte_size( &self ) -> usize
32    {
33      self.as_bytes().len()
34    }
35
36    /// Returns the count of elements contained in the data.
37    /// For single-element tuples `(T,)`, this is 1.
38    /// For collections (`Vec<T>`, `&[T]`, `[T; N]`), this is the number of `T` items.
39    fn len( &self ) -> usize;
40
41  }
42
43  /// Implementation for single POD types wrapped in a tuple `(T,)`.
44
45  impl< T : Pod > AsBytes for ( T, )
46  {
47
48    #[ inline ]
49    fn as_bytes( &self ) -> &[ u8 ]
50    {
51      bytemuck::bytes_of( &self.0 )
52    }
53
54    #[ inline ]
55    fn byte_size( &self ) -> usize
56    {
57      std::mem::size_of::< T >()
58    }
59
60    #[ inline ]
61    fn len( &self ) -> usize
62    {
63      1
64    }
65
66  }
67
68  /// Implementation for Vec<T> where T is POD.
69
70  impl< T : Pod > AsBytes for Vec< T >
71  {
72
73    #[ inline ]
74    fn as_bytes( &self ) -> &[ u8 ]
75    {
76      bytemuck::cast_slice( self )
77    }
78
79    #[ inline ]
80    fn byte_size( &self ) -> usize
81    {
82      self.len() * std::mem::size_of::< T >()
83    }
84
85    #[ inline ]
86    fn len( &self ) -> usize
87    {
88      self.len()
89    }
90
91  }
92
93  /// Implementation for [T] where T is POD.
94
95  impl< T : Pod > AsBytes for [ T ]
96  {
97
98    #[ inline ]
99    fn as_bytes( &self ) -> &[ u8 ]
100    {
101      bytemuck::cast_slice( self )
102    }
103
104    #[ inline ]
105    fn byte_size( &self ) -> usize
106    {
107      self.len() * std::mem::size_of::< T >()
108    }
109
110    #[ inline ]
111    fn len( &self ) -> usize
112    {
113      self.len()
114    }
115
116  }
117
118  /// Implementation for [T; N] where T is POD.
119
120  impl< T : Pod, const N : usize > AsBytes for [ T ; N ]
121  {
122
123    #[ inline ]
124    fn as_bytes( &self ) -> &[ u8 ]
125    {
126      bytemuck::cast_slice( self )
127    }
128
129    #[ inline ]
130    fn byte_size( &self ) -> usize
131    {
132      N * std::mem::size_of::< T >()
133    }
134
135    #[ inline ]
136    fn len( &self ) -> usize
137    {
138      N
139    }
140
141  }
142
143}
144
145
146#[ doc( inline ) ]
147#[ allow( unused_imports ) ]
148pub use own::*;
149
150/// Own namespace of the module.
151
152#[ allow( unused_imports ) ]
153pub mod own
154{
155  use super::*;
156
157  #[ doc( inline ) ]
158  pub use orphan::*;
159
160}
161
162
163#[ doc( inline ) ]
164#[ allow( unused_imports ) ]
165pub use own::*;
166
167/// Orphan namespace of the module.
168
169#[ allow( unused_imports ) ]
170pub mod orphan
171{
172  use super::*;
173  #[ doc( inline ) ]
174  pub use exposed::*;
175
176}
177
178/// Exposed namespace of the module.
179
180#[ allow( unused_imports ) ]
181pub mod exposed
182{
183  use super::*;
184
185  #[ doc( inline ) ]
186  pub use prelude::*;
187
188}
189
190/// Prelude to use essentials: `use my_module::prelude::*`.
191
192#[ allow( unused_imports ) ]
193pub mod prelude
194{
195  use super::*;
196
197  pub use private::AsBytes;
198}