2013-09-16 17:28:56 -05:00
|
|
|
// Copyright 2013 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.
|
|
|
|
|
|
|
|
//! Synchronous, in-memory pipes.
|
|
|
|
//!
|
|
|
|
//! Currently these aren't particularly useful, there only exists bindings
|
|
|
|
//! enough so that pipes can be created to child processes.
|
|
|
|
|
|
|
|
use prelude::*;
|
|
|
|
use super::{Reader, Writer};
|
2013-10-18 13:52:23 -05:00
|
|
|
use rt::io::{io_error, EndOfFile};
|
2013-10-22 10:41:05 -05:00
|
|
|
use rt::io::native::file;
|
|
|
|
use rt::rtio::{RtioPipe, with_local_io};
|
2013-09-16 17:28:56 -05:00
|
|
|
|
2013-10-02 20:52:03 -05:00
|
|
|
pub struct PipeStream {
|
2013-10-16 16:48:05 -05:00
|
|
|
priv obj: ~RtioPipe,
|
2013-10-02 20:52:03 -05:00
|
|
|
}
|
|
|
|
|
2013-09-16 17:28:56 -05:00
|
|
|
impl PipeStream {
|
2013-10-22 10:41:05 -05:00
|
|
|
/// Consumes a file descriptor to return a pipe stream that will have
|
|
|
|
/// synchronous, but non-blocking reads/writes. This is useful if the file
|
|
|
|
/// descriptor is acquired via means other than the standard methods.
|
|
|
|
///
|
|
|
|
/// This operation consumes ownership of the file descriptor and it will be
|
|
|
|
/// closed once the object is deallocated.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// use std::libc;
|
|
|
|
/// use std::rt::io::pipe;
|
|
|
|
///
|
|
|
|
/// let mut pipe = PipeStream::open(libc::STDERR_FILENO);
|
|
|
|
/// pipe.write(bytes!("Hello, stderr!"));
|
|
|
|
///
|
|
|
|
/// # Failure
|
|
|
|
///
|
|
|
|
/// If the pipe cannot be created, an error will be raised on the
|
|
|
|
/// `io_error` condition.
|
|
|
|
pub fn open(fd: file::fd_t) -> Option<PipeStream> {
|
|
|
|
do with_local_io |io| {
|
|
|
|
match io.pipe_open(fd) {
|
|
|
|
Ok(obj) => Some(PipeStream { obj: obj }),
|
|
|
|
Err(e) => {
|
|
|
|
io_error::cond.raise(e);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-18 16:01:22 -05:00
|
|
|
pub fn new(inner: ~RtioPipe) -> PipeStream {
|
2013-10-02 20:52:03 -05:00
|
|
|
PipeStream { obj: inner }
|
2013-09-16 17:28:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for PipeStream {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
2013-10-02 20:52:03 -05:00
|
|
|
match self.obj.read(buf) {
|
2013-09-16 17:28:56 -05:00
|
|
|
Ok(read) => Some(read),
|
|
|
|
Err(ioerr) => {
|
|
|
|
// EOF is indicated by returning None
|
|
|
|
if ioerr.kind != EndOfFile {
|
2013-10-18 13:52:23 -05:00
|
|
|
io_error::cond.raise(ioerr);
|
2013-09-16 17:28:56 -05:00
|
|
|
}
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-18 16:01:22 -05:00
|
|
|
fn eof(&mut self) -> bool { false }
|
2013-09-16 17:28:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for PipeStream {
|
|
|
|
fn write(&mut self, buf: &[u8]) {
|
2013-10-02 20:52:03 -05:00
|
|
|
match self.obj.write(buf) {
|
2013-09-16 17:28:56 -05:00
|
|
|
Ok(_) => (),
|
|
|
|
Err(ioerr) => {
|
|
|
|
io_error::cond.raise(ioerr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|