Skip to content

Instantly share code, notes, and snippets.

@HybridEidolon
Created December 12, 2016 01:22
Show Gist options
  • Save HybridEidolon/aac280f7a278c68e2afd174dca7622f2 to your computer and use it in GitHub Desktop.
Save HybridEidolon/aac280f7a278c68e2afd174dca7622f2 to your computer and use it in GitHub Desktop.
rust_mod = "psudata";
byteorder = "LE";
struct MsgHdr {
size: u32,
type: u16
}
struct Welcome {
copyright: c_str<64>,
server_key: array<u8, 64>,
client_key: array<u8, 64>
}
struct Test {
halfie: u16
_unused1: u16
}
// outputs psudata/mod.rs
use std::io::{Read, Write};
use bdlcore::{Serial, ReadExt, WriteExt};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
pub struct MsgHdr {
pub size: u32,
pub ty: u16
}
pub struct Welcome {
pub copyright: String,
pub server_key: Box<[u8]>,
pub client_key: Box<[u8]>
}
pub struct Test {
pub halfie: u16,
_unused1: u16
}
impl Serial for MsgHdr {
fn read<R: Read>(mut read: R) -> bdlcore::Result<Self> {
let size: u32 = try!(read.read_u32<LittleEndian>());
let ty: u16 = try!(read.read_u16<LittleEndian>());
Ok(MsgHdr {
size: size,
ty: ty
})
}
fn write<W: Write>(&self, mut write: W) -> bdlcore::Result<()> {
try!(write.write_u32<LittleEndian>(self.size));
try!(write.write_u16<LittleEndian>(self.ty));
Ok(())
}
}
impl Serial for Welcome {
fn read<R: Read>(mut read: R) -> bdlcore::Result<Self> {
let copyright: String = try!(read.read_cstr(64));
let server_key: Box<[u8]> = try!(read.read_array::<u8>(64));
let client_key: Box<[u8]> = try!(read.read_array::<u8>(64));
Ok(Welcome {
copyright: copyright,
server_key: server_key,
client_key: client_key
})
}
fn write<W: Write>(&self, mut write: W) -> bdlcore::Result<()> {
try!(write.write_cstr(&self.copyright, 64));
try!(write.write_array::<u8>(&self.server_key, 64));
try!(write.write_array::<u8>(&self.client_key, 64));
Ok(())
}
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment