pipe.rs 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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::*;
A
Alex Crichton 已提交
17
use io::{io_error, EndOfFile};
18 19
use libc;
use rt::rtio::{RtioPipe, LocalIo};
20

21
pub struct PipeStream {
22
    priv obj: ~RtioPipe,
23 24
}

25
impl PipeStream {
26 27 28 29 30 31 32 33 34 35
    /// 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;
A
Alex Crichton 已提交
36
    ///     use std::io::pipe;
37 38 39 40 41 42 43 44
    ///
    ///     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.
45 46 47 48
    pub fn open(fd: libc::c_int) -> Option<PipeStream> {
        LocalIo::maybe_raise(|io| {
            io.pipe_open(fd).map(|obj| PipeStream { obj: obj })
        })
49 50
    }

51
    pub fn new(inner: ~RtioPipe) -> PipeStream {
52
        PipeStream { obj: inner }
53 54 55 56 57
    }
}

impl Reader for PipeStream {
    fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
58
        match self.obj.read(buf) {
59 60 61 62
            Ok(read) => Some(read),
            Err(ioerr) => {
                // EOF is indicated by returning None
                if ioerr.kind != EndOfFile {
A
Alex Crichton 已提交
63
                    io_error::cond.raise(ioerr);
64 65 66 67 68 69 70 71 72
                }
                return None;
            }
        }
    }
}

impl Writer for PipeStream {
    fn write(&mut self, buf: &[u8]) {
73
        match self.obj.write(buf) {
74 75 76 77 78 79 80
            Ok(_) => (),
            Err(ioerr) => {
                io_error::cond.raise(ioerr);
            }
        }
    }
}
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

#[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();
        do spawn {
            let mut out = out;
            out.write([10]);
            p.recv(); // don't close the pipe until the other read has finished
        }

        let mut buf = [0, ..10];
        input.read(buf);
        c.send(());
    })
}