@@ -21,9 +21,11 @@ type Result<T> = result::Result<T, ()>;
21
21
/// - have no padding
22
22
pub unsafe trait Pod : Copy + ' static { }
23
23
24
- /// Cast a byte slice to a `Pod` type.
24
+ /// Cast the head of a byte slice to a `Pod` type.
25
25
///
26
- /// Returns the type and the tail of the slice.
26
+ /// Returns the type and the tail of the byte slice.
27
+ ///
28
+ /// Returns an error if the byte slice is too short or the alignment is invalid.
27
29
#[ inline]
28
30
pub fn from_bytes < T : Pod > ( data : & [ u8 ] ) -> Result < ( & T , & [ u8 ] ) > {
29
31
let size = mem:: size_of :: < T > ( ) ;
@@ -39,9 +41,11 @@ pub fn from_bytes<T: Pod>(data: &[u8]) -> Result<(&T, &[u8])> {
39
41
Ok ( ( val, tail) )
40
42
}
41
43
42
- /// Cast a mutable byte slice to a `Pod` type.
44
+ /// Cast the head of a mutable byte slice to a `Pod` type.
45
+ ///
46
+ /// Returns the type and the tail of the byte slice.
43
47
///
44
- /// Returns the type and the tail of the slice .
48
+ /// Returns an error if the byte slice is too short or the alignment is invalid .
45
49
#[ inline]
46
50
pub fn from_bytes_mut < T : Pod > ( data : & mut [ u8 ] ) -> Result < ( & mut T , & mut [ u8 ] ) > {
47
51
let size = mem:: size_of :: < T > ( ) ;
@@ -60,9 +64,11 @@ pub fn from_bytes_mut<T: Pod>(data: &mut [u8]) -> Result<(&mut T, &mut [u8])> {
60
64
Ok ( ( val, tail) )
61
65
}
62
66
63
- /// Cast a byte slice to a slice of a `Pod` type.
67
+ /// Cast the head of a byte slice to a slice of a `Pod` type.
64
68
///
65
69
/// Returns the type slice and the tail of the byte slice.
70
+ ///
71
+ /// Returns an error if the byte slice is too short or the alignment is invalid.
66
72
#[ inline]
67
73
pub fn slice_from_bytes < T : Pod > ( data : & [ u8 ] , count : usize ) -> Result < ( & [ T ] , & [ u8 ] ) > {
68
74
let size = count. checked_mul ( mem:: size_of :: < T > ( ) ) . ok_or ( ( ) ) ?;
@@ -78,9 +84,11 @@ pub fn slice_from_bytes<T: Pod>(data: &[u8], count: usize) -> Result<(&[T], &[u8
78
84
Ok ( ( slice, tail) )
79
85
}
80
86
81
- /// Cast a mutable byte slice to a slice of a `Pod` type.
87
+ /// Cast the head of a mutable byte slice to a slice of a `Pod` type.
82
88
///
83
89
/// Returns the type slice and the tail of the byte slice.
90
+ ///
91
+ /// Returns an error if the byte slice is too short or the alignment is invalid.
84
92
#[ inline]
85
93
pub fn slice_from_bytes_mut < T : Pod > (
86
94
data : & mut [ u8 ] ,
@@ -102,6 +110,38 @@ pub fn slice_from_bytes_mut<T: Pod>(
102
110
Ok ( ( slice, tail) )
103
111
}
104
112
113
+ /// Cast all of a byte slice to a slice of a `Pod` type.
114
+ ///
115
+ /// Returns the type slice.
116
+ ///
117
+ /// Returns an error if the size of the byte slice is not an exact multiple
118
+ /// of the type size, or the alignment is invalid.
119
+ #[ inline]
120
+ pub fn slice_from_all_bytes < T : Pod > ( data : & [ u8 ] ) -> Result < & [ T ] > {
121
+ let count = data. len ( ) / mem:: size_of :: < T > ( ) ;
122
+ let ( slice, tail) = slice_from_bytes ( data, count) ?;
123
+ if !tail. is_empty ( ) {
124
+ return Err ( ( ) ) ;
125
+ }
126
+ Ok ( slice)
127
+ }
128
+
129
+ /// Cast all of a byte slice to a slice of a `Pod` type.
130
+ ///
131
+ /// Returns the type slice.
132
+ ///
133
+ /// Returns an error if the size of the byte slice is not an exact multiple
134
+ /// of the type size, or the alignment is invalid.
135
+ #[ inline]
136
+ pub fn slice_from_all_bytes_mut < T : Pod > ( data : & mut [ u8 ] ) -> Result < & mut [ T ] > {
137
+ let count = data. len ( ) / mem:: size_of :: < T > ( ) ;
138
+ let ( slice, tail) = slice_from_bytes_mut ( data, count) ?;
139
+ if !tail. is_empty ( ) {
140
+ return Err ( ( ) ) ;
141
+ }
142
+ Ok ( slice)
143
+ }
144
+
105
145
/// Cast a `Pod` type to a byte slice.
106
146
#[ inline]
107
147
pub fn bytes_of < T : Pod > ( val : & T ) -> & [ u8 ] {
0 commit comments