summaryrefslogtreecommitdiffstats
path: root/third_party/rust/scroll/examples/data_ctx.rs
blob: 667f4b18f0dc5b743fd3b99a8e164f548fa860d1 (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
use scroll::{ctx, Endian, Pread, BE};

#[derive(Debug)]
struct Data<'a> {
    name: &'a str,
    id: u32,
}

impl<'a> ctx::TryFromCtx<'a, Endian> for Data<'a> {
    type Error = scroll::Error;
    fn try_from_ctx(src: &'a [u8], endian: Endian) -> Result<(Self, usize), Self::Error> {
        let name = src.pread::<&'a str>(0)?;
        let id = src.pread_with(name.len() + 1, endian)?;
        Ok((Data { name: name, id: id }, name.len() + 4))
    }
}

fn main() {
    let bytes = b"UserName\x00\x01\x02\x03\x04";
    let data = bytes.pread_with::<Data>(0, BE).unwrap();
    assert_eq!(data.id, 0x01020304);
    assert_eq!(data.name.to_string(), "UserName".to_string());
    println!("Data: {:?}", &data);
}