Skip to content

Commit

Permalink
impl std::io::Write for SmallVec
Browse files Browse the repository at this point in the history
  • Loading branch information
chpio committed Jun 22, 2017
1 parent b7f5fe5 commit e96793e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ use std::mem;
use std::ops;
use std::ptr;
use std::slice;
#[cfg(feature = "std")]
use std::io;
#[cfg(feature="heapsizeof")]
use std::os::raw::c_void;

Expand Down Expand Up @@ -632,6 +634,26 @@ impl<A: Array> BorrowMut<[A::Item]> for SmallVec<A> {
}
}

#[cfg(feature = "std")]
impl<A: Array<Item = u8>> io::Write for SmallVec<A> {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.extend_from_slice(buf);
Ok(buf.len())
}

#[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.extend_from_slice(buf);
Ok(())
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

impl<'a, A: Array> From<&'a [A::Item]> for SmallVec<A> where A::Item: Clone {
#[inline]
fn from(slice: &'a [A::Item]) -> SmallVec<A> {
Expand Down Expand Up @@ -1436,4 +1458,21 @@ pub mod tests {
assert_eq!(&*small_vec, &[1, 2, 3, 4, 5]);
drop(small_vec);
}

#[cfg(feature = "std")]
#[test]
fn test_write() {
use io::Write;

let data = [1, 2, 3, 4, 5];

let mut small_vec: SmallVec<[u8; 2]> = SmallVec::new();
let len = small_vec.write(&data[..]).unwrap();
assert_eq!(len, 5);
assert_eq!(small_vec.as_ref(), data.as_ref());

let mut small_vec: SmallVec<[u8; 2]> = SmallVec::new();
small_vec.write_all(&data[..]).unwrap();
assert_eq!(small_vec.as_ref(), data.as_ref());
}
}

0 comments on commit e96793e

Please sign in to comment.