Last active
October 17, 2016 05:51
-
-
Save Ralith/6a93fe9317a271e0ed87302ab07cfedb to your computer and use it in GitHub Desktop.
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
struct WordWrapper(Vec<capnp::Word>); | |
impl AsMut<[u8]> for WordWrapper { | |
fn as_mut(&mut self) -> &mut [u8] { | |
capnp::Word::words_to_bytes_mut(&mut self.0) | |
} | |
} | |
fn read_capnp_frame<T: io::Read>(stream: T, mut segment_sizes: Vec<u8>, mut segment_data: Vec<capnp::Word>) -> impl Future<Item=(T, Vec<u8>, Vec<capnp::Word>), Error=io::Error> { | |
tokio::io::read_exact(stream, [0; 4]).and_then(move |(stream, segment_count)| { | |
segment_sizes.resize(LittleEndian::read_u32(&segment_count) as usize * 4, 0); | |
tokio::io::read_exact(stream, segment_sizes).and_then(move |(stream, segment_sizes)| { | |
let mut len = 0; | |
for i in 0..segment_sizes.len()/4 { | |
len += LittleEndian::read_u32(&segment_sizes[i*4..i*4+4]) as usize; | |
} | |
segment_data.resize(len, capnp_word!(0, 0, 0, 0, 0, 0, 0, 0)); | |
tokio::io::read_exact(stream, WordWrapper(segment_data)).map(move |(stream, segment_data)| (stream, segment_sizes, segment_data.0)) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment