@@ -249,6 +249,23 @@ impl<T: Default + FamStruct> FamStructWrapper<T> {
249
249
Ok ( FamStructWrapper { mem_allocator } )
250
250
}
251
251
252
+ /// Constructs a FamStructWrapper with an empty flexible array member
253
+ /// from the given FamStruct header.
254
+ ///
255
+ /// # Errors
256
+ ///
257
+ /// If the length stored in the header is not 0, returns [`Error::SizeLimitExceeded`]
258
+ pub fn from_header ( header : T ) -> Result < FamStructWrapper < T > , Error > {
259
+ if header. len ( ) != 0 {
260
+ return Err ( Error :: SizeLimitExceeded ) ;
261
+ }
262
+
263
+ // SAFETY: We are passing an array of length 1, which corresponds to exactly
264
+ // the header. The length inside the header is set to 0, and there are also no
265
+ // further elements in the vector that would constitute any T::Entry.
266
+ unsafe { Ok ( Self :: from_raw ( vec ! [ header] ) ) }
267
+ }
268
+
252
269
/// Create a new FamStructWrapper from a slice of elements.
253
270
///
254
271
/// # Arguments
@@ -965,6 +982,21 @@ mod tests {
965
982
assert ! ( adapter == adapter. clone( ) ) ;
966
983
}
967
984
985
+ #[ test]
986
+ fn test_from_header ( ) {
987
+ let header = MockFamStruct :: default ( ) ;
988
+ let wrapper = MockFamStructWrapper :: from_header ( header) . unwrap ( ) ;
989
+ assert_eq ! ( wrapper. len( ) , 0 ) ;
990
+ assert_eq ! ( wrapper. as_fam_struct_ref( ) . len, 0 ) ;
991
+
992
+ let header = MockFamStruct {
993
+ len : 100 ,
994
+ ..Default :: default ( )
995
+ } ;
996
+ let error = MockFamStructWrapper :: from_header ( header) ;
997
+ assert ! ( matches!( error, Err ( Error :: SizeLimitExceeded ) ) ) ;
998
+ }
999
+
968
1000
#[ test]
969
1001
fn test_raw_content ( ) {
970
1002
let data = vec ! [
0 commit comments