sheave_core/messages/
audio.rs

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

#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct Audio(Vec<u8>);

impl Audio {
    pub fn new(bytes: Vec<u8>) -> Self {
        Self(bytes)
    }

    pub fn get_bytes(&self) -> &[u8] {
        &self.0
    }
}

impl<const N: usize> From<[u8; N]> for Audio {
    fn from(bytes: [u8; N]) -> Self {
        Self(bytes.into())
    }
}

impl<const N: usize> From<&[u8; N]> for Audio {
    fn from(bytes: &[u8; N]) -> Self {
        Self(bytes.into())
    }
}

impl<const N: usize> From<&mut [u8; N]> for Audio {
    fn from(bytes: &mut [u8; N]) -> Self {
        Self(bytes.into())
    }
}

impl From<&[u8]> for Audio {
    fn from(bytes: &[u8]) -> Self {
        Self(bytes.into())
    }
}

impl From<&mut [u8]> for Audio {
    fn from(bytes: &mut [u8]) -> Self {
        Self(bytes.into())
    }
}

impl From<&str> for Audio {
    fn from(s: &str) -> Self {
        Self(s.into())
    }
}

impl From<String> for Audio {
    fn from(s: String) -> Self {
        Self(s.into())
    }
}

impl From<CString> for Audio {
    fn from(s: CString) -> Self {
        Self(s.into())
    }
}

impl<'a> From<Cow<'a, [u8]>> for Audio {
    fn from(bytes: Cow<'a, [u8]>) -> Self {
        Self(bytes.into())
    }
}

impl From<Box<[u8]>> for Audio {
    fn from(bytes: Box<[u8]>) -> Self {
        Self(bytes.into())
    }
}

impl From<VecDeque<u8>> for Audio {
    fn from(bytes: VecDeque<u8>) -> Self {
        Self(bytes.into())
    }
}

impl From<BinaryHeap<u8>> for Audio {
    fn from(bytes: BinaryHeap<u8>) -> Self {
        Self(bytes.into())
    }
}

impl From<Audio> for Vec<u8> {
    fn from(audio: Audio) -> Self {
        audio.0
    }
}

impl<'a> From<Audio> for Cow<'a, [u8]> {
    fn from(audio: Audio) -> Self {
        audio.0.into()
    }
}

impl<'a> From<&'a Audio> for Cow<'a, [u8]> {
    fn from(audio: &'a Audio) -> Self {
        Cow::from(&audio.0)
    }
}

impl From<Audio> for Box<[u8]> {
    fn from(audio: Audio) -> Self {
        audio.0.into()
    }
}

impl From<Audio> for VecDeque<u8> {
    fn from(audio: Audio) -> Self {
        audio.0.into()
    }
}

impl From<Audio> for BinaryHeap<u8> {
    fn from(audio: Audio) -> Self {
        audio.0.into()
    }
}

impl From<Audio> for Rc<[u8]> {
    fn from(audio: Audio) -> Self {
        audio.0.into()
    }
}

impl From<Audio> for Arc<[u8]> {
    fn from(audio: Audio) -> Self {
        audio.0.into()
    }
}

impl<I: SliceIndex<[u8]>> Index<I> for Audio {
    type Output = I::Output;

    fn index(&self, index: I) -> &Self::Output {
        self.0.index(index)
    }
}

impl<I: SliceIndex<[u8]>> IndexMut<I> for Audio {
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        self.0.index_mut(index)
    }
}

impl Deref for Audio {
    type Target = Vec<u8>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Audio {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<const N: usize> PartialEq<[u8; N]> for Audio {
    fn eq(&self, other: &[u8; N]) -> bool {
        self.0.eq(other)
    }
}

impl<const N: usize> PartialEq<&[u8; N]> for Audio {
    fn eq(&self, other: &&[u8; N]) -> bool {
        self.0.eq(other)
    }
}

impl PartialEq<[u8]> for Audio {
    fn eq(&self, other: &[u8]) -> bool {
        self.0.eq(other)
    }
}

impl PartialEq<&[u8]> for Audio {
    fn eq(&self, other: &&[u8]) -> bool {
        self.0.eq(other)
    }
}

impl PartialEq<&mut [u8]> for Audio {
    fn eq(&self, other: &&mut [u8]) -> bool {
        self.0.eq(other)
    }
}

impl PartialEq<Vec<u8>> for Audio {
    fn eq(&self, other: &Vec<u8>) -> bool {
        self.0.eq(other)
    }
}

impl PartialEq<Audio> for [u8] {
    fn eq(&self, other: &Audio) -> bool {
        self.eq(&other.0)
    }
}

impl PartialEq<Audio> for &[u8] {
    fn eq(&self, other: &Audio) -> bool {
        self.eq(&other.0)
    }
}

impl PartialEq<Audio> for &mut [u8] {
    fn eq(&self, other: &Audio) -> bool {
        self.eq(&other.0)
    }
}

impl PartialEq<Audio> for Cow<'_, [u8]> {
    fn eq(&self, other: &Audio) -> bool {
        self.eq(&other.0)
    }
}

impl PartialEq<Audio> for Vec<u8> {
    fn eq(&self, other: &Audio) -> bool {
        self.eq(&other.0)
    }
}

impl PartialEq<Audio> for VecDeque<u8> {
    fn eq(&self, other: &Audio) -> bool {
        self.eq(&other.0)
    }
}

impl ChunkData for Audio {
    const CHANNEL: Channel = Channel::Audio;
    const MESSAGE_TYPE: MessageType = MessageType::Audio;
}

impl Decoder<Audio> for ByteBuffer {
    fn decode(&mut self) -> IOResult<Audio> {
        let remained = self.remained();
        self.get_bytes(remained).map(|bytes| Audio::new(bytes.to_vec()))
    }
}

impl Encoder<Audio> for ByteBuffer {
    fn encode(&mut self, audio: &Audio) {
        self.put_bytes(audio.get_bytes());
    }
}