diff --git a/CHANGELOG b/CHANGELOG index 02f4512..f9015e5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +## v6.2.2 +- Improve documentation. + ## v6.2.1 - Add `prefix_with` and `suffix_with`. - Bump dependencies. diff --git a/Cargo.lock b/Cargo.lock index 8c15ac4..2d3f1f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,7 +25,7 @@ checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" [[package]] name = "array-bytes" -version = "6.2.1" +version = "6.2.2" dependencies = [ "criterion", "faster-hex", diff --git a/Cargo.toml b/Cargo.toml index d543650..53f8254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ license = "Apache-2.0/GPL-3.0" name = "array-bytes" readme = "README.md" repository = "https://github.com/hack-ink/array-bytes" -version = "6.2.1" +version = "6.2.2" [profile.ci-dev] incremental = false diff --git a/src/lib.rs b/src/lib.rs index 1c80655..02bf78e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -341,13 +341,16 @@ 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 /// ``` @@ -355,33 +358,36 @@ where /// 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(slice: S, element: T) -> [T; N] +pub fn prefix_with(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 /// ``` @@ -389,20 +395,20 @@ where /// 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(slice: S, element: T) -> [T; N] +pub fn suffix_with(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 },