]> git.proxmox.com Git - rustc.git/blame - src/libstd/sys/windows/pipe.rs
Imported Upstream version 1.6.0+dfsg1
[rustc.git] / src / libstd / sys / windows / pipe.rs
CommitLineData
85aaf69f
SL
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.
4//
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.
10
85aaf69f 11use io;
e9174d1e 12use ptr;
9346a6ac
AL
13use sys::cvt;
14use sys::c;
15use sys::handle::Handle;
85aaf69f
SL
16
17////////////////////////////////////////////////////////////////////////////////
18// Anonymous pipes
19////////////////////////////////////////////////////////////////////////////////
20
21pub struct AnonPipe {
9346a6ac 22 inner: Handle,
85aaf69f
SL
23}
24
9346a6ac 25pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
92a42be0
SL
26 let mut reader = c::INVALID_HANDLE_VALUE;
27 let mut writer = c::INVALID_HANDLE_VALUE;
9346a6ac 28 try!(cvt(unsafe {
e9174d1e 29 c::CreatePipe(&mut reader, &mut writer, ptr::null_mut(), 0)
9346a6ac
AL
30 }));
31 let reader = Handle::new(reader);
32 let writer = Handle::new(writer);
33 Ok((AnonPipe { inner: reader }, AnonPipe { inner: writer }))
85aaf69f
SL
34}
35
36impl AnonPipe {
9346a6ac 37 pub fn handle(&self) -> &Handle { &self.inner }
c1a9b12d 38 pub fn into_handle(self) -> Handle { self.inner }
85aaf69f 39
92a42be0 40 pub fn raw(&self) -> c::HANDLE { self.inner.raw() }
62682a34 41
85aaf69f 42 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
9346a6ac 43 self.inner.read(buf)
85aaf69f
SL
44 }
45
46 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
9346a6ac 47 self.inner.write(buf)
85aaf69f
SL
48 }
49}