Skip to content

Commit

Permalink
Release v6.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Dec 6, 2023
1 parent 36f9d82 commit 79e8b2f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v6.2.2
- Improve documentation.

## v6.2.1
- Add `prefix_with` and `suffix_with`.
- Bump dependencies.
Expand Down
58 changes: 32 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,68 +341,74 @@ where
slice2array(slice).unwrap()
}

/// Prefixes the given element to the given slice to make it a fixed-size array of length `N`.
/// Prefixes the given element to the given array/slice/vector to make it a fixed-size array of
/// length `N`.
///
/// If the length of the slice is already equal to `N`, it returns the slice as a fixed-size array.
/// If the length of the slice is greater than `N`, it returns the first `N` elements of the slice
/// as a fixed-size array.
/// If the length of the slice is less than `N`, it creates a new fixed-size array of length `N` and
/// copies the slice into it, padding the remaining elements with the given element.
/// If the length of the array/slice/vector is already equal to `N`, it returns the
/// array/slice/vector as a fixed-size array.
/// If the length of the array/slice/vector is greater than `N`, it returns the first `N` elements
/// of the array/slice/vector as a fixed-size array.
/// If the length of the array/slice/vector is less than `N`, it creates a new fixed-size array of
/// length `N` and copies the array/slice/vector into it, padding the remaining elements with the
/// given element.
///
/// # Examples
/// ```
/// assert_eq!(array_bytes::prefix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
/// assert_eq!(array_bytes::prefix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
/// assert_eq!(array_bytes::prefix_with::<_, _, 5>([1, 2, 3], 0), [0, 0, 1, 2, 3]);
/// ```
pub fn prefix_with<S, T, const N: usize>(slice: S, element: T) -> [T; N]
pub fn prefix_with<A, T, const N: usize>(any: A, element: T) -> [T; N]
where
S: AsRef<[T]>,
A: AsRef<[T]>,
T: Copy,
{
let s = slice.as_ref();
let a = any.as_ref();

match s.len().cmp(&N) {
Ordering::Equal => slice2array_unchecked(s),
Ordering::Greater => slice2array_unchecked(&s[..N]),
match a.len().cmp(&N) {
Ordering::Equal => slice2array_unchecked(a),
Ordering::Greater => slice2array_unchecked(&a[..N]),
Ordering::Less => {
let mut padded = [element; N];

padded[N - s.len()..].copy_from_slice(s);
padded[N - a.len()..].copy_from_slice(a);

padded
},
}
}

/// Suffixes the given element to the given slice to make it a fixed-size array of length `N`.
/// Suffixes the given element to the given array/slice/vector to make it a fixed-size array of
/// length `N`.
///
/// If the length of the slice is already equal to `N`, it returns the slice as a fixed-size array.
/// If the length of the slice is greater than `N`, it returns the first `N` elements of the slice
/// as a fixed-size array. If the length of the slice is less than `N`, it creates a new fixed-size
/// array of length `N` and copies the slice into it, padding the remaining elements with the given
/// element.
/// If the length of the array/slice/vector is already equal to `N`, it returns the
/// array/slice/vector as a fixed-size array.
/// If the length of the array/slice/vector is greater than `N`, it returns the first `N` elements
/// of the array/slice/vector as a fixed-size array.
/// If the length of the array/slice/vector is less than `N`, it creates a new fixed-size array of
/// length `N` and copies the array/slice/vector into it, padding the remaining elements with the
/// given element.
///
/// # Examples
/// ```
/// assert_eq!(array_bytes::suffix_with::<_, _, 4>([1, 2, 3, 4], 0), [1, 2, 3, 4]);
/// assert_eq!(array_bytes::suffix_with::<_, _, 4>([1, 2, 3, 4, 5, 6], 0), [1, 2, 3, 4]);
/// assert_eq!(array_bytes::suffix_with::<_, _, 5>([1, 2, 3], 0), [1, 2, 3, 0, 0]);
/// ```
pub fn suffix_with<S, T, const N: usize>(slice: S, element: T) -> [T; N]
pub fn suffix_with<A, T, const N: usize>(any: A, element: T) -> [T; N]
where
S: AsRef<[T]>,
A: AsRef<[T]>,
T: Copy,
{
let s = slice.as_ref();
let a = any.as_ref();

match s.len().cmp(&N) {
Ordering::Equal => slice2array_unchecked(s),
Ordering::Greater => slice2array_unchecked(&s[..N]),
match a.len().cmp(&N) {
Ordering::Equal => slice2array_unchecked(a),
Ordering::Greater => slice2array_unchecked(&a[..N]),
Ordering::Less => {
let mut padded = [element; N];

padded[..s.len()].copy_from_slice(s);
padded[..a.len()].copy_from_slice(a);

padded
},
Expand Down

0 comments on commit 79e8b2f

Please sign in to comment.