Skip to content
CryoCryo home

file_descriptor

import std::io::file_descriptor; · source

io::file_descriptor is the canonical home for the libc calls every descriptor-owning type needs (Stdin, File, TcpStream, ChildStdin, ...), so wrappers import from here rather than re-declaring the same externs.

Types

BorrowedFileDescriptor

type struct BorrowedFileDescriptor {
    private fd: i32;
}

A descriptor read through without being owned: dropping it closes nothing, so it can read a descriptor whose owner closes it. Because it implements Read, the trait's own methods — read_to_end, read_exact, and the rest — work over any open descriptor.

import std::io::error::{ IoError };
import std::io::file_descriptor::{ BorrowedFileDescriptor };

function read_standard_input() -> Result<Array<u8>, IoError> {
    mut input: BorrowedFileDescriptor = BorrowedFileDescriptor::new(0);
    mut all: Array<u8> = Array::new();
    input.read_to_end(&all)?;
    return Result::Ok(all);
}

Constructors

static new(fd: i32) -> BorrowedFileDescriptor;

Read fd without taking it over. The caller keeps fd open for as long as this value reads it.

Trait implementations

implement trait Read for struct BorrowedFileDescriptor

Free Functions

FunctionNotes
read_file_descriptor(fd, buffer, length)read(2), retrying on EINTR. Zero means EOF.
write_file_descriptor(fd, bytes)write(2), same retry semantics. Short writes are normal.
close_file_descriptor(fd)close(2). Swallows the result — nothing sensible can be done with a close failure.
set_nonblocking(fd, on)Turns non-blocking mode on or off, keeping the other file-status flags. Fails with Unsupported on Windows, where a C runtime descriptor has no such mode.

The read and write wrappers handle the "retry on Interrupted, map everything else through IoError::from_errno" dance, which is what collapses most Read and Write implementations to a single line.

function read_file_descriptor(fd: i32, buffer: mut &Slice<u8>) -> Result<i64, IoError>;

read(2) wrapper: retries on EINTR, maps every other failure through IoError::from_errno. Returns the number of bytes actually read into the front of buffer; zero means EOF.

function write_file_descriptor(fd: i32, bytes: &Slice<u8>) -> Result<i64, IoError>;

write(2) wrapper with the same retry semantics as read_file_descriptor. Returns the number of bytes actually written; short writes are normal on slow sinks; callers who need all bytes written use Write::write_all.

function close_file_descriptor(fd: i32) -> void;

close(2) wrapper. Swallows the return value because nothing sensible can be done with a close failure; the fd is gone from the process's perspective either way.

function set_nonblocking(fd: i32, on: boolean) -> Result<(), IoError>;

Turn fd's non-blocking mode on or off, keeping its other file-status flags. In non-blocking mode a read with nothing available fails with WouldBlock instead of sleeping the thread.