From 4d10f8f0a7deb58f03c8b785c89e8744d9940cb2 Mon Sep 17 00:00:00 2001 From: Henrik Date: Thu, 2 Nov 2023 21:40:20 +0100 Subject: [PATCH 1/3] Implement Sample for byte arrays --- audio-core/src/sample.rs | 17 +++++++++++++++++ audio/src/tests/byte_arrays.rs | 11 +++++++++++ audio/src/tests/mod.rs | 1 + 3 files changed, 29 insertions(+) create mode 100644 audio/src/tests/byte_arrays.rs diff --git a/audio-core/src/sample.rs b/audio-core/src/sample.rs index 16ea18e..fdfc390 100644 --- a/audio-core/src/sample.rs +++ b/audio-core/src/sample.rs @@ -69,3 +69,20 @@ impl_int!(i64); impl_int!(i128); impl_int!(usize); impl_int!(isize); + + +// Helper macro to implement [Sample] for byte arrays. +macro_rules! impl_bytes { + ($bytes:expr) => { + unsafe impl Sample for [u8; $bytes] { + const ZERO: Self = [0; $bytes]; + } + }; +} + +// Implement for byte arrays of common lengths +impl_bytes!(2); +impl_bytes!(3); +impl_bytes!(4); +impl_bytes!(8); +impl_bytes!(16); diff --git a/audio/src/tests/byte_arrays.rs b/audio/src/tests/byte_arrays.rs new file mode 100644 index 0000000..f988e6e --- /dev/null +++ b/audio/src/tests/byte_arrays.rs @@ -0,0 +1,11 @@ +#[test] +fn test_byte_array() { + + let buf: crate::buf::Interleaved<[u8; 2]> = crate::interleaved![[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; + + assert_eq!(buf.channels(), 2); + assert_eq!(buf.sample(0, 0).unwrap(), [1, 2]); + assert_eq!(buf.sample(0, 1).unwrap(), [3, 4]); + assert_eq!(buf.sample(1, 0).unwrap(), [5, 6]); + assert_eq!(buf.sample(1, 1).unwrap(), [7, 8]); +} \ No newline at end of file diff --git a/audio/src/tests/mod.rs b/audio/src/tests/mod.rs index 38af571..a6528cc 100644 --- a/audio/src/tests/mod.rs +++ b/audio/src/tests/mod.rs @@ -3,3 +3,4 @@ mod dynamic; mod interleaved; mod io; mod sequential; +mod byte_arrays; From 02bbc2ba1dbde8f044c9626026a1d9c0a5b06167 Mon Sep 17 00:00:00 2001 From: Henrik Date: Thu, 2 Nov 2023 21:45:08 +0100 Subject: [PATCH 2/3] Run format --- audio-core/src/sample.rs | 1 - audio/src/tests/byte_arrays.rs | 6 +++--- audio/src/tests/mod.rs | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/audio-core/src/sample.rs b/audio-core/src/sample.rs index fdfc390..eff9002 100644 --- a/audio-core/src/sample.rs +++ b/audio-core/src/sample.rs @@ -70,7 +70,6 @@ impl_int!(i128); impl_int!(usize); impl_int!(isize); - // Helper macro to implement [Sample] for byte arrays. macro_rules! impl_bytes { ($bytes:expr) => { diff --git a/audio/src/tests/byte_arrays.rs b/audio/src/tests/byte_arrays.rs index f988e6e..d1fe5c6 100644 --- a/audio/src/tests/byte_arrays.rs +++ b/audio/src/tests/byte_arrays.rs @@ -1,11 +1,11 @@ #[test] fn test_byte_array() { - - let buf: crate::buf::Interleaved<[u8; 2]> = crate::interleaved![[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; + let buf: crate::buf::Interleaved<[u8; 2]> = + crate::interleaved![[[1, 2], [3, 4]], [[5, 6], [7, 8]]]; assert_eq!(buf.channels(), 2); assert_eq!(buf.sample(0, 0).unwrap(), [1, 2]); assert_eq!(buf.sample(0, 1).unwrap(), [3, 4]); assert_eq!(buf.sample(1, 0).unwrap(), [5, 6]); assert_eq!(buf.sample(1, 1).unwrap(), [7, 8]); -} \ No newline at end of file +} diff --git a/audio/src/tests/mod.rs b/audio/src/tests/mod.rs index a6528cc..f45f7b6 100644 --- a/audio/src/tests/mod.rs +++ b/audio/src/tests/mod.rs @@ -1,6 +1,6 @@ +mod byte_arrays; mod copy_channel; mod dynamic; mod interleaved; mod io; mod sequential; -mod byte_arrays; From b0711dc574cdcdee0851e89b2c139c5ffa10d3b1 Mon Sep 17 00:00:00 2001 From: Henrik Date: Sun, 5 Nov 2023 21:14:11 +0100 Subject: [PATCH 3/3] Change to a generic implementation for any array length, drop Default requirement --- audio-core/src/sample.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/audio-core/src/sample.rs b/audio-core/src/sample.rs index eff9002..aa6b872 100644 --- a/audio-core/src/sample.rs +++ b/audio-core/src/sample.rs @@ -10,7 +10,7 @@ /// /// Implementor must make sure that a bit-pattern of all-zeros is a legal /// bit-pattern for the implemented type. -pub unsafe trait Sample: Copy + Default { +pub unsafe trait Sample: Copy { /// The zero pattern for the sample. const ZERO: Self; } @@ -70,18 +70,7 @@ impl_int!(i128); impl_int!(usize); impl_int!(isize); -// Helper macro to implement [Sample] for byte arrays. -macro_rules! impl_bytes { - ($bytes:expr) => { - unsafe impl Sample for [u8; $bytes] { - const ZERO: Self = [0; $bytes]; - } - }; +// Implement for byte arrays of any length +unsafe impl Sample for [u8; N] { + const ZERO: Self = [0; N]; } - -// Implement for byte arrays of common lengths -impl_bytes!(2); -impl_bytes!(3); -impl_bytes!(4); -impl_bytes!(8); -impl_bytes!(16);