2013-09-16 15:28:56 -07: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::*;
|
2014-01-29 16:33:57 -08:00
|
|
|
use io::IoResult;
|
2013-12-12 17:30:41 -08:00
|
|
|
use libc;
|
|
|
|
use rt::rtio::{RtioPipe, LocalIo};
|
2013-09-16 15:28:56 -07:00
|
|
|
|
2013-10-02 18:52:03 -07:00
|
|
|
pub struct PipeStream {
|
2013-10-16 14:48:05 -07:00
|
|
|
priv obj: ~RtioPipe,
|
2013-10-02 18:52:03 -07:00
|
|
|
}
|
|
|
|
|
2013-09-16 15:28:56 -07:00
|
|
|
impl PipeStream {
|
2013-10-22 08:41:05 -07: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
|
|
|
|
///
|
2014-01-30 16:55:20 -08:00
|
|
|
/// ```rust
|
|
|
|
/// # #[allow(unused_must_use)];
|
|
|
|
/// use std::libc;
|
|
|
|
/// use std::io::pipe::PipeStream;
|
2013-10-22 08:41:05 -07:00
|
|
|
///
|
2014-01-30 16:55:20 -08:00
|
|
|
/// let mut pipe = PipeStream::open(libc::STDERR_FILENO);
|
|
|
|
/// pipe.write(bytes!("Hello, stderr!"));
|
|
|
|
/// ```
|
2014-01-29 16:33:57 -08:00
|
|
|
pub fn open(fd: libc::c_int) -> IoResult<PipeStream> {
|
2013-12-12 17:30:41 -08:00
|
|
|
LocalIo::maybe_raise(|io| {
|
|
|
|
io.pipe_open(fd).map(|obj| PipeStream { obj: obj })
|
|
|
|
})
|
2013-10-22 08:41:05 -07:00
|
|
|
}
|
|
|
|
|
2013-10-18 14:01:22 -07:00
|
|
|
pub fn new(inner: ~RtioPipe) -> PipeStream {
|
2013-10-02 18:52:03 -07:00
|
|
|
PipeStream { obj: inner }
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for PipeStream {
|
2014-01-29 16:33:57 -08:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.obj.read(buf) }
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for PipeStream {
|
2014-01-29 16:33:57 -08:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.obj.write(buf) }
|
2013-09-16 15:28:56 -07:00
|
|
|
}
|
2014-01-06 14:17:23 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
iotest!(fn partial_read() {
|
|
|
|
use os;
|
|
|
|
use io::pipe::PipeStream;
|
|
|
|
|
|
|
|
let os::Pipe { input, out } = os::pipe();
|
|
|
|
let out = PipeStream::open(out);
|
|
|
|
let mut input = PipeStream::open(input);
|
|
|
|
let (p, c) = Chan::new();
|
2014-01-26 22:42:26 -05:00
|
|
|
spawn(proc() {
|
2014-01-06 14:17:23 -08:00
|
|
|
let mut out = out;
|
2014-01-30 14:10:53 -08:00
|
|
|
out.write([10]).unwrap();
|
2014-01-06 14:17:23 -08:00
|
|
|
p.recv(); // don't close the pipe until the other read has finished
|
2014-01-26 22:42:26 -05:00
|
|
|
});
|
2014-01-06 14:17:23 -08:00
|
|
|
|
|
|
|
let mut buf = [0, ..10];
|
2014-01-30 14:10:53 -08:00
|
|
|
input.read(buf).unwrap();
|
2014-01-06 14:17:23 -08:00
|
|
|
c.send(());
|
|
|
|
})
|
|
|
|
}
|