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
use {BVEncodeResult, BVDecodeResult, BVSize};
use errors::{ByteVecError, BVExpectedSize};
pub trait ByteEncodable {
fn get_size<Size>(&self) -> Option<Size> where Size: BVSize + ByteEncodable;
fn encode<Size>(&self) -> BVEncodeResult<Vec<u8>> where Size: BVSize + ByteEncodable;
}
pub trait ByteDecodable: Sized {
fn decode<Size>(bytes: &[u8]) -> BVDecodeResult<Self> where Size: BVSize + ByteDecodable;
fn decode_max<Size>(bytes: &[u8], limit: Size) -> BVDecodeResult<Self>
where Size: BVSize + ByteDecodable
{
if bytes.len() <= limit.as_usize() {
Self::decode::<Size>(bytes)
} else {
Err(ByteVecError::BadSizeDecodeError {
expected: BVExpectedSize::LessOrEqualThan(limit.as_usize()),
actual: bytes.len(),
})
}
}
}