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;
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
| Function | Notes |
|---|---|
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>;
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>;
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;
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>;
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.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct BorrowedFileDescriptor {}A descriptor read through without being owned: dropping it closes nothing, so it reads a descriptor whose owner closes it, and Read's own methods (read_to_end, read_exact, ...) work over any open descriptor.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
BorrowedFileDescriptor.fd: i32import 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).
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}The error of a failed I/O operation: its kind, the operating system's own code, and, when the operation named one, what it was doing and the path. It prints as could not open config.toml: not found.
type struct BorrowedFileDescriptor {}A descriptor read through without being owned: dropping it closes nothing, so it reads a descriptor whose owner closes it, and Read's own methods (read_to_end, read_exact, ...) work over any open descriptor.
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_standard_input() -> Result<Array<u8>, IoError>type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type struct Array<T, A = GlobalAllocator> where A: Allocator {}A growable sequence of Ts stored contiguously, owning its elements; A is the allocator its storage comes from. Indexing out of range panics; get answers None instead.
mut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut input: BorrowedFileDescriptorstatic BorrowedFileDescriptor::new(fd: i32) -> BorrowedFileDescriptorRead fd without taking it over. The caller keeps fd open for as long as this value reads it.
mut all: Array<u8>static Array::new() -> Array<T, GlobalAllocator>Empty array backed by GlobalAllocator.
mut input: BorrowedFileDescriptorBorrowedFileDescriptor::read_to_end(mut &this, out: mut &Array<u8>) -> Result<i64, IoError>via trait Read
Read every remaining byte into out, growing the array as needed. Returns the number of bytes appended. EOF terminates the loop; other errors abort and surface.
mut all: Array<u8>value?Error propagation. On a Result<T, E> it yields the T of an Ok and otherwise returns the Err(e) from the enclosing function unchanged; on an Option<T> it yields the T of a Some or returns None. It works on any type implementing Try, in a function whose return type implements FromResidual of it; an error of another type converts through From.
return value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
Result::Ok(T)type struct Stdin {}Handle to process standard input. Construction is free; all instances dispatch to file descriptor 0. Unbuffered: a line read that needs minimum syscalls should wrap this in a BufferedReader<Stdin> from io::buffered.
type struct File {}An open file, closed when the value is released. It implements Read and Write; the static methods read or write a whole file in one call.
type struct TcpStream {}A connected TCP socket. Comes from TcpStream::connect (client) or listener.accept() (server), both of which leave it in non-blocking mode; bytes move through read_some and write_all, or through BufferedStream<TcpStream> via its AsyncTransport implementation. Owns its socket and closes it on drop.
type struct ChildStdin {}Write end of a pipe whose read end is the child's stdin. A write after the child has closed its stdin, or exited, fails with BrokenPipe; it never ends the writing process.
type trait Read {
read(mut &this, buffer: mut &Slice<u8>) -> Result<i64, IoError>;
read_all(mut &this, buffer: mut &Slice<u8>) -> Result<i64, IoError>;
read_byte(mut &this) -> Result<Option<u8>, IoError>;
read_char(mut &this) -> Result<Option<char>, IoError>;
read_until(mut &this, delimiter: u8, out: mut &Array<u8>) -> Result<i64, IoError>;
read_exact(mut &this, buffer: mut &Slice<u8>) -> Result<(), IoError>;
read_to_end(mut &this, out: mut &Array<u8>) -> Result<i64, IoError>;
read_to_string(mut &this, out: mut &String) -> Result<i64, IoError>;
read_line(mut &this, out: mut &String) -> Result<i64, IoError>;
}A source of bytes: a file, a pipe, standard input, a socket. An implementation provides read; the rest (read_to_end, read_to_string, read_line, ...) are built on it.
BorrowedFileDescriptor::read_exact(mut &this, buffer: mut &Slice<u8>) -> Result<(), IoError>via trait Read
Fill all of buffer. A short read at EOF is a failure, reported as UnexpectedEof. Use this when the caller knows the precise number of bytes that must be present.
static name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
(parameter) fd: i32implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referenceimplement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencefunction 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.
BorrowedFileDescriptor::read(mut &this, buffer: mut &Slice<u8>) -> Result<i64, IoError>via trait Read
std::io::traitsfunction 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.
intrinsic function write<T>(target: mut T*, value: T) -> voidfunction close_file_descriptor(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.
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.
IoErrorKind::UnsupportedThe operation is recognized but not implemented on this build (e.g. a feature stubbed pending implementation, or a platform the stdlib doesn't support yet). Distinct from InvalidInput (caller's fault) and Other (unmapped errno).
IoErrorKind::Interruptedstatic 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 Write {
write_some(mut &this, bytes: &Slice<u8>) -> Result<i64, IoError>;
flush(mut &this) -> Result<(), IoError>;
write<T>(mut &this, data: T) -> Result<(), IoError>;
}A sink for bytes: a file, standard output, a socket, a String's buffer. An implementation provides write_some and flush; the rest (write_all, write_str, write_line, ...) are built on them.
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.
(parameter) buffer: mut &Slice<u8>mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
type struct Slice<T> {}The elements are reached through ptr, declared read-only: through a mut &Slice<T> it reads as a writable pointer, through a &Slice<T> as the read-only one it is.
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.
(parameter) bytes: &Slice<u8>function close_file_descriptor(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.
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.
(parameter) on: booleanprimitive ()The unit type: a value that carries no information, and the empty tuple. Distinct from void, which is the absence of a value - Result<(), E> succeeds with nothing to return. 0 bytes.
IoErrorKind::WouldBlock