Skip to content
This repository was archived by the owner on Dec 17, 2020. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::io::Read;
use std::convert::TryInto as _;
use std::mem;
use std::vec::Vec;

Expand All @@ -28,6 +30,27 @@ pub trait FallibleVec<T> {
fn try_extend_from_slice(&mut self, other: &[T]) -> Result<(), ()> where T: Clone;
}

/// Reserves the upper limit of what `src` can generate before reading all
/// bytes until EOF in this source, placing them into buf. If the allocation
/// is unsuccessful, or reading from the source generates an error before
/// reaching EOF, this will return an error. Otherwise, it will return the
/// number of bytes read.
///
/// Since `src.limit()` may return a value greater than the number of bytes
/// which can be read from the source, it's possible this function may fail
/// in the allocation phase even though allocating the number of bytes available
/// to read would have succeeded. In general, it is assumed that the callers
/// have accurate knowledge of the number of bytes of interest and have created
/// `src` accordingly.
pub fn try_read_to_end<T>(src: &mut std::io::Take<T>, buf: &mut Vec<u8>) -> Result<usize, ()>
where T: Read
{
let limit: usize = src.limit().try_into().map_err(|_| ())?;
FallibleVec::try_reserve(buf, limit)?;
let bytes_read = src.read_to_end(buf).map_err(|_| ())?;
Ok(bytes_read)
}

/////////////////////////////////////////////////////////////////
// Vec

Expand Down Expand Up @@ -134,3 +157,19 @@ fn extend_from_slice() {
FallibleVec::try_extend_from_slice(&mut vec, b"bar").unwrap();
assert_eq!(&vec, b"foobar");
}

#[test]
fn try_read_to_end_() {
let mut src = b"1234567890".take(5);
let mut buf = vec![];
let bytes_read = try_read_to_end(&mut src, &mut buf).unwrap();
assert_eq!(bytes_read, 5);
assert_eq!(buf, b"12345");
}

#[test]
fn try_read_to_end_oom() {
let mut src = b"1234567890".take(std::usize::MAX.try_into().expect("usize < u64"));
let mut buf = vec![];
assert!(try_read_to_end(&mut src, &mut buf).is_err());
}