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
use crate::codecs::DataFormat;
use crate::codecs::Encodable;
use std::io::Result;
use std::io::Write;
const ID: u64 = 2;
const MEDIA_TYPE: &str = "application/octet-stream";
pub struct RawBinDataFormat;
impl DataFormat for RawBinDataFormat {
#[inline]
fn id() -> u64 {
ID
}
#[inline]
fn media_type() -> &'static str {
MEDIA_TYPE
}
}
impl<T: AsRef<[u8]>> Encodable<RawBinDataFormat> for T {
#[inline]
fn encode_to(&self, _format: &RawBinDataFormat, w: &mut impl Write) -> Result<usize> {
w.write(self.as_ref())
}
}
#[cfg(test)]
mod test {
use super::*;
use std::io::Cursor;
use std::io::Read;
#[test]
fn check_raw_binary_encoder_slice() {
let mut vec = Vec::<u8>::new();
let mut cursor = Cursor::new(&mut vec);
let msg = &[1u8; 10][..];
let df = RawBinDataFormat;
msg.encode_to(&df, &mut cursor).unwrap();
assert_eq!(cursor.position() as usize, msg.len());
cursor.set_position(0);
let expected = &mut [11u8; 10][..];
cursor.read_exact(expected).unwrap();
assert_eq!(expected, msg);
}
#[test]
fn check_data_format() {
assert_eq!(RawBinDataFormat::id(), ID);
assert_eq!(RawBinDataFormat::media_type(), MEDIA_TYPE);
}
}