audio_core/exact_size_buf.rs
1use crate::Buf;
2
3/// Trait used to describe a buffer that knows exactly how many frames it has
4/// regardless of if it's sized or not.
5///
6/// # Examples
7///
8/// ```
9/// use audio::ExactSizeBuf;
10///
11/// fn test<T>(buf: T) where T: ExactSizeBuf {
12/// assert_eq!(buf.frames(), 4);
13/// }
14///
15/// test(audio::interleaved![[0i16; 4]; 4]);
16/// test(audio::sequential![[0i16; 4]; 4]);
17/// test(audio::dynamic![[0i16; 4]; 4]);
18/// test(audio::wrap::interleaved([0i16; 16], 4));
19/// test(audio::wrap::sequential([0i16; 16], 4));
20/// ```
21pub trait ExactSizeBuf: Buf {
22 /// The number of frames in a buffer.
23 ///
24 /// # Examples
25 ///
26 /// ```
27 /// use audio::ExactSizeBuf;
28 ///
29 /// fn test<T>(buf: T) where T: ExactSizeBuf {
30 /// assert_eq!(buf.frames(), 4);
31 /// }
32 ///
33 /// test(audio::interleaved![[0i16; 4]; 4]);
34 /// test(audio::sequential![[0i16; 4]; 4]);
35 /// test(audio::dynamic![[0i16; 4]; 4]);
36 /// test(audio::wrap::interleaved([0i16; 16], 4));
37 /// test(audio::wrap::sequential([0i16; 16], 4));
38 /// ```
39 fn frames(&self) -> usize;
40}
41
42impl<B> ExactSizeBuf for &B
43where
44 B: ?Sized + ExactSizeBuf,
45{
46 #[inline]
47 fn frames(&self) -> usize {
48 (**self).frames()
49 }
50}
51
52impl<B> ExactSizeBuf for &mut B
53where
54 B: ?Sized + ExactSizeBuf,
55{
56 #[inline]
57 fn frames(&self) -> usize {
58 (**self).frames()
59 }
60}