Last active
August 29, 2015 14:04
-
-
Save celestefox/080a954477dbac673d31 to your computer and use it in GitHub Desktop.
Playing with Rust and serialization
This file contains 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
extern crate serialize; | |
extern crate uuid; | |
use uuid::Uuid; | |
use std::fmt::Show; | |
use std::collections::HashMap; | |
use serialize::{Encodable, Decodable}; | |
use serialize::ebml::writer::Encoder; | |
use serialize::ebml::reader::Decoder; | |
use serialize::ebml::{Doc, Error}; | |
use std::io::{File, IoResult, IoError}; | |
#[deriving(Show, Encodable, Decodable, Clone)] | |
enum BlockData { | |
BlockDataUuid(Uuid), | |
BlockDataString(String), | |
BlockDataFloat(f32), | |
BlockDataDouble(f64), | |
BlockDataBool(bool), | |
BlockDataCPos(u8), | |
BlockDataShortInt(i16), | |
BlockDataShortUnsigned(u16), | |
BlockDataInt(i32), | |
BlockDataUnsigned(u32), | |
BlockDataLongInt(i64), | |
BlockDataLongUnsigned(u64) | |
} | |
#[deriving(Show, Encodable, Decodable, Clone)] | |
struct Block { | |
id: Uuid, | |
data: Box<HashMap<Uuid, BlockData>> | |
} | |
#[deriving(Show, Encodable, Decodable, Clone)] | |
struct Chunk (Box<Vec<Vec<Vec<Box<Block>>>>>); | |
fn log<T: Show>(value: &T) { | |
println!("{}", value); | |
} | |
fn ebml_encode<'a, T: Encodable<Encoder<'a, File>, IoError>>(p: &Path, value: &T) -> IoResult<()> { | |
let mut file = try!(File::create(p)); | |
let mut encoder = Encoder::new(&mut file); | |
value.encode(&mut encoder) | |
} | |
fn ebml_decode<'a, T: Decodable<Decoder<'a>, Error>>(p: &Path) -> Result<T, IoError> { | |
let mut file = try!(File::open(p)); | |
let cont = try!(file.read_to_end()); | |
let doc = Doc::new(cont.as_slice()); | |
let mut decoder = Decoder::new(doc); | |
let r = serialize::Decodable::decode(&mut decoder); | |
return match r { | |
Ok(res) => res, | |
Err(e) => fail!("{}", e) | |
} | |
} | |
fn main() { | |
let my_uuid = Uuid::parse_string("2665534263e644278f31ca69774af957").ok().expect("Parsing uuid failed"); | |
let mut bdata = HashMap::new(); | |
bdata.insert(Uuid::new_v4(), BlockDataString("Test".to_string())); | |
bdata.insert(Uuid::new_v4(), BlockDataInt(1337)); | |
let my_block = Block{ id: my_uuid, data: box bdata }; | |
log(&my_block); | |
ebml_encode(&Path::new("block.bin"), &my_block); | |
let mut recon_block: Block = ebml_decode(&Path::new("block.bin")).ok().expect("Failed to decode block"); | |
log(&recon_block); | |
recon_block.id = Uuid::new_v4(); | |
log(&recon_block); | |
let my_block_vecs: Vec<Vec<Vec<Box<Block>>>> = Vec::from_fn(16, |i| Vec::from_fn(16, |j| Vec::from_fn(16, |k| box recon_block.clone()))); | |
let my_chunk = Chunk(box my_block_vecs); | |
//log(&my_chunk); | |
ebml_encode(&Path::new("chunk.bin"), &my_chunk); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment