-pub(crate) struct VecReadWrapper<T: Readable>(pub Vec<T>);
-impl<T: Readable> Readable for VecReadWrapper<T> {
- fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
- let count: u64 = Readable::read(reader)?;
- let mut values = Vec::with_capacity(cmp::min(count, MAX_ALLOC_SIZE / (core::mem::size_of::<T>() as u64)) as usize);
- for _ in 0..count {
- match Readable::read(reader) {
- Ok(v) => { values.push(v); },
+
+/// Wrapper to read elements from a given stream until it reaches the end of the stream.
+pub(crate) struct VecReadWrapper<T>(pub Vec<T>);
+impl<T: MaybeReadable> Readable for VecReadWrapper<T> {
+ #[inline]
+ fn read<R: Read>(mut reader: &mut R) -> Result<Self, DecodeError> {
+ let mut values = Vec::new();
+ loop {
+ let mut track_read = ReadTrackingReader::new(&mut reader);
+ match MaybeReadable::read(&mut track_read) {
+ Ok(Some(v)) => { values.push(v); },
+ Ok(None) => { },
+ // If we failed to read any bytes at all, we reached the end of our TLV
+ // stream and have simply exhausted all entries.
+ Err(ref e) if e == &DecodeError::ShortRead && !track_read.have_read => break,