Skip to content

Commit

Permalink
hybrid-array: remove ByteArray alias (#995)
Browse files Browse the repository at this point in the history
It's longer than `Array<u8, _>` and provides little value other than
needless indirection.

Closes #987
  • Loading branch information
tarcieri authored Dec 30, 2023
1 parent 143f342 commit 1f75c62
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions hybrid-array/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,6 @@ fn check_slice_length<T, U: ArraySize>(slice: &[T]) -> Result<(), TryFromSliceEr
Ok(())
}

/// Byte array type.
pub type ByteArray<U> = Array<u8, U>;

/// Array operations which are const generic over a given array size.
pub trait ArrayOps<T, const N: usize>:
AsRef<[T; N]>
Expand Down
28 changes: 14 additions & 14 deletions hybrid-array/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use hybrid_array::{Array, ByteArray};
use hybrid_array::Array;
use typenum::{U0, U2, U3, U4, U6, U7};

const EXAMPLE_SLICE: &[u8] = &[1, 2, 3, 4, 5, 6];
Expand All @@ -11,38 +11,38 @@ fn clone_from_slice() {

#[test]
fn tryfrom_slice_for_array() {
assert!(ByteArray::<U0>::try_from(EXAMPLE_SLICE).is_err());
assert!(ByteArray::<U3>::try_from(EXAMPLE_SLICE).is_err());
assert!(Array::<u8, U0>::try_from(EXAMPLE_SLICE).is_err());
assert!(Array::<u8, U3>::try_from(EXAMPLE_SLICE).is_err());

let array_ref = ByteArray::<U6>::try_from(EXAMPLE_SLICE).expect("slice contains 6 bytes");
let array_ref = Array::<u8, U6>::try_from(EXAMPLE_SLICE).expect("slice contains 6 bytes");
assert_eq!(&*array_ref, EXAMPLE_SLICE);

assert!(ByteArray::<U7>::try_from(EXAMPLE_SLICE).is_err());
assert!(Array::<u8, U7>::try_from(EXAMPLE_SLICE).is_err());
}

#[test]
fn tryfrom_slice_for_array_ref() {
assert!(<&ByteArray<U0>>::try_from(EXAMPLE_SLICE).is_err());
assert!(<&ByteArray::<U3>>::try_from(EXAMPLE_SLICE).is_err());
assert!(<&Array<u8, U0>>::try_from(EXAMPLE_SLICE).is_err());
assert!(<&Array::<u8, U3>>::try_from(EXAMPLE_SLICE).is_err());

let array_ref = <&ByteArray<U6>>::try_from(EXAMPLE_SLICE).expect("slice contains 6 bytes");
let array_ref = <&Array<u8, U6>>::try_from(EXAMPLE_SLICE).expect("slice contains 6 bytes");
assert_eq!(array_ref.as_slice(), EXAMPLE_SLICE);

assert!(<&ByteArray::<U7>>::try_from(EXAMPLE_SLICE).is_err());
assert!(<&Array::<u8, U7>>::try_from(EXAMPLE_SLICE).is_err());
}

#[test]
fn concat() {
let prefix = ByteArray::<U2>::clone_from_slice(&EXAMPLE_SLICE[..2]);
let suffix = ByteArray::<U4>::clone_from_slice(&EXAMPLE_SLICE[2..]);
let prefix = Array::<u8, U2>::clone_from_slice(&EXAMPLE_SLICE[..2]);
let suffix = Array::<u8, U4>::clone_from_slice(&EXAMPLE_SLICE[2..]);

let array = prefix.concat(suffix);
assert_eq!(array.as_slice(), EXAMPLE_SLICE);
}

#[test]
fn split() {
let array = ByteArray::<U6>::clone_from_slice(EXAMPLE_SLICE);
let array = Array::<u8, U6>::clone_from_slice(EXAMPLE_SLICE);

let (prefix, suffix) = array.split::<U2>();

Expand All @@ -52,7 +52,7 @@ fn split() {

#[test]
fn split_ref() {
let array = ByteArray::<U6>::clone_from_slice(EXAMPLE_SLICE);
let array = Array::<u8, U6>::clone_from_slice(EXAMPLE_SLICE);

let (prefix, suffix) = array.split_ref::<U3>();

Expand All @@ -62,7 +62,7 @@ fn split_ref() {

#[test]
fn split_ref_mut() {
let array = &mut ByteArray::<U6>::clone_from_slice(EXAMPLE_SLICE);
let array = &mut Array::<u8, U6>::clone_from_slice(EXAMPLE_SLICE);

let (prefix, suffix) = array.split_ref_mut::<U4>();

Expand Down

0 comments on commit 1f75c62

Please sign in to comment.