Class in io compiler

Stream

class Stream
Import
use io::{Stream};
Source: compiler/src/builtin_docs/io.hy

An open file, socket or standard stream.

A Stream is an opaque heap handle made by open, stdin / stdout / stderr, or the socket functions in io::net::tcp and io::net::udp. Every stream is non-blocking: read, write and the socket calls never wait, they return Err(IoError::WouldBlock) when the OS is not ready. Use wait_readable / wait_writable to wait, or the blocking adapters in io::sync (write_all, read_to_end, …).

The handle is closed by close, or when the garbage collector drops it. Stream implements Read and Write, so s.read(buf) and s.write(buf) work as methods too.

Methods#

NameDescription
attachInstalls package IO hooks on this stream in place and returns the same stream.
parkWaits until this stream’s descriptor is ready.
fdReturns the OS file descriptor (socket handle on Windows) of this stream.

Examples#

use io::{close, open, Stream};
use io::fs::{remove_file};
use io::sync::{read_to_end, write_all};
use string::{from_bytes, to_bytes};
use tests::assert;
let path = "/tmp/coil_doc_stream.txt";
let s: Stream = match open(path, "w") {
    Result::Ok(s) => s,
    Result::Err(_) => panic "open failed",
};
let _ = write_all(s, to_bytes("hello"));
let _ = close(s);

let r = match open(path, "r") {
    Result::Ok(s) => s,
    Result::Err(_) => panic "open failed",
};
let text = match read_to_end(r) {
    Result::Ok(bytes) => match from_bytes(bytes) {
        Result::Ok(t) => t,
        Result::Err(_) => "",
    },
    Result::Err(_) => "",
};
let _ = close(r);
let _ = remove_file(path);
assert!(text == "hello");

See also#

↑ ↓ navigate ↵ open