aboutsummaryrefslogtreecommitdiff
path: root/src/primitive/signedint.rs
blob: b13968519ae5a6758b3905a4f99fa274f4d0ea78 (plain)
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
use byteorder::{BigEndian, ReadBytesExt};
use std::io::Cursor;

use std::result::Result;
use std::vec::Vec;

use crate::{deserialize::*, error::ProtocolError, serialize::*};

impl Serialize for i64 {
    fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
        Ok(Vec::from(self.to_be_bytes()))
    }
}

impl Deserialize for i64 {
    fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> {
        let mut rdr = Cursor::new(&b[0..8]);
        return Ok((8, rdr.read_i64::<BigEndian>()?));
    }
}

impl Serialize for i32 {
    fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
        Ok(Vec::from(self.to_be_bytes()))
    }
}

impl Deserialize for i32 {
    fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> {
        let mut rdr = Cursor::new(&b[0..4]);
        return Ok((4, rdr.read_i32::<BigEndian>()?));
    }
}

impl Serialize for i16 {
    fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
        Ok(Vec::from(self.to_be_bytes()))
    }
}

impl Deserialize for i16 {
    fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> {
        let mut rdr = Cursor::new(&b[0..2]);
        return Ok((2, rdr.read_i16::<BigEndian>()?));
    }
}

impl Serialize for i8 {
    fn serialize(&self) -> Result<Vec<u8>, ProtocolError> {
        Ok(Vec::from(self.to_be_bytes()))
    }
}

impl Deserialize for i8 {
    fn parse(b: &[u8]) -> Result<(usize, Self), ProtocolError> {
        let mut rdr = Cursor::new(&b[0..1]);
        return Ok((1, rdr.read_i8()?));
    }
}