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