forked from hush/silentdragonlite-cli
![adityapk@gmail.com](/assets/img/avatar_default.png)
3 changed files with 37 additions and 15 deletions
@ -1,10 +1,21 @@ |
|||
pub fn set_panic_hook() { |
|||
// When the `console_error_panic_hook` feature is enabled, we can call the
|
|||
// `set_panic_hook` function at least once during initialization, and then
|
|||
// we will get better error messages if our code ever panics.
|
|||
//
|
|||
// For more details see
|
|||
// https://github.com/rustwasm/console_error_panic_hook#readme
|
|||
#[cfg(feature = "console_error_panic_hook")] |
|||
console_error_panic_hook::set_once(); |
|||
use std::io::{self, Read, Write}; |
|||
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; |
|||
|
|||
pub fn read_string<R: Read>(mut reader: R) -> io::Result<String> { |
|||
// Strings are written as <littleendian> len + bytes
|
|||
let str_len = reader.read_u64::<LittleEndian>()?; |
|||
let mut str_bytes = vec![0; str_len as usize]; |
|||
reader.read_exact(&mut str_bytes)?; |
|||
|
|||
let str = String::from_utf8(str_bytes).map_err(|e| { |
|||
io::Error::new(io::ErrorKind::InvalidData, e.to_string()) |
|||
})?; |
|||
|
|||
Ok(str) |
|||
} |
|||
|
|||
pub fn write_string<W: Write>(mut writer: W, s: &String) -> io::Result<()> { |
|||
// Strings are written as len + utf8
|
|||
writer.write_u64::<LittleEndian>(s.as_bytes().len() as u64)?; |
|||
writer.write_all(s.as_bytes()) |
|||
} |
Loading…
Reference in new issue