Last active
January 29, 2017 13:15
-
-
Save jsimmons/5958425a4e8129549d06ecb11dc20cc4 to your computer and use it in GitHub Desktop.
Playing with Glen Fiedler's packet serialisation approach in Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct RigidBody { | |
x: f32, | |
y: f32, | |
} | |
impl Serialize for RigidBody { | |
fn serialize<T: BitStream>(&mut self, stream: &mut T) -> Result { | |
stream.stream_f32(&mut self.x).unwrap(); | |
stream.stream_f32(&mut self.y).unwrap(); | |
Ok(()) | |
} | |
} | |
fn main() | |
let mut data: [u32;1024] = [0; 1024]; | |
{ | |
let mut writer = BitWriter::new(&mut data); | |
for _ in 0..512 { | |
writer.write_bits(0xd, 4); | |
writer.write_bits(0xe, 4); | |
writer.write_bits(0xa, 4); | |
writer.write_bits(0xd, 4); | |
writer.write_bits(0xb, 4); | |
writer.write_bits(0xe, 4); | |
writer.write_bits(0xe, 4); | |
writer.write_bits(0xf, 4); | |
writer.write_bits(0xcafeface, 32); | |
} | |
} | |
{ | |
let mut reader = BitReader::new(&data); | |
for _ in 0..512 { | |
assert!(reader.read_bits(4) == 0xd); | |
assert!(reader.read_bits(4) == 0xe); | |
assert!(reader.read_bits(4) == 0xa); | |
assert!(reader.read_bits(4) == 0xd); | |
assert!(reader.read_bits(4) == 0xb); | |
assert!(reader.read_bits(4) == 0xe); | |
assert!(reader.read_bits(4) == 0xe); | |
assert!(reader.read_bits(4) == 0xf); | |
assert!(reader.read_bits(32) == 0xcafeface); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment