std::io (v0.2.1)
Input and output APIs, stream handles, formatted printing, scanning, and file operations.
Overview
The std::io module provides the usual input and output tools through a Thrust interface. It keeps streams, files, printing, scanning, and small file helpers in one module, so basic I/O code stays easy to read.
Most functions call into std::ffi::c::io. That means the behavior is close to C stdio: return values should be checked, file handles must be closed, and formatting follows C-style format strings.
Use this module for terminal output, simple file access, buffered reads and writes, and cases where you need direct control over a stream. The API is low-level, but the names are clearer than calling the raw C bindings directly.
Source: thrustc/std/v0.2.1/io.thrust
Public Signatures
Exported declarations for this module snapshot.
const EOF: s32 @public @extern("EOF") = -1;
const SEEK_SET: s32 @public @extern("SEEK_SET") = 0;
const SEEK_CUR: s32 @public @extern("SEEK_CUR") = 1;
const SEEK_END: s32 @public @extern("SEEK_END") = 2;
const BUFSIZ: usize @public @extern("BUFSIZ") = 8192;
const IOFBF: s32 @public @extern("IOFBF") = 0;
const IOLBF: s32 @public @extern("IOLBF") = 1;
const IONBF: s32 @public @extern("IONBF") = 2;
static mut stdin: File @public @extern("stdin");
static mut stdout: File @public @extern("stdout");
static mut stderr: File @public @extern("stderr");
fn print(fmt: CString) s32 @public @arbitraryArgs;
fn printTo(stream: File, fmt: CString) s32 @public @arbitraryArgs;
fn printToBuffer(buffer: ptr, size: usize, fmt: CString) s32 @public @arbitraryArgs;
fn scan(fmt: CString) s32 @public @arbitraryArgs;
fn scanFrom(stream: File, fmt: CString) s32 @public @arbitraryArgs;
fn scanFromString(text: CString, fmt: CString) s32 @public @arbitraryArgs;
fn println(text: CString) s32 @public;
fn printError(text: CString) void @public;
fn openFile(path: CString, mode: CString) File @public;
fn reopenFile(path: CString, mode: CString, stream: File) File @public;
fn closeFile(stream: File) s32 @public;
fn flush(stream: File) s32 @public;
fn readBytes(buffer: ptr, size: usize, count: usize, stream: File) usize @public;
fn writeBytes(buffer: ptr, size: usize, count: usize, stream: File) usize @public;
fn seek(stream: File, offset: s64, whence: s32) s32 @public;
fn tell(stream: File) s64 @public;
fn rewindStream(stream: File) void @public;
fn getStreamPosition(stream: File, position: ptr) s32 @public;
fn setStreamPosition(stream: File, position: ptr) s32 @public;
fn atEndOfStream(stream: File) s32 @public;
fn streamHasError(stream: File) s32 @public;
fn clearStreamError(stream: File) void @public;
fn streamFileDescriptor(stream: File) s32 @public;
fn openFileDescriptor(fd: s32, mode: CString) File @public;
fn readCharFrom(stream: File) s32 @public;
fn readChar() s32 @public;
fn writeCharTo(character: s32, stream: File) s32 @public;
fn writeChar(character: s32) s32 @public;
fn pushBackChar(character: s32, stream: File) s32 @public;
fn readLine(buffer: ptr, size: s32, stream: File) ptr @public;
fn writeLineTo(text: CString, stream: File) s32 @public;
fn deleteFile(path: CString) s32 @public;
fn moveFile(from: CString, to: CString) s32 @public;
fn setStreamBuffer(stream: File, buffer: ptr, mode: s32, size: usize) s32 @public;
fn setBuffer(stream: File, buffer: ptr) void @public;
fn setBufferSize(stream: File, buffer: ptr, size: usize) void @public;
fn setLineBuffer(stream: File) void @public;
fn readCharFast(stream: File) s32 @public;
fn writeCharFast(character: s32, stream: File) s32 @public;
fn printToBufferUnsafe(buffer: ptr, fmt: CString) s32 @public @arbitraryArgs;
fn openTempFile() File @public;
fn seekOffset(stream: File, offset: s64, whence: s32) s32 @public;
fn tellOffset(stream: File) s64 @public;
fn readLineAllocated(lineptr: ptr, size: ptr[usize], stream: File) s64 @public;
fn openPipe(command: CString, mode: CString) File @public;
fn closePipe(stream: File) s32 @public;
fn printToFileDescriptor(fd: s32, fmt: CString) s32 @public @arbitraryArgs;
fn lockStream(stream: File) void @public;
fn tryLockStream(stream: File) s32 @public;
fn unlockStream(stream: File) void @public;
fn renameAt(oldDirFd: s32, oldPath: CString, newDirFd: s32, newPath: CString) s32 @public;
fn renameAt2(oldDirFd: s32, oldPath: CString, newDirFd: s32, newPath: CString, flags: u32) s32 @public;
fn closeAllFiles() s32 @public;
Behavior and Use
Formatted output uses variadic functions. Use print for stdout, printTo for a specific stream, and printToBuffer when the destination is a fixed-size buffer.
Reading and writing functions return the amount of data processed or a status code. Check those values instead of assuming the whole operation succeeded.
Examples
import std::io;
fn main() s32 @public {
io::print("value=%d\n", 42);
return 0;
}
import std::io;
fn main() s32 @public {
var file := io::openFile("data.txt", "r");
if file == nullptr {
io::printError("openFile");
return 1;
}
var c: s32 = io::readCharFrom(file);
if c != io::EOF {
io::print("first byte=%d\n", c);
}
if io::closeFile(file) != 0 {
return 1;
}
return 0;
}
import std::io;
fn main() s32 @public {
var text: array[char; 64];
if io::printToBuffer(ref text, 64, "id=%d name=%s", 7, "tool") < 0 {
return 1;
}
io::print("%s\n", ref text);
return 0;
}
Notes
- Formatting helpers use C-style format strings, so the arguments must match the format.
- closeFile must be called on successfully opened handles to release resources.
- lockStream, tryLockStream, and unlockStream are available when a stream needs manual locking.