Files
std/io/pipe.sakura
T
2026-11-13 21:54:22 -06:00

47 lines
1.6 KiB
Plaintext

#! @file pipe.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `pipe`, the two ends of a pipe, each a `file`: what is written to
#! one is read from the other, as when a program talks with one it started.
import system;
module std::io;
#! The two ends of a pipe, each a `file` that closes where its scope ends: what
#! is written to one is read from the other.
#!
#! A program it starts inherits neither end, unless it is given one, as
#! `std::process` gives a child its streams. Reading the reading end ends once
#! every copy of the writing end is closed. Writing to the writing end once
#! nothing reads fails with `io_error::broken_pipe`, rather than stopping the
#! program, as `pipe::open` says.
pub struct pipe {
pub var reading: file; #! The end that is read.
pub var writing: file; #! The end that is written.
}
#! Makes a pipe.
#!
#! The first pipe the program makes has a write to a pipe nothing reads fail
#! with `io_error::broken_pipe`, rather than stopping the program, where the
#! program has not said otherwise of `system::signal::pipe` itself. Writing
#! to the program's own output or error once nothing reads it still stops the
#! program, quietly, as it would have before.
#! @returns The pipe, or why it could not be made.
pub def pipe::open(): result<pipe, io_error> {
var reading: system::fd = 0;
var writing: system::fd = 0;
val made := system::pipe(&mut reading, &mut writing);
if made < 0 {
return system::error_of(made);
}
return pipe {
reading = file { _handle = reading, _open = true },
writing = file { _handle = writing, _open = true }
};
}