Skip to content

Commit

Permalink
Implement Sample for byte arrays (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
HEnquist authored Nov 6, 2023
1 parent dae9e00 commit fe5e782
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion audio-core/src/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -69,3 +69,8 @@ impl_int!(i64);
impl_int!(i128);
impl_int!(usize);
impl_int!(isize);

// Implement for byte arrays of any length
unsafe impl<const N: usize> Sample for [u8; N] {
const ZERO: Self = [0; N];
}
11 changes: 11 additions & 0 deletions audio/src/tests/byte_arrays.rs
Original file line number Diff line number Diff line change
@@ -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]);
}
1 change: 1 addition & 0 deletions audio/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod byte_arrays;
mod copy_channel;
mod dynamic;
mod interleaved;
Expand Down

0 comments on commit fe5e782

Please sign in to comment.