Remove slice_to_end

This commit is contained in:
Chris Denton
2024-07-16 20:24:57 +00:00
parent 5922234654
commit 55c84e39cc
2 changed files with 13 additions and 18 deletions

View File

@@ -138,7 +138,7 @@ impl Handle {
pub unsafe fn read_overlapped( pub unsafe fn read_overlapped(
&self, &self,
buf: &mut [u8], buf: &mut [mem::MaybeUninit<u8>],
overlapped: *mut c::OVERLAPPED, overlapped: *mut c::OVERLAPPED,
) -> io::Result<Option<usize>> { ) -> io::Result<Option<usize>> {
// SAFETY: We have exclusive access to the buffer and it's up to the caller to // SAFETY: We have exclusive access to the buffer and it's up to the caller to
@@ -146,8 +146,13 @@ impl Handle {
unsafe { unsafe {
let len = cmp::min(buf.len(), u32::MAX as usize) as u32; let len = cmp::min(buf.len(), u32::MAX as usize) as u32;
let mut amt = 0; let mut amt = 0;
let res = let res = cvt(c::ReadFile(
cvt(c::ReadFile(self.as_raw_handle(), buf.as_mut_ptr(), len, &mut amt, overlapped)); self.as_raw_handle(),
buf.as_mut_ptr().cast::<u8>(),
len,
&mut amt,
overlapped,
));
match res { match res {
Ok(_) => Ok(Some(amt as usize)), Ok(_) => Ok(Some(amt as usize)),
Err(e) => { Err(e) => {

View File

@@ -5,7 +5,6 @@ use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read};
use crate::mem; use crate::mem;
use crate::path::Path; use crate::path::Path;
use crate::ptr; use crate::ptr;
use crate::slice;
use crate::sync::atomic::AtomicUsize; use crate::sync::atomic::AtomicUsize;
use crate::sync::atomic::Ordering::Relaxed; use crate::sync::atomic::Ordering::Relaxed;
use crate::sys::c; use crate::sys::c;
@@ -479,8 +478,11 @@ impl<'a> AsyncPipe<'a> {
fn schedule_read(&mut self) -> io::Result<bool> { fn schedule_read(&mut self) -> io::Result<bool> {
assert_eq!(self.state, State::NotReading); assert_eq!(self.state, State::NotReading);
let amt = unsafe { let amt = unsafe {
let slice = slice_to_end(self.dst); if self.dst.capacity() == self.dst.len() {
self.pipe.read_overlapped(slice, &mut *self.overlapped)? let additional = if self.dst.capacity() == 0 { 16 } else { 1 };
self.dst.reserve(additional);
}
self.pipe.read_overlapped(self.dst.spare_capacity_mut(), &mut *self.overlapped)?
}; };
// If this read finished immediately then our overlapped event will // If this read finished immediately then our overlapped event will
@@ -560,15 +562,3 @@ impl<'a> Drop for AsyncPipe<'a> {
} }
} }
} }
#[allow(unsafe_op_in_unsafe_fn)]
unsafe fn slice_to_end(v: &mut Vec<u8>) -> &mut [u8] {
if v.capacity() == 0 {
v.reserve(16);
}
if v.capacity() == v.len() {
v.reserve(1);
}
// FIXME: Isn't this just spare_capacity_mut but worse?
slice::from_raw_parts_mut(v.as_mut_ptr().add(v.len()), v.capacity() - v.len())
}