Write for Cursor with a custom Allocator
This commit is contained in:
parent
a04d553e88
commit
38cef656d8
1 changed files with 23 additions and 7 deletions
|
|
@ -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};
|
||||
|
||||
|
|
@ -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,
|
||||
|
|
@ -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)?;
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue