Standard library
std/io
Errors from the operating system, files, processes and the network, and the interfaces for streams of bytes.
fn copyFile(from: str, to: str) ! io.Error {
var src = try os.open(from)
var dst = try os.create(to)
_ = try io.copy(&dst, &src)
}Types: enum ErrorKind, struct Error, interface Reader, interface Writer, struct BytesReader, struct Buffer
Functions: fromCode, newError, readAll, readText, copy, writeText, bytesReader, textReader
Types
enum ErrorKind
pub copy enum ErrorKind {
Other
NotFound
PermissionDenied
AlreadyExists
NotADirectory
IsADirectory
DirectoryNotEmpty
InvalidInput
UnexpectedEof
BrokenPipe
ConnectionRefused
ConnectionReset
AddressInUse
TimedOut
Cancelled
Interrupted
}What went wrong, independent of the platform.
struct Error
pub struct Error {
pub kind: ErrorKind
pub msg: String
// the operating system's error number, or 0
pub code: int = 0
}An error from the operating system or a stream: what kind, and a message
that says what was being done (open notes.txt: no such file or directory).
os.Error and net.Error are this type.
Error.message
pub fn message(self) -> StringThe message, as print shows it.
Error.isCancelled
pub fn isCancelled(self) -> boolThe call stopped because its task was cancelled.
Error.isNotFound
pub fn isNotFound(self) -> boolThe file or directory does not exist.
interface Reader
pub interface Reader {
// Appends at most `max` bytes to `buf` and returns how many; 0 means the
// end of the stream. Waits until at least one byte is there.
fn read(inout self, buf: inout Vec[byte], max: int) -> int ! Error
}A source of bytes: files, connections, process output, io.BytesReader.
interface Writer
pub interface Writer {
// Writes all of `data`.
fn write(inout self, data: []byte) ! Error
}A sink for bytes: files, connections, io.Buffer.
struct BytesReader
pub struct BytesReader {
// and private fields
}Reads from bytes in memory.
BytesReader.read
pub fn read(inout self, buf: inout Vec[byte], max: int) -> int ! ErrorAppends up to max of the remaining bytes to buf.
struct Buffer
pub struct Buffer {
pub data: Vec[byte] = []
}Collects what is written to it.
Buffer.write
pub fn write(inout self, data: []byte) ! ErrorAppends data; never fails.
Buffer.text
pub fn text(self) -> StringWhat was written, as a string.
Functions
fromCode
pub fn fromCode(what: str, code: int) -> ErrorAn error from the operating system's error number code, for what ("open
data.txt"). Used by os, net and code calling C directly.
newError
pub fn newError(kind: ErrorKind, msg: str) -> ErrorAn error that doesn't come from the operating system.
readAll
pub fn readAll(r: inout Reader) -> Vec[byte] ! ErrorReads r to its end.
readText
pub fn readText(r: inout Reader) -> String ! ErrorReads r to its end as text.
copy
pub fn copy(dst: inout Writer, src: inout Reader) -> int ! ErrorCopies src into dst until src ends; returns the number of bytes.
writeText
pub fn writeText(w: inout Writer, s: str) ! ErrorWrites the bytes of s to w.
bytesReader
pub fn bytesReader(data: sink Vec[byte]) -> BytesReaderA reader of data.
textReader
pub fn textReader(s: str) -> BytesReaderA reader of the bytes of s.