Skip to content

Instantly share code, notes, and snippets.

@Noxivs
Last active August 29, 2015 14:04
Show Gist options
  • Save Noxivs/486e233c49ab837e5fbb to your computer and use it in GitHub Desktop.
Save Noxivs/486e233c49ab837e5fbb to your computer and use it in GitHub Desktop.
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