@@ -341,68 +341,74 @@ where
341
341
slice2array ( slice) . unwrap ( )
342
342
}
343
343
344
- /// Prefixes the given element to the given slice to make it a fixed-size array of length `N`.
344
+ /// Prefixes the given element to the given array/slice/vector to make it a fixed-size array of
345
+ /// length `N`.
345
346
///
346
- /// If the length of the slice is already equal to `N`, it returns the slice as a fixed-size array.
347
- /// If the length of the slice is greater than `N`, it returns the first `N` elements of the slice
348
- /// as a fixed-size array.
349
- /// If the length of the slice is less than `N`, it creates a new fixed-size array of length `N` and
350
- /// copies the slice into it, padding the remaining elements with the given element.
347
+ /// If the length of the array/slice/vector is already equal to `N`, it returns the
348
+ /// array/slice/vector as a fixed-size array.
349
+ /// If the length of the array/slice/vector is greater than `N`, it returns the first `N` elements
350
+ /// of the array/slice/vector as a fixed-size array.
351
+ /// If the length of the array/slice/vector is less than `N`, it creates a new fixed-size array of
352
+ /// length `N` and copies the array/slice/vector into it, padding the remaining elements with the
353
+ /// given element.
351
354
///
352
355
/// # Examples
353
356
/// ```
354
357
/// assert_eq!(array_bytes::prefix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
355
358
/// assert_eq!(array_bytes::prefix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
356
359
/// assert_eq!(array_bytes::prefix_with::<_, _, 5>([1, 2, 3], 0), [0, 0, 1, 2, 3]);
357
360
/// ```
358
- pub fn prefix_with < S , T , const N : usize > ( slice : S , element : T ) -> [ T ; N ]
361
+ pub fn prefix_with < A , T , const N : usize > ( any : A , element : T ) -> [ T ; N ]
359
362
where
360
- S : AsRef < [ T ] > ,
363
+ A : AsRef < [ T ] > ,
361
364
T : Copy ,
362
365
{
363
- let s = slice . as_ref ( ) ;
366
+ let a = any . as_ref ( ) ;
364
367
365
- match s . len ( ) . cmp ( & N ) {
366
- Ordering :: Equal => slice2array_unchecked ( s ) ,
367
- Ordering :: Greater => slice2array_unchecked ( & s [ ..N ] ) ,
368
+ match a . len ( ) . cmp ( & N ) {
369
+ Ordering :: Equal => slice2array_unchecked ( a ) ,
370
+ Ordering :: Greater => slice2array_unchecked ( & a [ ..N ] ) ,
368
371
Ordering :: Less => {
369
372
let mut padded = [ element; N ] ;
370
373
371
- padded[ N - s . len ( ) ..] . copy_from_slice ( s ) ;
374
+ padded[ N - a . len ( ) ..] . copy_from_slice ( a ) ;
372
375
373
376
padded
374
377
} ,
375
378
}
376
379
}
377
380
378
- /// Suffixes the given element to the given slice to make it a fixed-size array of length `N`.
381
+ /// Suffixes the given element to the given array/slice/vector to make it a fixed-size array of
382
+ /// length `N`.
379
383
///
380
- /// If the length of the slice is already equal to `N`, it returns the slice as a fixed-size array.
381
- /// If the length of the slice is greater than `N`, it returns the first `N` elements of the slice
382
- /// as a fixed-size array. If the length of the slice is less than `N`, it creates a new fixed-size
383
- /// array of length `N` and copies the slice into it, padding the remaining elements with the given
384
- /// element.
384
+ /// If the length of the array/slice/vector is already equal to `N`, it returns the
385
+ /// array/slice/vector as a fixed-size array.
386
+ /// If the length of the array/slice/vector is greater than `N`, it returns the first `N` elements
387
+ /// of the array/slice/vector as a fixed-size array.
388
+ /// If the length of the array/slice/vector is less than `N`, it creates a new fixed-size array of
389
+ /// length `N` and copies the array/slice/vector into it, padding the remaining elements with the
390
+ /// given element.
385
391
///
386
392
/// # Examples
387
393
/// ```
388
394
/// assert_eq!(array_bytes::suffix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
389
395
/// assert_eq!(array_bytes::suffix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
390
396
/// assert_eq!(array_bytes::suffix_with::<_, _, 5>([1, 2, 3], 0), [1, 2, 3, 0, 0]);
391
397
/// ```
392
- pub fn suffix_with < S , T , const N : usize > ( slice : S , element : T ) -> [ T ; N ]
398
+ pub fn suffix_with < A , T , const N : usize > ( any : A , element : T ) -> [ T ; N ]
393
399
where
394
- S : AsRef < [ T ] > ,
400
+ A : AsRef < [ T ] > ,
395
401
T : Copy ,
396
402
{
397
- let s = slice . as_ref ( ) ;
403
+ let a = any . as_ref ( ) ;
398
404
399
- match s . len ( ) . cmp ( & N ) {
400
- Ordering :: Equal => slice2array_unchecked ( s ) ,
401
- Ordering :: Greater => slice2array_unchecked ( & s [ ..N ] ) ,
405
+ match a . len ( ) . cmp ( & N ) {
406
+ Ordering :: Equal => slice2array_unchecked ( a ) ,
407
+ Ordering :: Greater => slice2array_unchecked ( & a [ ..N ] ) ,
402
408
Ordering :: Less => {
403
409
let mut padded = [ element; N ] ;
404
410
405
- padded[ ..s . len ( ) ] . copy_from_slice ( s ) ;
411
+ padded[ ..a . len ( ) ] . copy_from_slice ( a ) ;
406
412
407
413
padded
408
414
} ,
0 commit comments