Skip to content

Instantly share code, notes, and snippets.

Created March 9, 2017 19:51
Show Gist options
  • Save anonymous/f622c457e03417ce188fc8c97fee4d33 to your computer and use it in GitHub Desktop.
Save anonymous/f622c457e03417ce188fc8c97fee4d33 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::io::{self, Read, Write};
struct StringRW {
value: String,
buffer: Vec<u8>,
}
impl StringRW {
fn new(value: &str) -> Self {
StringRW {
value: value.to_owned(),
buffer: value.chars().map(|c| c as u8).collect::<Vec<u8>>(),
}
}
}
impl Read for StringRW {
fn read<'a>(&'a mut self, mut buff: &'a mut [u8]) -> Result<usize, io::Error> {
buff = self.buffer.as_mut_slice();
Ok(buff.len())
}
}
fn main() {
let mut srw = StringRW::new(">>>[]");
let mut buff: Vec<u8> = Vec::new();
srw.read(&mut buff);
println!("{:?}", buff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment