-
-
Save durka/f1d2bd36882bf8eb29ed to your computer and use it in GitHub Desktop.
read_to_vec
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::{Cursor, Read, self}; | |
trait ReadExt { | |
fn read_to_vec(&mut self, buf: &mut Vec<u8>) -> io::Result<usize>; | |
} | |
impl<T: Read> ReadExt for T { | |
fn read_to_vec(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { | |
self.take(buf.capacity() as u64).read_to_end(buf) | |
} | |
} | |
fn main() { | |
let mut source = Cursor::<&[u8]>::new(b"stuff"); | |
let mut buf = Vec::<u8>::with_capacity(4); | |
source.read_to_vec(&mut buf).unwrap(); | |
println!("{:?}", buf); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment