-
-
Save squiidz/c2fac6c3cc3c422aaa7c583f6561eec5 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or 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::{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