sheave_core/messages/
audio.rs

1use std::{
2    borrow::Cow,
3    collections::{
4        BinaryHeap,
5        VecDeque
6    },
7    ffi::CString,
8    io::Result as IOResult,
9    ops::{
10        Deref,
11        DerefMut,
12        Index,
13        IndexMut
14    },
15    rc::Rc,
16    slice::SliceIndex,
17    sync::Arc
18};
19use crate::{
20    Decoder,
21    Encoder,
22    ByteBuffer
23};
24use super::{
25    Channel,
26    ChunkData,
27    headers::MessageType
28};
29
30#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
31pub struct Audio(Vec<u8>);
32
33impl Audio {
34    pub fn new(bytes: Vec<u8>) -> Self {
35        Self(bytes)
36    }
37
38    pub fn get_bytes(&self) -> &[u8] {
39        &self.0
40    }
41}
42
43impl<const N: usize> From<[u8; N]> for Audio {
44    fn from(bytes: [u8; N]) -> Self {
45        Self(bytes.into())
46    }
47}
48
49impl<const N: usize> From<&[u8; N]> for Audio {
50    fn from(bytes: &[u8; N]) -> Self {
51        Self(bytes.into())
52    }
53}
54
55impl<const N: usize> From<&mut [u8; N]> for Audio {
56    fn from(bytes: &mut [u8; N]) -> Self {
57        Self(bytes.into())
58    }
59}
60
61impl From<&[u8]> for Audio {
62    fn from(bytes: &[u8]) -> Self {
63        Self(bytes.into())
64    }
65}
66
67impl From<&mut [u8]> for Audio {
68    fn from(bytes: &mut [u8]) -> Self {
69        Self(bytes.into())
70    }
71}
72
73impl From<&str> for Audio {
74    fn from(s: &str) -> Self {
75        Self(s.into())
76    }
77}
78
79impl From<String> for Audio {
80    fn from(s: String) -> Self {
81        Self(s.into())
82    }
83}
84
85impl From<CString> for Audio {
86    fn from(s: CString) -> Self {
87        Self(s.into())
88    }
89}
90
91impl<'a> From<Cow<'a, [u8]>> for Audio {
92    fn from(bytes: Cow<'a, [u8]>) -> Self {
93        Self(bytes.into())
94    }
95}
96
97impl From<Box<[u8]>> for Audio {
98    fn from(bytes: Box<[u8]>) -> Self {
99        Self(bytes.into())
100    }
101}
102
103impl From<VecDeque<u8>> for Audio {
104    fn from(bytes: VecDeque<u8>) -> Self {
105        Self(bytes.into())
106    }
107}
108
109impl From<BinaryHeap<u8>> for Audio {
110    fn from(bytes: BinaryHeap<u8>) -> Self {
111        Self(bytes.into())
112    }
113}
114
115impl From<Audio> for Vec<u8> {
116    fn from(audio: Audio) -> Self {
117        audio.0
118    }
119}
120
121impl<'a> From<Audio> for Cow<'a, [u8]> {
122    fn from(audio: Audio) -> Self {
123        audio.0.into()
124    }
125}
126
127impl<'a> From<&'a Audio> for Cow<'a, [u8]> {
128    fn from(audio: &'a Audio) -> Self {
129        Cow::from(&audio.0)
130    }
131}
132
133impl From<Audio> for Box<[u8]> {
134    fn from(audio: Audio) -> Self {
135        audio.0.into()
136    }
137}
138
139impl From<Audio> for VecDeque<u8> {
140    fn from(audio: Audio) -> Self {
141        audio.0.into()
142    }
143}
144
145impl From<Audio> for BinaryHeap<u8> {
146    fn from(audio: Audio) -> Self {
147        audio.0.into()
148    }
149}
150
151impl From<Audio> for Rc<[u8]> {
152    fn from(audio: Audio) -> Self {
153        audio.0.into()
154    }
155}
156
157impl From<Audio> for Arc<[u8]> {
158    fn from(audio: Audio) -> Self {
159        audio.0.into()
160    }
161}
162
163impl<I: SliceIndex<[u8]>> Index<I> for Audio {
164    type Output = I::Output;
165
166    fn index(&self, index: I) -> &Self::Output {
167        self.0.index(index)
168    }
169}
170
171impl<I: SliceIndex<[u8]>> IndexMut<I> for Audio {
172    fn index_mut(&mut self, index: I) -> &mut Self::Output {
173        self.0.index_mut(index)
174    }
175}
176
177impl Deref for Audio {
178    type Target = Vec<u8>;
179
180    fn deref(&self) -> &Self::Target {
181        &self.0
182    }
183}
184
185impl DerefMut for Audio {
186    fn deref_mut(&mut self) -> &mut Self::Target {
187        &mut self.0
188    }
189}
190
191impl<const N: usize> PartialEq<[u8; N]> for Audio {
192    fn eq(&self, other: &[u8; N]) -> bool {
193        self.0.eq(other)
194    }
195}
196
197impl<const N: usize> PartialEq<&[u8; N]> for Audio {
198    fn eq(&self, other: &&[u8; N]) -> bool {
199        self.0.eq(other)
200    }
201}
202
203impl PartialEq<[u8]> for Audio {
204    fn eq(&self, other: &[u8]) -> bool {
205        self.0.eq(other)
206    }
207}
208
209impl PartialEq<&[u8]> for Audio {
210    fn eq(&self, other: &&[u8]) -> bool {
211        self.0.eq(other)
212    }
213}
214
215impl PartialEq<&mut [u8]> for Audio {
216    fn eq(&self, other: &&mut [u8]) -> bool {
217        self.0.eq(other)
218    }
219}
220
221impl PartialEq<Vec<u8>> for Audio {
222    fn eq(&self, other: &Vec<u8>) -> bool {
223        self.0.eq(other)
224    }
225}
226
227impl PartialEq<Audio> for [u8] {
228    fn eq(&self, other: &Audio) -> bool {
229        self.eq(&other.0)
230    }
231}
232
233impl PartialEq<Audio> for &[u8] {
234    fn eq(&self, other: &Audio) -> bool {
235        self.eq(&other.0)
236    }
237}
238
239impl PartialEq<Audio> for &mut [u8] {
240    fn eq(&self, other: &Audio) -> bool {
241        self.eq(&other.0)
242    }
243}
244
245impl PartialEq<Audio> for Cow<'_, [u8]> {
246    fn eq(&self, other: &Audio) -> bool {
247        self.eq(&other.0)
248    }
249}
250
251impl PartialEq<Audio> for Vec<u8> {
252    fn eq(&self, other: &Audio) -> bool {
253        self.eq(&other.0)
254    }
255}
256
257impl PartialEq<Audio> for VecDeque<u8> {
258    fn eq(&self, other: &Audio) -> bool {
259        self.eq(&other.0)
260    }
261}
262
263impl ChunkData for Audio {
264    const CHANNEL: Channel = Channel::Audio;
265    const MESSAGE_TYPE: MessageType = MessageType::Audio;
266}
267
268impl Decoder<Audio> for ByteBuffer {
269    fn decode(&mut self) -> IOResult<Audio> {
270        let remained = self.remained();
271        self.get_bytes(remained).map(|bytes| Audio::new(bytes.to_vec()))
272    }
273}
274
275impl Encoder<Audio> for ByteBuffer {
276    fn encode(&mut self, audio: &Audio) {
277        self.put_bytes(audio.get_bytes());
278    }
279}