Skip to content

Commit

Permalink
Merge pull request #13 from jess-sol/main
Browse files Browse the repository at this point in the history
Add Write implementation for Vec
  • Loading branch information
zakarumych authored Apr 12, 2024
2 parents 738620d + 4d178c7 commit c50521d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/stable/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ use core::ops::{self, Bound, Index, IndexMut, Range, RangeBounds};
use core::ptr::{self, NonNull};
use core::slice::{self, SliceIndex};

#[cfg(feature = "std")]
use std::io;

use super::{
alloc::{Allocator, Global},
assume,
Expand Down Expand Up @@ -3115,6 +3118,38 @@ pub fn from_elem<T: Clone>(elem: T, n: usize) -> Vec<T> {
v
}

/// Write is implemented for `Vec<u8>` by appending to the vector.
/// The vector will grow as needed.
#[cfg(feature = "std")]
impl<A: Allocator> io::Write for Vec<u8, A> {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.extend_from_slice(buf);
Ok(buf.len())
}

#[inline]
fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
let len = bufs.iter().map(|b| b.len()).sum();
self.reserve(len);
for buf in bufs {
self.extend_from_slice(buf);
}
Ok(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(())
}
}

#[cfg(feature = "serde")]
impl<T, A> serde::Serialize for Vec<T, A>
where
Expand Down

0 comments on commit c50521d

Please sign in to comment.