Skip to content

Commit

Permalink
Write for Cursor with a custom Allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jan 20, 2022
1 parent a04d553 commit 38cef65
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions library/std/src/io/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod tests;

use crate::io::prelude::*;

use crate::alloc::Allocator;
use crate::cmp;
use crate::io::{self, Error, ErrorKind, IoSlice, IoSliceMut, ReadBuf, SeekFrom};

Expand Down Expand Up @@ -398,7 +399,10 @@ fn slice_write_vectored(
}

// Resizing write implementation
fn vec_write(pos_mut: &mut u64, vec: &mut Vec<u8>, buf: &[u8]) -> io::Result<usize> {
fn vec_write<A>(pos_mut: &mut u64, vec: &mut Vec<u8, A>, buf: &[u8]) -> io::Result<usize>
where
A: Allocator,
{
let pos: usize = (*pos_mut).try_into().map_err(|_| {
Error::new_const(
ErrorKind::InvalidInput,
Expand Down Expand Up @@ -426,11 +430,14 @@ fn vec_write(pos_mut: &mut u64, vec: &mut Vec<u8>, buf: &[u8]) -> io::Result<usi
Ok(buf.len())
}

fn vec_write_vectored(
fn vec_write_vectored<A>(
pos_mut: &mut u64,
vec: &mut Vec<u8>,
vec: &mut Vec<u8, A>,
bufs: &[IoSlice<'_>],
) -> io::Result<usize> {
) -> io::Result<usize>
where
A: Allocator,
{
let mut nwritten = 0;
for buf in bufs {
nwritten += vec_write(pos_mut, vec, buf)?;
Expand Down Expand Up @@ -462,7 +469,10 @@ impl Write for Cursor<&mut [u8]> {
}

#[stable(feature = "cursor_mut_vec", since = "1.25.0")]
impl Write for Cursor<&mut Vec<u8>> {
impl<A> Write for Cursor<&mut Vec<u8, A>>
where
A: Allocator,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
vec_write(&mut self.pos, self.inner, buf)
}
Expand All @@ -483,7 +493,10 @@ impl Write for Cursor<&mut Vec<u8>> {
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Write for Cursor<Vec<u8>> {
impl<A> Write for Cursor<Vec<u8, A>>
where
A: Allocator,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
vec_write(&mut self.pos, &mut self.inner, buf)
}
Expand All @@ -504,7 +517,10 @@ impl Write for Cursor<Vec<u8>> {
}

#[stable(feature = "cursor_box_slice", since = "1.5.0")]
impl Write for Cursor<Box<[u8]>> {
impl<A> Write for Cursor<Box<[u8], A>>
where
A: Allocator,
{
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
slice_write(&mut self.pos, &mut self.inner, buf)
Expand Down

0 comments on commit 38cef65

Please sign in to comment.