2016-12-15 08:47:31 +00:00
|
|
|
use std::io;
|
2017-04-20 20:43:01 +00:00
|
|
|
use smallvec::{Array, SmallVec};
|
2016-12-15 08:47:31 +00:00
|
|
|
|
|
|
|
pub trait ReadExt: io::Read {
|
|
|
|
fn read_max(&mut self, mut buf: &mut [u8]) -> io::Result<usize> {
|
|
|
|
let start_len = buf.len();
|
|
|
|
while !buf.is_empty() {
|
|
|
|
match self.read(buf) {
|
|
|
|
Ok(0) => break,
|
|
|
|
Ok(n) => { let tmp = buf; buf = &mut tmp[n..]; }
|
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 17:18:30 +00:00
|
|
|
Ok(start_len - buf.len())
|
2016-12-15 08:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: io::Read> ReadExt for T { }
|
2017-04-20 20:43:01 +00:00
|
|
|
|
|
|
|
// TODO: It would be nice if we could somehow have one trait that could give us
|
|
|
|
// either SmallVec or Vec.
|
|
|
|
pub trait IntoCollection<T> {
|
|
|
|
fn into_collection<A: Array<Item=T>>(self) -> SmallVec<A>;
|
|
|
|
fn mapped<U, F: FnMut(T) -> U, A: Array<Item=U>>(self, f: F) -> SmallVec<A>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoCollection<T> for T {
|
|
|
|
#[inline]
|
|
|
|
fn into_collection<A: Array<Item=T>>(self) -> SmallVec<A> {
|
|
|
|
let mut vec = SmallVec::new();
|
|
|
|
vec.push(self);
|
|
|
|
vec
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mapped<U, F: FnMut(T) -> U, A: Array<Item=U>>(self, mut f: F) -> SmallVec<A> {
|
|
|
|
f(self).into_collection()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoCollection<T> for Vec<T> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn into_collection<A: Array<Item=T>>(self) -> SmallVec<A> {
|
|
|
|
SmallVec::from_vec(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn mapped<U, F: FnMut(T) -> U, A: Array<Item=U>>(self, mut f: F) -> SmallVec<A> {
|
|
|
|
self.into_iter().map(|item| f(item)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 11:29:59 +00:00
|
|
|
macro_rules! impl_for_slice {
|
|
|
|
($($size:tt)*) => (
|
|
|
|
impl<'a, T: Clone> IntoCollection<T> for &'a [T $($size)*] {
|
|
|
|
#[inline(always)]
|
|
|
|
fn into_collection<A: Array<Item=T>>(self) -> SmallVec<A> {
|
|
|
|
self.iter().cloned().collect()
|
|
|
|
}
|
2017-04-20 20:43:01 +00:00
|
|
|
|
2017-06-22 11:29:59 +00:00
|
|
|
#[inline]
|
|
|
|
fn mapped<U, F: FnMut(T) -> U, A: Array<Item=U>>(self, mut f: F) -> SmallVec<A> {
|
|
|
|
self.iter().cloned().map(|item| f(item)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2017-04-20 20:43:01 +00:00
|
|
|
}
|
2017-06-22 11:29:59 +00:00
|
|
|
|
|
|
|
impl_for_slice!();
|
|
|
|
impl_for_slice!(; 1);
|
|
|
|
impl_for_slice!(; 2);
|
|
|
|
impl_for_slice!(; 3);
|
|
|
|
impl_for_slice!(; 4);
|
|
|
|
impl_for_slice!(; 5);
|
|
|
|
impl_for_slice!(; 6);
|
|
|
|
impl_for_slice!(; 7);
|
|
|
|
impl_for_slice!(; 8);
|
|
|
|
impl_for_slice!(; 9);
|
|
|
|
impl_for_slice!(; 10);
|