Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl std::io::Write for SmallVec #52

Merged
merged 1 commit into from
Jun 22, 2017
Merged
Changes from all commits
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 @@ -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")]
Copy link
Contributor Author

@chpio chpio Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that the correct way of doing this? I guess it is if you want the tests to pass on core.

#[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());
}
}