Last active
August 29, 2015 14:04
-
-
Save Noxivs/486e233c49ab837e5fbb to your computer and use it in GitHub Desktop.
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
use std::io::{MemReader, MemWriter,IoResult, IoError, InvalidInput}; | |
trait StringReader { | |
fn read_le_string(&mut self) -> IoResult<String>; | |
} | |
impl StringReader for MemReader { | |
fn read_le_string(&mut self) -> IoResult<String> { | |
let str_b_len = self.read_le_u16(); | |
match str_b_len { | |
Ok(l) => { | |
let vec_str_b = self.read_exact(l as uint); | |
match vec_str_b { | |
Ok(v) => { | |
let str_b = String::from_utf8(v); | |
match str_b { | |
Ok(s) => Ok(s), | |
Err(_) => Err(IoError { kind: InvalidInput,desc: "can't build the string buffer from vector", detail: None }) | |
} | |
}, | |
Err(err) => Err(err) | |
} | |
}, | |
Err(_) => Err(IoError { kind: InvalidInput,desc: "invalid len", detail: None }) | |
} | |
} | |
} | |
trait StringWriter { | |
fn write_le_string(&mut self, s: String) -> IoResult<()>; | |
} | |
impl StringWriter for MemWriter { | |
fn write_le_string(&mut self, s: String) -> IoResult<()> { | |
match self.write_le_u16(s.len() as u16) { | |
Ok(_) => self.write_str(s.as_slice()), | |
Err(e) => Err(e) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment