1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
use std::collections::VecDeque; use std::io::IoSlice; use bytes::Buf; pub(crate) struct BufList<T> { bufs: VecDeque<T>, } impl<T: Buf> BufList<T> { pub(crate) fn new() -> BufList<T> { BufList { bufs: VecDeque::new(), } } #[inline] pub(crate) fn push(&mut self, buf: T) { debug_assert!(buf.has_remaining()); self.bufs.push_back(buf); } #[inline] pub(crate) fn bufs_cnt(&self) -> usize { self.bufs.len() } } impl<T: Buf> Buf for BufList<T> { #[inline] fn remaining(&self) -> usize { self.bufs.iter().map(|buf| buf.remaining()).sum() } #[inline] fn bytes(&self) -> &[u8] { self.bufs.front().map(Buf::bytes).unwrap_or_default() } #[inline] fn advance(&mut self, mut cnt: usize) { while cnt > 0 { { let front = &mut self.bufs[0]; let rem = front.remaining(); if rem > cnt { front.advance(cnt); return; } else { front.advance(rem); cnt -= rem; } } self.bufs.pop_front(); } } #[inline] fn bytes_vectored<'t>(&'t self, dst: &mut [IoSlice<'t>]) -> usize { if dst.is_empty() { return 0; } let mut vecs = 0; for buf in &self.bufs { vecs += buf.bytes_vectored(&mut dst[vecs..]); if vecs == dst.len() { break; } } vecs } }