2015-02-06 09:42:57 -08:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2016-02-12 00:17:24 -08:00
|
|
|
use prelude::v1::*;
|
|
|
|
|
|
2015-02-06 09:42:57 -08:00
|
|
|
use io;
|
2015-09-03 09:49:50 +03:00
|
|
|
use ptr;
|
2015-04-14 11:17:47 -07:00
|
|
|
use sys::cvt;
|
|
|
|
|
use sys::c;
|
|
|
|
|
use sys::handle::Handle;
|
2015-02-06 09:42:57 -08:00
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Anonymous pipes
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
pub struct AnonPipe {
|
2015-04-14 11:17:47 -07:00
|
|
|
inner: Handle,
|
2015-02-06 09:42:57 -08:00
|
|
|
}
|
|
|
|
|
|
2015-04-03 15:34:15 -07:00
|
|
|
pub fn anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> {
|
2015-11-02 16:23:22 -08:00
|
|
|
let mut reader = c::INVALID_HANDLE_VALUE;
|
|
|
|
|
let mut writer = c::INVALID_HANDLE_VALUE;
|
2015-04-14 11:17:47 -07:00
|
|
|
try!(cvt(unsafe {
|
2015-09-03 09:49:50 +03:00
|
|
|
c::CreatePipe(&mut reader, &mut writer, ptr::null_mut(), 0)
|
2015-04-14 11:17:47 -07:00
|
|
|
}));
|
|
|
|
|
let reader = Handle::new(reader);
|
|
|
|
|
let writer = Handle::new(writer);
|
|
|
|
|
Ok((AnonPipe { inner: reader }, AnonPipe { inner: writer }))
|
2015-02-06 09:42:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AnonPipe {
|
2015-04-14 11:17:47 -07:00
|
|
|
pub fn handle(&self) -> &Handle { &self.inner }
|
2015-07-15 23:31:24 -07:00
|
|
|
pub fn into_handle(self) -> Handle { self.inner }
|
2015-02-06 09:42:57 -08:00
|
|
|
|
|
|
|
|
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
|
2015-04-14 11:17:47 -07:00
|
|
|
self.inner.read(buf)
|
2015-02-06 09:42:57 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-12 00:17:24 -08:00
|
|
|
pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
|
|
|
self.inner.read_to_end(buf)
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-06 09:42:57 -08:00
|
|
|
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
|
2015-04-14 11:17:47 -07:00
|
|
|
self.inner.write(buf)
|
2015-02-06 09:42:57 -08:00
|
|
|
}
|
|
|
|
|
}
|