fd
import std::io::fd; · source
Free Functions
File descriptors
io::fd is the canonical home for the libc calls every fd-owning type needs, so wrappers import from here rather than re-declaring the same externs.
| Function | Notes |
|---|---|
read_fd(fd, buffer, length) | read(2), retrying on EINTR. Zero means EOF. |
write_fd(fd, bytes) | write(2), same retry semantics. Short writes are normal. |
close_fd(fd) | close(2). Swallows the result — nothing sensible can be done with a close failure. |
These 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_fd(fd: i32, buffer: u8*, length: u64) -> Result<u64, IoError>;
function read_fd(fd: i32, buffer: u8*, length: u64) -> Result<u64, IoError>;read(2) wrapper: retries on EINTR, maps every other failure through IoError::from_errno. Returns the number of bytes actually read; zero means EOF.
function write_fd(fd: i32, bytes: Slice<u8>) -> Result<u64, IoError>;
function write_fd(fd: i32, bytes: Slice<u8>) -> Result<u64, IoError>;write(2) wrapper with the same retry semantics as read_fd. 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_fd(fd: i32) -> void;
function close_fd(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.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
function read_fd(fd: i32, buffer: u8*, length: u64) -> Result<u64, IoError>read(2) wrapper: retries on EINTR, maps every other failure through IoError::from_errno. Returns the number of bytes actually read; zero means EOF.
function read(fd: i32, buffer: u8*, count: u64) -> i64POSIX-shaped read over CRT _read. The count is clamped to the CRT's INT_MAX request ceiling - a short read is legal under the POSIX contract, so callers loop as they already must.
function write_fd(fd: i32, bytes: Slice<u8>) -> Result<u64, IoError>write(2) wrapper with the same retry semantics as read_fd. 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 write(fd: i32, buffer: u8*, count: u64) -> i64POSIX-shaped write over CRT _write; same clamp, short writes legal.
function close_fd(fd: i32) -> voidclose(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.
IoErrorKind::Interruptedtype struct IoError {
kind: IoErrorKind;
os_code: i32;
}static IoError::from_errno(code: i32) -> IoErrorMap a POSIX errno to an IoError. Unknown codes fold to Other with the numeric code preserved in os_code.
type trait Read {
read(mut &this, buffer: u8*, length: u64) -> Result<u64, IoError>;
read_all(mut &this, buffer: u8*, length: u64) -> Result<u64, IoError>;
read_byte(mut &this) -> Result<Option<u8>, IoError>;
read_char(mut &this) -> Result<Option<u32>, IoError>;
read_until(mut &this, delimiter: u8, out: mut &Array<u8>) -> Result<u64, IoError>;
read_exact(mut &this, buffer: u8*, length: u64) -> Result<(), IoError>;
read_to_end(mut &this, out: mut &Array<u8>) -> Result<u64, IoError>;
read_to_string(mut &this, out: mut &String) -> Result<u64, IoError>;
read_line(mut &this, out: mut &String) -> Result<u64, IoError>;
}type trait Write {
write_some(mut &this, bytes: Slice<u8>) -> Result<u64, IoError>;
flush(mut &this) -> Result<(), IoError>;
write<T>(mut &this, data: T) -> Result<(), IoError>;
}function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function read_fd(fd: i32, buffer: u8*, length: u64) -> Result<u64, IoError>read(2) wrapper: retries on EINTR, maps every other failure through IoError::from_errno. Returns the number of bytes actually read; zero means EOF.
(parameter) fd: i32(parameter) buffer: u8*(parameter) length: u64function write_fd(fd: i32, bytes: Slice<u8>) -> Result<u64, IoError>write(2) wrapper with the same retry semantics as read_fd. Returns the number of bytes actually written; short writes are normal on slow sinks; callers who need all bytes written use Write::write_all.
(parameter) bytes: Slice<u8>type struct Slice<T> {
ptr: T*;
length: u64;
}Slice<T>: a borrowed view into a contiguous sequence.
std::core::slicefunction close_fd(fd: i32) -> voidclose(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.