std::io::error
I/O Error Types
Structured error types for I/O operations, exposed as Io::ErrorKind
and Io::IoError.
Types
| Type | Description |
|---|---|
Io::ErrorKind |
Category of the I/O error (enum, 8 variants) |
Io::IoError |
Full error: kind + raw errno + human-readable message |
ErrorKind Variants
| Variant | Errno | Description |
|---|---|---|
NOT_FOUND |
ENOENT |
No such file or directory |
PERMISSION_DENIED |
EACCES |
Permission denied |
ALREADY_EXISTS |
EEXIST |
File or resource already exists |
INVALID_INPUT |
EINVAL |
Invalid argument |
INTERRUPTED |
EINTR |
Operation interrupted by signal |
WOULD_BLOCK |
EAGAIN |
Resource temporarily unavailable |
TIMED_OUT |
ETIMEDOUT |
Connection or operation timed out |
OTHER |
anything else | Unmapped errno value |
Construction
| Method | Description |
|---|---|
Io::IoError::fromErrno(raw) |
Map raw errno to kind + strerror message |
Io::IoError::lastError() |
Read thread-local errno, then fromErrno |
Example
import Io from "std::io";
import LibC from "std::libc";
function main(): i32 {
let fd: i32 = LibC::File::open("/nonexistent", LibC::File::O_RDONLY);
if (fd == -1) {
let err: Io::IoError = Io::IoError::lastError();
// err.kind == Io::ErrorKind::NOT_FOUND
// err.raw == 2 (ENOENT)
// err.message == "No such file or directory"
return 1;
}
return 0;
}
namespace __io_errno_fnFunctions
function errno_location(): *mut i32Returns the thread-local errno pointer on Linux.
namespace __io_errorReturns the thread-local errno pointer on macOS.
Functions
function strerror(errnum: i32): *u8Returns the platform error message for an errno value.
Constants
const EACCES: i32const EAGAIN: i32const EEXIST: i32const EINTR: i32const EINVAL: i32const ENOENT: i32const ETIMEDOUT: i32namespace IoI/O error types and errno conversion helpers.
This namespace maps platform errno values into portable ErrorKind values
while preserving the raw integer and host-provided message.
Records
record IoErrorStructured error from an I/O operation.
Wraps a raw POSIX errno value with a categorized ErrorKind
and a human-readable message from strerror(3).
Fields
kind– categorized error (for programmatic branching)raw– raw errno value (for logging or OS-specific checks)message– human-readable description fromstrerror
Construction
Use fromErrno when you already captured the errno value, or
lastError to read the thread-local errno automatically:
// After a failed syscall:
let err: Io::IoError = Io::IoError::lastError();
// Or with an explicit errno:
let err: Io::IoError = Io::IoError::fromErrno(2);
kind: ErrorKindCategorized error kind.
raw: i32Raw POSIX errno value.
message: strHuman-readable error description from strerror(3).
Points to a static buffer managed by the C runtime. Valid for the program’s lifetime; no allocation or deallocation needed.
static fromErrno(rawErrno: i32): IoErrorCreates an IoError from a raw POSIX errno value.
Maps the errno to an ErrorKind and calls strerror(3) to
obtain the human-readable message.
Arguments
rawErrno- Raw POSIX errno value (e.g. fromLibC::Errno::get())
Returns
Fully populated IoError with kind, raw value, and message.
static lastError(): IoErrorReads the thread-local errno and creates an IoError.
Convenience wrapper: reads errno via __errno_location(),
then delegates to fromErrno.
When to use
Call this immediately after a failed syscall, before any other
call that might overwrite errno:
let fd: i32 = LibC::File::open("/missing", LibC::File::O_RDONLY);
if (fd == -1) {
let err: Io::IoError = Io::IoError::lastError(); // reads errno NOW
}
errorKind(&self): ErrorKindReturns the categorized error kind.
errorMessage(&self): strReturns the human-readable error message.
rawCode(&self): i32Returns the raw POSIX errno value.
Enums
enum ErrorKindCategory of an I/O error.
Each variant maps to one or more POSIX errno values. The OTHER
variant is the catch-all for errno values not explicitly mapped.
NOT_FOUNDPERMISSION_DENIEDALREADY_EXISTSINVALID_INPUTINTERRUPTEDWOULD_BLOCKTIMED_OUTOTHER