1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![unstable(issue = "0", feature = "windows_handle")]
16 use io
::{ErrorKind, Read}
;
23 use sys_common
::io
::read_to_end_uninitialized
;
26 /// An owned container for `HANDLE` object, closing them on Drop.
28 /// All methods are inherited through a `Deref` impl to `RawHandle`
29 pub struct Handle(RawHandle
);
31 /// A wrapper type for `HANDLE` objects to give them proper Send/Sync inference
32 /// as well as Rust-y methods.
34 /// This does **not** drop the handle when it goes out of scope, use `Handle`
36 #[derive(Copy, Clone)]
37 pub struct RawHandle(c
::HANDLE
);
39 unsafe impl Send
for RawHandle {}
40 unsafe impl Sync
for RawHandle {}
43 pub fn new(handle
: c
::HANDLE
) -> Handle
{
44 Handle(RawHandle
::new(handle
))
47 pub fn new_event(manual
: bool
, init
: bool
) -> io
::Result
<Handle
> {
49 let event
= c
::CreateEventW(0 as *mut _
,
54 Err(io
::Error
::last_os_error())
56 Ok(Handle
::new(event
))
61 pub fn into_raw(self) -> c
::HANDLE
{
68 impl Deref
for Handle
{
69 type Target
= RawHandle
;
70 fn deref(&self) -> &RawHandle { &self.0 }
73 impl Drop
for Handle
{
75 unsafe { let _ = c::CloseHandle(self.raw()); }
80 pub fn new(handle
: c
::HANDLE
) -> RawHandle
{
84 pub fn raw(&self) -> c
::HANDLE { self.0 }
86 pub fn read(&self, buf
: &mut [u8]) -> io
::Result
<usize> {
88 // ReadFile takes a DWORD (u32) for the length so it only supports
89 // reading u32::MAX bytes at a time.
90 let len
= cmp
::min(buf
.len(), u32::MAX
as usize) as c
::DWORD
;
91 let res
= cvt(unsafe {
92 c
::ReadFile(self.0, buf
.as_mut_ptr() as c
::LPVOID
,
93 len
, &mut read
, ptr
::null_mut())
97 Ok(_
) => Ok(read
as usize),
99 // The special treatment of BrokenPipe is to deal with Windows
100 // pipe semantics, which yields this error when *reading* from
101 // a pipe after the other end has closed; we interpret that as
103 Err(ref e
) if e
.kind() == ErrorKind
::BrokenPipe
=> Ok(0),
109 pub unsafe fn read_overlapped(&self,
111 overlapped
: *mut c
::OVERLAPPED
)
112 -> io
::Result
<Option
<usize>> {
113 let len
= cmp
::min(buf
.len(), <c
::DWORD
>::max_value() as usize) as c
::DWORD
;
116 c
::ReadFile(self.0, buf
.as_ptr() as c
::LPVOID
,
117 len
, &mut amt
, overlapped
)
120 Ok(_
) => Ok(Some(amt
as usize)),
122 if e
.raw_os_error() == Some(c
::ERROR_IO_PENDING
as i32) {
124 } else if e
.raw_os_error() == Some(c
::ERROR_BROKEN_PIPE
as i32) {
133 pub fn overlapped_result(&self,
134 overlapped
: *mut c
::OVERLAPPED
,
135 wait
: bool
) -> io
::Result
<usize> {
138 let wait
= if wait {c::TRUE}
else {c::FALSE}
;
140 c
::GetOverlappedResult(self.raw(), overlapped
, &mut bytes
, wait
)
143 Ok(_
) => Ok(bytes
as usize),
145 if e
.raw_os_error() == Some(c
::ERROR_HANDLE_EOF
as i32) ||
146 e
.raw_os_error() == Some(c
::ERROR_BROKEN_PIPE
as i32) {
156 pub fn cancel_io(&self) -> io
::Result
<()> {
158 cvt(c
::CancelIo(self.raw())).map(|_
| ())
162 pub fn read_to_end(&self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
164 (&mut me
).read_to_end(buf
)
167 pub fn write(&self, buf
: &[u8]) -> io
::Result
<usize> {
169 // WriteFile takes a DWORD (u32) for the length so it only supports
170 // writing u32::MAX bytes at a time.
171 let len
= cmp
::min(buf
.len(), u32::MAX
as usize) as c
::DWORD
;
173 c
::WriteFile(self.0, buf
.as_ptr() as c
::LPVOID
,
174 len
, &mut amt
, ptr
::null_mut())
179 pub fn duplicate(&self, access
: c
::DWORD
, inherit
: bool
,
180 options
: c
::DWORD
) -> io
::Result
<Handle
> {
181 let mut ret
= 0 as c
::HANDLE
;
183 let cur_proc
= c
::GetCurrentProcess();
184 c
::DuplicateHandle(cur_proc
, self.0, cur_proc
, &mut ret
,
185 access
, inherit
as c
::BOOL
,
192 impl<'a
> Read
for &'a RawHandle
{
193 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
197 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
198 unsafe { read_to_end_uninitialized(self, buf) }