diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:32 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:32 +0000 |
commit | 4547b622d8d29df964fa2914213088b148c498fc (patch) | |
tree | 9fc6b25f3c3add6b745be9a2400a6e96140046e9 /vendor/miow/src/handle.rs | |
parent | Releasing progress-linux version 1.66.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-4547b622d8d29df964fa2914213088b148c498fc.tar.xz rustc-4547b622d8d29df964fa2914213088b148c498fc.zip |
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/miow/src/handle.rs')
-rw-r--r-- | vendor/miow/src/handle.rs | 347 |
1 files changed, 174 insertions, 173 deletions
diff --git a/vendor/miow/src/handle.rs b/vendor/miow/src/handle.rs index f50c49d50..5302246ea 100644 --- a/vendor/miow/src/handle.rs +++ b/vendor/miow/src/handle.rs @@ -1,173 +1,174 @@ -use crate::*;
-use std::cmp;
-use std::io;
-use std::ptr;
-
-use windows_sys::Win32::Storage::FileSystem::*;
-use windows_sys::Win32::System::IO::*;
-
-#[derive(Debug)]
-pub struct Handle(HANDLE);
-
-unsafe impl Send for Handle {}
-unsafe impl Sync for Handle {}
-
-impl Handle {
- pub fn new(handle: HANDLE) -> Handle {
- Handle(handle)
- }
-
- pub fn raw(&self) -> HANDLE {
- self.0
- }
-
- pub fn into_raw(self) -> HANDLE {
- use std::mem;
-
- let ret = self.0;
- mem::forget(self);
- ret
- }
-
- pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
- let mut bytes = 0;
- let len = cmp::min(buf.len(), <u32>::max_value() as usize) as u32;
- crate::cvt(unsafe {
- WriteFile(
- self.0,
- buf.as_ptr() as *const _,
- len,
- &mut bytes,
- 0 as *mut _,
- )
- })?;
- Ok(bytes as usize)
- }
-
- pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
- let mut bytes = 0;
- let len = cmp::min(buf.len(), <u32>::max_value() as usize) as u32;
- crate::cvt(unsafe {
- ReadFile(
- self.0,
- buf.as_mut_ptr() as *mut _,
- len,
- &mut bytes,
- 0 as *mut _,
- )
- })?;
- Ok(bytes as usize)
- }
-
- pub unsafe fn read_overlapped(
- &self,
- buf: &mut [u8],
- overlapped: *mut OVERLAPPED,
- ) -> io::Result<Option<usize>> {
- self.read_overlapped_helper(buf, overlapped, FALSE)
- }
-
- pub unsafe fn read_overlapped_wait(
- &self,
- buf: &mut [u8],
- overlapped: *mut OVERLAPPED,
- ) -> io::Result<usize> {
- match self.read_overlapped_helper(buf, overlapped, TRUE) {
- Ok(Some(bytes)) => Ok(bytes),
- Ok(None) => panic!("logic error"),
- Err(e) => Err(e),
- }
- }
-
- pub unsafe fn read_overlapped_helper(
- &self,
- buf: &mut [u8],
- overlapped: *mut OVERLAPPED,
- wait: BOOL,
- ) -> io::Result<Option<usize>> {
- let len = cmp::min(buf.len(), <u32>::max_value() as usize) as u32;
- let res = crate::cvt({
- ReadFile(
- self.0,
- buf.as_mut_ptr() as *mut _,
- len,
- ptr::null_mut(),
- overlapped,
- )
- });
- match res {
- Ok(_) => (),
- Err(ref e) if e.raw_os_error() == Some(ERROR_IO_PENDING as i32) => (),
- Err(e) => return Err(e),
- }
-
- let mut bytes = 0;
- let res = crate::cvt(GetOverlappedResult(self.0, overlapped, &mut bytes, wait));
- match res {
- Ok(_) => Ok(Some(bytes as usize)),
- Err(ref e) if e.raw_os_error() == Some(ERROR_IO_INCOMPLETE as i32) && wait == FALSE => {
- Ok(None)
- }
- Err(e) => Err(e),
- }
- }
-
- pub unsafe fn write_overlapped(
- &self,
- buf: &[u8],
- overlapped: *mut OVERLAPPED,
- ) -> io::Result<Option<usize>> {
- self.write_overlapped_helper(buf, overlapped, FALSE)
- }
-
- pub unsafe fn write_overlapped_wait(
- &self,
- buf: &[u8],
- overlapped: *mut OVERLAPPED,
- ) -> io::Result<usize> {
- match self.write_overlapped_helper(buf, overlapped, TRUE) {
- Ok(Some(bytes)) => Ok(bytes),
- Ok(None) => panic!("logic error"),
- Err(e) => Err(e),
- }
- }
-
- unsafe fn write_overlapped_helper(
- &self,
- buf: &[u8],
- overlapped: *mut OVERLAPPED,
- wait: BOOL,
- ) -> io::Result<Option<usize>> {
- let len = cmp::min(buf.len(), <u32>::max_value() as usize) as u32;
- let res = crate::cvt({
- WriteFile(
- self.0,
- buf.as_ptr() as *const _,
- len,
- ptr::null_mut(),
- overlapped,
- )
- });
- match res {
- Ok(_) => (),
- Err(ref e) if e.raw_os_error() == Some(ERROR_IO_PENDING as i32) => (),
- Err(e) => return Err(e),
- }
-
- let mut bytes = 0;
- let res = crate::cvt(GetOverlappedResult(self.0, overlapped, &mut bytes, wait));
- match res {
- Ok(_) => Ok(Some(bytes as usize)),
- Err(ref e) if e.raw_os_error() == Some(ERROR_IO_INCOMPLETE as i32) && wait == FALSE => {
- Ok(None)
- }
- Err(e) => Err(e),
- }
- }
-}
-
-impl Drop for Handle {
- fn drop(&mut self) {
- unsafe { CloseHandle(self.0) };
- }
-}
+use crate::{BOOL, FALSE, TRUE}; +use std::cmp; +use std::io; +use std::ptr; + +use windows_sys::Win32::Foundation::{CloseHandle, ERROR_IO_INCOMPLETE, ERROR_IO_PENDING, HANDLE}; +use windows_sys::Win32::Storage::FileSystem::{ReadFile, WriteFile}; +use windows_sys::Win32::System::IO::{GetOverlappedResult, OVERLAPPED}; + +#[derive(Debug)] +pub struct Handle(HANDLE); + +unsafe impl Send for Handle {} +unsafe impl Sync for Handle {} + +impl Handle { + pub fn new(handle: HANDLE) -> Handle { + Handle(handle) + } + + pub fn raw(&self) -> HANDLE { + self.0 + } + + pub fn into_raw(self) -> HANDLE { + use std::mem; + + let ret = self.0; + mem::forget(self); + ret + } + + pub fn write(&self, buf: &[u8]) -> io::Result<usize> { + let mut bytes = 0; + let len = cmp::min(buf.len(), <u32>::max_value() as usize) as u32; + crate::cvt(unsafe { + WriteFile( + self.0, + buf.as_ptr() as *const _, + len, + &mut bytes, + 0 as *mut _, + ) + })?; + Ok(bytes as usize) + } + + pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> { + let mut bytes = 0; + let len = cmp::min(buf.len(), <u32>::max_value() as usize) as u32; + crate::cvt(unsafe { + ReadFile( + self.0, + buf.as_mut_ptr() as *mut _, + len, + &mut bytes, + 0 as *mut _, + ) + })?; + Ok(bytes as usize) + } + + pub unsafe fn read_overlapped( + &self, + buf: &mut [u8], + overlapped: *mut OVERLAPPED, + ) -> io::Result<Option<usize>> { + self.read_overlapped_helper(buf, overlapped, FALSE) + } + + pub unsafe fn read_overlapped_wait( + &self, + buf: &mut [u8], + overlapped: *mut OVERLAPPED, + ) -> io::Result<usize> { + match self.read_overlapped_helper(buf, overlapped, TRUE) { + Ok(Some(bytes)) => Ok(bytes), + Ok(None) => panic!("logic error"), + Err(e) => Err(e), + } + } + + pub unsafe fn read_overlapped_helper( + &self, + buf: &mut [u8], + overlapped: *mut OVERLAPPED, + wait: BOOL, + ) -> io::Result<Option<usize>> { + let len = cmp::min(buf.len(), <u32>::max_value() as usize) as u32; + let res = crate::cvt({ + ReadFile( + self.0, + buf.as_mut_ptr() as *mut _, + len, + ptr::null_mut(), + overlapped, + ) + }); + match res { + Ok(_) => (), + Err(ref e) if e.raw_os_error() == Some(ERROR_IO_PENDING as i32) => (), + Err(e) => return Err(e), + } + + let mut bytes = 0; + let res = crate::cvt(GetOverlappedResult(self.0, overlapped, &mut bytes, wait)); + match res { + Ok(_) => Ok(Some(bytes as usize)), + Err(ref e) if e.raw_os_error() == Some(ERROR_IO_INCOMPLETE as i32) && wait == FALSE => { + Ok(None) + } + Err(e) => Err(e), + } + } + + pub unsafe fn write_overlapped( + &self, + buf: &[u8], + overlapped: *mut OVERLAPPED, + ) -> io::Result<Option<usize>> { + self.write_overlapped_helper(buf, overlapped, FALSE) + } + + pub unsafe fn write_overlapped_wait( + &self, + buf: &[u8], + overlapped: *mut OVERLAPPED, + ) -> io::Result<usize> { + match self.write_overlapped_helper(buf, overlapped, TRUE) { + Ok(Some(bytes)) => Ok(bytes), + Ok(None) => panic!("logic error"), + Err(e) => Err(e), + } + } + + unsafe fn write_overlapped_helper( + &self, + buf: &[u8], + overlapped: *mut OVERLAPPED, + wait: BOOL, + ) -> io::Result<Option<usize>> { + let len = cmp::min(buf.len(), <u32>::max_value() as usize) as u32; + let res = crate::cvt({ + WriteFile( + self.0, + buf.as_ptr() as *const _, + len, + ptr::null_mut(), + overlapped, + ) + }); + match res { + Ok(_) => (), + Err(ref e) if e.raw_os_error() == Some(ERROR_IO_PENDING as i32) => (), + Err(e) => return Err(e), + } + + let mut bytes = 0; + let res = crate::cvt(GetOverlappedResult(self.0, overlapped, &mut bytes, wait)); + match res { + Ok(_) => Ok(Some(bytes as usize)), + Err(ref e) if e.raw_os_error() == Some(ERROR_IO_INCOMPLETE as i32) && wait == FALSE => { + Ok(None) + } + Err(e) => Err(e), + } + } +} + +impl Drop for Handle { + fn drop(&mut self) { + unsafe { CloseHandle(self.0) }; + } +} |