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
use std::io::Cursor;

use nanoserde::{DeBin, SerBin};

use byteorder::ReadBytesExt;

use super::property::Property;

/// A Property that can read/write itself from/into incoming/outgoing packets
pub trait PropertyIo<T> {
    /// Writes contained value into outgoing byte stream
    fn write(&self, buffer: &mut Vec<u8>);
    /// Given a cursor into incoming packet data, updates the Property with the
    /// synced value
    fn read(&mut self, cursor: &mut Cursor<&[u8]>);
}

impl<T: Clone + DeBin + SerBin> PropertyIo<T> for Property<T> {
    fn write(&self, buffer: &mut Vec<u8>) {
        let encoded = &mut SerBin::serialize_bin(&self.inner);
        buffer.push(encoded.len() as u8);
        buffer.append(encoded);
    }

    fn read(&mut self, cursor: &mut Cursor<&[u8]>) {
        let length = cursor.read_u8().unwrap();
        let mut buffer = Vec::with_capacity(length as usize);
        for _ in 0..length {
            buffer.push(cursor.read_u8().unwrap());
        }
        self.inner = DeBin::deserialize_bin(&buffer[..]).unwrap();
    }
}