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