@@ -2203,12 +2203,10 @@ pub unsafe trait FromBytes: FromZeros {
2203
2203
2204
2204
/// Interprets the prefix of the given `bytes` as a `&Self` without copying.
2205
2205
///
2206
- /// `ref_from_prefix` returns a reference to the first `size_of::<Self>()`
2207
- /// bytes of `bytes`. If `bytes.len() < size_of::<Self>()` or `bytes` is not
2208
- /// aligned to `align_of::<Self>()`, this returns `None`.
2209
- ///
2210
- /// To also access the prefix bytes, use [`Ref::new_from_prefix`]. Then, use
2211
- /// [`Ref::into_ref`] to get a `&Self` with the same lifetime.
2206
+ /// This method returns both a reference to the first `size_of::<Self>()`
2207
+ /// bytes of `bytes` interpreted as `Self`, and a reference to the remaining
2208
+ /// bytes. If `bytes.len() < size_of::<Self>()` or `bytes` is not aligned to
2209
+ /// `align_of::<Self>()`, this returns `None`.
2212
2210
///
2213
2211
/// # Examples
2214
2212
///
@@ -2235,31 +2233,31 @@ pub unsafe trait FromBytes: FromZeros {
2235
2233
/// // These are more bytes than are needed to encode a `Packet`.
2236
2234
/// let bytes = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14][..];
2237
2235
///
2238
- /// let packet = Packet::ref_from_prefix(bytes).unwrap();
2236
+ /// let ( packet, excess) = Packet::ref_from_prefix(bytes).unwrap();
2239
2237
///
2240
2238
/// assert_eq!(packet.header.src_port, [0, 1]);
2241
2239
/// assert_eq!(packet.header.dst_port, [2, 3]);
2242
2240
/// assert_eq!(packet.header.length, [4, 5]);
2243
2241
/// assert_eq!(packet.header.checksum, [6, 7]);
2244
2242
/// assert_eq!(packet.body, [[8, 9], [10, 11], [12, 13]]);
2243
+ /// assert_eq!(excess, &[14u8][..]);
2245
2244
/// ```
2246
2245
#[ must_use = "has no side effects" ]
2247
2246
#[ inline]
2248
- fn ref_from_prefix ( bytes : & [ u8 ] ) -> Option < & Self >
2247
+ fn ref_from_prefix ( bytes : & [ u8 ] ) -> Option < ( & Self , & [ u8 ] ) >
2249
2248
where
2250
2249
Self : KnownLayout + NoCell ,
2251
2250
{
2252
- Ref :: < & [ u8 ] , Self > :: bikeshed_new_from_prefix_known_layout ( bytes) . map ( |( r, _) | r. into_ref ( ) )
2251
+ Ref :: < & [ u8 ] , Self > :: bikeshed_new_from_prefix_known_layout ( bytes)
2252
+ . map ( |( r, s) | ( r. into_ref ( ) , s) )
2253
2253
}
2254
2254
2255
2255
/// Interprets the suffix of the given `bytes` as a `&Self` without copying.
2256
2256
///
2257
- /// `ref_from_suffix` returns a reference to the last `size_of::<Self>()`
2258
- /// bytes of `bytes`. If `bytes.len() < size_of::<Self>()` or the suffix of
2259
- /// `bytes` is not aligned to `align_of::<Self>()`, this returns `None`.
2260
- ///
2261
- /// To also access the suffix bytes, use [`Ref::new_from_suffix`]. Then, use
2262
- /// [`Ref::into_ref`] to get a `&Self` with the same lifetime.
2257
+ /// This method returns both a reference to the last `size_of::<Self>()`
2258
+ /// bytes of `bytes` interpreted as `Self`, and a reference to the preceding
2259
+ /// bytes. If `bytes.len() < size_of::<Self>()` or the suffix of `bytes` is
2260
+ /// not aligned to `align_of::<Self>()`, this returns `None`.
2263
2261
///
2264
2262
/// # Examples
2265
2263
///
@@ -2276,17 +2274,19 @@ pub unsafe trait FromBytes: FromZeros {
2276
2274
/// // These are more bytes than are needed to encode a `PacketTrailer`.
2277
2275
/// let bytes = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..];
2278
2276
///
2279
- /// let trailer = PacketTrailer::ref_from_suffix(bytes).unwrap();
2277
+ /// let (prefix, trailer) = PacketTrailer::ref_from_suffix(bytes).unwrap();
2280
2278
///
2279
+ /// assert_eq!(prefix, &[0, 1, 2, 3, 4, 5][..]);
2281
2280
/// assert_eq!(trailer.frame_check_sequence, [6, 7, 8, 9]);
2282
2281
/// ```
2283
2282
#[ must_use = "has no side effects" ]
2284
2283
#[ inline]
2285
- fn ref_from_suffix ( bytes : & [ u8 ] ) -> Option < & Self >
2284
+ fn ref_from_suffix ( bytes : & [ u8 ] ) -> Option < ( & [ u8 ] , & Self ) >
2286
2285
where
2287
2286
Self : NoCell + KnownLayout ,
2288
2287
{
2289
- Ref :: < & [ u8 ] , Self > :: bikeshed_new_from_suffix_known_layout ( bytes) . map ( |( _, r) | r. into_ref ( ) )
2288
+ Ref :: < & [ u8 ] , Self > :: bikeshed_new_from_suffix_known_layout ( bytes)
2289
+ . map ( |( p, r) | ( p, r. into_ref ( ) ) )
2290
2290
}
2291
2291
2292
2292
/// Interprets the given `bytes` as a `&mut Self` without copying.
@@ -2335,12 +2335,10 @@ pub unsafe trait FromBytes: FromZeros {
2335
2335
/// Interprets the prefix of the given `bytes` as a `&mut Self` without
2336
2336
/// copying.
2337
2337
///
2338
- /// `mut_from_prefix` returns a reference to the first `size_of::<Self>()`
2339
- /// bytes of `bytes`. If `bytes.len() < size_of::<Self>()` or `bytes` is not
2340
- /// aligned to `align_of::<Self>()`, this returns `None`.
2341
- ///
2342
- /// To also access the prefix bytes, use [`Ref::new_from_prefix`]. Then, use
2343
- /// [`Ref::into_mut`] to get a `&mut Self` with the same lifetime.
2338
+ /// This method returns both a reference to the first `size_of::<Self>()`
2339
+ /// bytes of `bytes` interpreted as `Self`, and a reference to the remaining
2340
+ /// bytes. If `bytes.len() < size_of::<Self>()` or `bytes` is not aligned to
2341
+ /// `align_of::<Self>()`, this returns `None`.
2344
2342
///
2345
2343
/// # Examples
2346
2344
///
@@ -2360,35 +2358,36 @@ pub unsafe trait FromBytes: FromZeros {
2360
2358
/// // These are more bytes than are needed to encode a `PacketHeader`.
2361
2359
/// let bytes = &mut [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..];
2362
2360
///
2363
- /// let header = PacketHeader::mut_from_prefix(bytes).unwrap();
2361
+ /// let ( header, body) = PacketHeader::mut_from_prefix(bytes).unwrap();
2364
2362
///
2365
2363
/// assert_eq!(header.src_port, [0, 1]);
2366
2364
/// assert_eq!(header.dst_port, [2, 3]);
2367
2365
/// assert_eq!(header.length, [4, 5]);
2368
2366
/// assert_eq!(header.checksum, [6, 7]);
2367
+ /// assert_eq!(body, &[8, 9][..]);
2369
2368
///
2370
2369
/// header.checksum = [0, 0];
2370
+ /// body.fill(1);
2371
2371
///
2372
- /// assert_eq!(bytes, [0, 1, 2, 3, 4, 5, 0, 0, 8, 9 ]);
2372
+ /// assert_eq!(bytes, [0, 1, 2, 3, 4, 5, 0, 0, 1, 1 ]);
2373
2373
/// ```
2374
2374
#[ must_use = "has no side effects" ]
2375
2375
#[ inline]
2376
- fn mut_from_prefix ( bytes : & mut [ u8 ] ) -> Option < & mut Self >
2376
+ fn mut_from_prefix ( bytes : & mut [ u8 ] ) -> Option < ( & mut Self , & mut [ u8 ] ) >
2377
2377
where
2378
2378
Self : IntoBytes + KnownLayout + NoCell ,
2379
2379
{
2380
2380
Ref :: < & mut [ u8 ] , Self > :: bikeshed_new_from_prefix_known_layout ( bytes)
2381
- . map ( |( r, _ ) | r. into_mut ( ) )
2381
+ . map ( |( r, s ) | ( r. into_mut ( ) , s ) )
2382
2382
}
2383
2383
2384
- /// Interprets the suffix of the given `bytes` as a `&mut Self` without copying.
2385
- ///
2386
- /// `mut_from_suffix` returns a reference to the last `size_of::<Self>()`
2387
- /// bytes of `bytes`. If `bytes.len() < size_of::<Self>()` or the suffix of
2388
- /// `bytes` is not aligned to `align_of::<Self>()`, this returns `None`.
2384
+ /// Interprets the suffix of the given `bytes` as a `&mut Self` without
2385
+ /// copying.
2389
2386
///
2390
- /// To also access the suffix bytes, use [`Ref::new_from_suffix`]. Then,
2391
- /// use [`Ref::into_mut`] to get a `&mut Self` with the same lifetime.
2387
+ /// This method returns both a reference to the last `size_of::<Self>()`
2388
+ /// bytes of `bytes` interpreted as `Self`, and a reference to the preceding
2389
+ /// bytes. If `bytes.len() < size_of::<Self>()` or the suffix of `bytes` is
2390
+ /// not aligned to `align_of::<Self>()`, this returns `None`.
2392
2391
///
2393
2392
/// # Examples
2394
2393
///
@@ -2405,34 +2404,35 @@ pub unsafe trait FromBytes: FromZeros {
2405
2404
/// // These are more bytes than are needed to encode a `PacketTrailer`.
2406
2405
/// let bytes = &mut [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..];
2407
2406
///
2408
- /// let trailer = PacketTrailer::mut_from_suffix(bytes).unwrap();
2407
+ /// let (prefix, trailer) = PacketTrailer::mut_from_suffix(bytes).unwrap();
2409
2408
///
2409
+ /// assert_eq!(prefix, &[0u8, 1, 2, 3, 4, 5][..]);
2410
2410
/// assert_eq!(trailer.frame_check_sequence, [6, 7, 8, 9]);
2411
2411
///
2412
- /// trailer.frame_check_sequence = [0, 0, 0, 0];
2412
+ /// prefix.fill(0);
2413
+ /// trailer.frame_check_sequence.fill(1);
2413
2414
///
2414
- /// assert_eq!(bytes, [0, 1, 2, 3, 4, 5, 0, 0, 0, 0 ]);
2415
+ /// assert_eq!(bytes, [0, 0, 0, 0, 0, 0, 1, 1, 1, 1 ]);
2415
2416
/// ```
2416
2417
#[ must_use = "has no side effects" ]
2417
2418
#[ inline]
2418
- fn mut_from_suffix ( bytes : & mut [ u8 ] ) -> Option < & mut Self >
2419
+ fn mut_from_suffix ( bytes : & mut [ u8 ] ) -> Option < ( & mut [ u8 ] , & mut Self ) >
2419
2420
where
2420
2421
Self : IntoBytes + KnownLayout + NoCell ,
2421
2422
{
2422
2423
Ref :: < & mut [ u8 ] , Self > :: bikeshed_new_from_suffix_known_layout ( bytes)
2423
- . map ( |( _ , r) | r. into_mut ( ) )
2424
+ . map ( |( p , r) | ( p , r. into_mut ( ) ) )
2424
2425
}
2425
2426
2426
2427
/// Interprets the prefix of the given `bytes` as a `&[Self]` with length
2427
2428
/// equal to `count` without copying.
2428
2429
///
2429
- /// This method verifies that `bytes.len() >= size_of::<T>() * count`
2430
- /// and that `bytes` is aligned to `align_of::<T>()`. It consumes the
2431
- /// first `size_of::<T>() * count` bytes from `bytes` to construct a
2432
- /// `&[Self]`, and returns the remaining bytes to the caller. It also
2433
- /// ensures that `sizeof::<T>() * count` does not overflow a `usize`.
2434
- /// If any of the length, alignment, or overflow checks fail, it returns
2435
- /// `None`.
2430
+ /// This method verifies that `bytes.len() >= size_of::<T>() * count` and
2431
+ /// that `bytes` is aligned to `align_of::<T>()`. It reinterprets the first
2432
+ /// `size_of::<T>() * count` bytes from `bytes` to construct a `&[Self]`,
2433
+ /// and returns the remaining bytes to the caller. It also ensures that
2434
+ /// `sizeof::<T>() * count` does not overflow a `usize`. If any of the
2435
+ /// length, alignment, or overflow checks fail, it returns `None`.
2436
2436
///
2437
2437
/// # Panics
2438
2438
///
@@ -2478,13 +2478,12 @@ pub unsafe trait FromBytes: FromZeros {
2478
2478
/// Interprets the suffix of the given `bytes` as a `&[Self]` with length
2479
2479
/// equal to `count` without copying.
2480
2480
///
2481
- /// This method verifies that `bytes.len() >= size_of::<T>() * count`
2482
- /// and that `bytes` is aligned to `align_of::<T>()`. It consumes the
2483
- /// last `size_of::<T>() * count` bytes from `bytes` to construct a
2484
- /// `&[Self]`, and returns the preceding bytes to the caller. It also
2485
- /// ensures that `sizeof::<T>() * count` does not overflow a `usize`.
2486
- /// If any of the length, alignment, or overflow checks fail, it returns
2487
- /// `None`.
2481
+ /// This method verifies that `bytes.len() >= size_of::<T>() * count` and
2482
+ /// that `bytes` is aligned to `align_of::<T>()`. It reinterprets the last
2483
+ /// `size_of::<T>() * count` bytes from `bytes` to construct a `&[Self]`,
2484
+ /// and returns the preceding bytes to the caller. It also ensures that
2485
+ /// `sizeof::<T>() * count` does not overflow a `usize`. If any of the
2486
+ /// length, alignment, or overflow checks fail, it returns `None`.
2488
2487
///
2489
2488
/// # Panics
2490
2489
///
@@ -2579,16 +2578,15 @@ pub unsafe trait FromBytes: FromZeros {
2579
2578
Ref :: < _ , [ Self ] > :: new ( bytes) . map ( |r| r. into_mut ( ) )
2580
2579
}
2581
2580
2582
- /// Interprets the prefix of the given `bytes` as a `&mut [Self]` with length
2583
- /// equal to `count` without copying.
2581
+ /// Interprets the prefix of the given `bytes` as a `&mut [Self]` with
2582
+ /// length equal to `count` without copying.
2584
2583
///
2585
- /// This method verifies that `bytes.len() >= size_of::<T>() * count`
2586
- /// and that `bytes` is aligned to `align_of::<T>()`. It consumes the
2587
- /// first `size_of::<T>() * count` bytes from `bytes` to construct a
2588
- /// `&[Self]`, and returns the remaining bytes to the caller. It also
2589
- /// ensures that `sizeof::<T>() * count` does not overflow a `usize`.
2590
- /// If any of the length, alignment, or overflow checks fail, it returns
2591
- /// `None`.
2584
+ /// This method verifies that `bytes.len() >= size_of::<T>() * count` and
2585
+ /// that `bytes` is aligned to `align_of::<T>()`. It reinterprets the first
2586
+ /// `size_of::<T>() * count` bytes from `bytes` to construct a `&[Self]`,
2587
+ /// and returns the remaining bytes to the caller. It also ensures that
2588
+ /// `sizeof::<T>() * count` does not overflow a `usize`. If any of the
2589
+ /// length, alignment, or overflow checks fail, it returns `None`.
2592
2590
///
2593
2591
/// # Panics
2594
2592
///
@@ -2623,8 +2621,9 @@ pub unsafe trait FromBytes: FromZeros {
2623
2621
/// assert_eq!(rest, &[8, 9]);
2624
2622
///
2625
2623
/// pixels[1] = Pixel { r: 0, g: 0, b: 0, a: 0 };
2624
+ /// rest.fill(1);
2626
2625
///
2627
- /// assert_eq!(bytes, [0, 1, 2, 3, 0, 0, 0, 0, 8, 9 ]);
2626
+ /// assert_eq!(bytes, [0, 1, 2, 3, 0, 0, 0, 0, 1, 1 ]);
2628
2627
/// ```
2629
2628
#[ must_use = "has no side effects" ]
2630
2629
#[ inline]
@@ -2638,13 +2637,12 @@ pub unsafe trait FromBytes: FromZeros {
2638
2637
/// Interprets the suffix of the given `bytes` as a `&mut [Self]` with length
2639
2638
/// equal to `count` without copying.
2640
2639
///
2641
- /// This method verifies that `bytes.len() >= size_of::<T>() * count`
2642
- /// and that `bytes` is aligned to `align_of::<T>()`. It consumes the
2643
- /// last `size_of::<T>() * count` bytes from `bytes` to construct a
2644
- /// `&[Self]`, and returns the preceding bytes to the caller. It also
2645
- /// ensures that `sizeof::<T>() * count` does not overflow a `usize`.
2646
- /// If any of the length, alignment, or overflow checks fail, it returns
2647
- /// `None`.
2640
+ /// This method verifies that `bytes.len() >= size_of::<T>() * count` and
2641
+ /// that `bytes` is aligned to `align_of::<T>()`. It reinterprets the last
2642
+ /// `size_of::<T>() * count` bytes from `bytes` to construct a `&[Self]`,
2643
+ /// and returns the preceding bytes to the caller. It also ensures that
2644
+ /// `sizeof::<T>() * count` does not overflow a `usize`. If any of the
2645
+ /// length, alignment, or overflow checks fail, it returns `None`.
2648
2646
///
2649
2647
/// # Panics
2650
2648
///
@@ -2678,9 +2676,10 @@ pub unsafe trait FromBytes: FromZeros {
2678
2676
/// Pixel { r: 6, g: 7, b: 8, a: 9 },
2679
2677
/// ]);
2680
2678
///
2679
+ /// rest.fill(9);
2681
2680
/// pixels[1] = Pixel { r: 0, g: 0, b: 0, a: 0 };
2682
2681
///
2683
- /// assert_eq!(bytes, [0, 1 , 2, 3, 4, 5, 0, 0, 0, 0]);
2682
+ /// assert_eq!(bytes, [9, 9 , 2, 3, 4, 5, 0, 0, 0, 0]);
2684
2683
/// ```
2685
2684
#[ must_use = "has no side effects" ]
2686
2685
#[ inline]
@@ -7846,12 +7845,21 @@ mod tests {
7846
7845
suffix. 0 = 0x0101010101010101 ;
7847
7846
// The `[u8:9]` is a non-half size of the full buffer, which would catch
7848
7847
// `from_prefix` having the same implementation as `from_suffix` (issues #506, #511).
7849
- assert_eq ! ( <[ u8 ; 9 ] >:: ref_from_suffix( & buf. t[ ..] ) . unwrap( ) , & [ 7u8 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ) ;
7850
- let suffix = AU64 :: mut_from_suffix ( & mut buf. t [ 1 ..] ) . unwrap ( ) ;
7848
+ assert_eq ! (
7849
+ <[ u8 ; 9 ] >:: ref_from_suffix( & buf. t[ ..] ) . unwrap( ) ,
7850
+ ( & [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] [ ..] , & [ 7u8 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] )
7851
+ ) ;
7852
+ let ( prefix, suffix) = AU64 :: mut_from_suffix ( & mut buf. t [ 1 ..] ) . unwrap ( ) ;
7853
+ assert_eq ! ( prefix, & mut [ 1u8 , 2 , 3 , 4 , 5 , 6 , 7 ] [ ..] ) ;
7851
7854
suffix. 0 = 0x0202020202020202 ;
7852
- <[ u8 ; 10 ] >:: mut_from_suffix ( & mut buf. t [ ..] ) . unwrap ( ) [ 0 ] = 42 ;
7853
- assert_eq ! ( <[ u8 ; 9 ] >:: ref_from_prefix( & buf. t[ ..] ) . unwrap( ) , & [ 0 , 1 , 2 , 3 , 4 , 5 , 42 , 7 , 2 ] ) ;
7854
- <[ u8 ; 2 ] >:: mut_from_prefix ( & mut buf. t [ ..] ) . unwrap ( ) [ 1 ] = 30 ;
7855
+ let ( prefix, suffix) = <[ u8 ; 10 ] >:: mut_from_suffix ( & mut buf. t [ ..] ) . unwrap ( ) ;
7856
+ assert_eq ! ( prefix, & mut [ 0u8 , 1 , 2 , 3 , 4 , 5 ] [ ..] ) ;
7857
+ suffix[ 0 ] = 42 ;
7858
+ assert_eq ! (
7859
+ <[ u8 ; 9 ] >:: ref_from_prefix( & buf. t[ ..] ) . unwrap( ) ,
7860
+ ( & [ 0u8 , 1 , 2 , 3 , 4 , 5 , 42 , 7 , 2 ] , & [ 2u8 , 2 , 2 , 2 , 2 , 2 , 2 ] [ ..] )
7861
+ ) ;
7862
+ <[ u8 ; 2 ] >:: mut_from_prefix ( & mut buf. t [ ..] ) . unwrap ( ) . 0 [ 1 ] = 30 ;
7855
7863
assert_eq ! ( buf. t, [ 0 , 30 , 2 , 3 , 4 , 5 , 42 , 7 , 2 , 2 , 2 , 2 , 2 , 2 , 2 , 2 ] ) ;
7856
7864
}
7857
7865
0 commit comments