docs

core::io

本页内容

API 参考只有英文版,由标准库源代码中的英文文档注释生成。

IoError, the byte-stream traits Read, BufRead, Write, Seek and their async twins, the buffers, Cursor, copy and the standard streams. The prelude names the module io and the traits Read, Write, AsyncRead, AsyncWrite.

Reads fill a SliceMut<u8> (a view of the caller's list); writes take anything AsBytes. Bytes cross to the runtime as GcBuf<u8> regions (buf, off, len), and become a str only through the UTF-8 check.

Async methods take ref self. A buffer owns its inner stream; over a stream that is not affine, and for a Cursor, copies share the state (it lives in lists).

Structs

BufReaderSource

pub struct BufReader<R>

Reads inner in chunks, so small reads (read_line, bytes) do not each cost a system call. It owns inner.

  • pub const fn new(inner: R) -> BufReader<R>§

    Source

    A buffered inner with the default capacity.

  • pub const fn with_capacity(cap: usize, inner: R) -> BufReader<R>§

    Source

    A buffered inner with a cap-byte buffer.

  • pub const fn into_inner(self) -> R§

    Source

    The inner stream; buffered bytes are dropped.

  • pub const fn buffer_len(self) -> usize§

    Source

    Bytes buffered and not yet read.

  • pub const fn capacity(self) -> usize§

    Source

    The buffer's size in bytes.

Trait implementations 7

BufWriterSource

pub struct BufWriter<W>

Collects writes and passes them to inner in batches of cap bytes. close() (or into_inner()) writes out the rest. It owns inner.

  • pub const fn new(inner: W) -> BufWriter<W>§

    Source

    A buffered inner with the default capacity.

  • pub const fn with_capacity(cap: usize, inner: W) -> BufWriter<W>§

    Source

    A buffered inner with a cap-byte buffer.

  • pub const fn buffer_len(self) -> usize§

    Source

    Bytes buffered and not yet written.

  • pub const fn capacity(self) -> usize§

    Source

    The buffer's size in bytes.

  • pub fn into_inner(self) throws (IoError) -> W§

    Source

    Write out what is buffered, then hand back the inner writer.

Trait implementations 6

LineWriterSource

pub struct LineWriter<W>

Buffers like BufWriter, and writes out through the last \n when a write holds one.

  • pub const fn new(inner: W) -> LineWriter<W>§

    Source

    inner behind a buffer flushed at each newline.

  • pub fn into_inner(self) throws (IoError) -> W§

    Source

    Write out what is buffered, then hand back the inner writer.

Trait implementations 6

CursorSource

pub struct Cursor<T>

Bytes in memory as a stream with a position: reads (and, over a List<u8>, writes) go from position(). A handle: copies share the position.

  • pub const fn new(inner: T) -> Cursor<T>§

    Source

    A cursor over inner at position 0.

  • pub const fn position(self) -> u64§

    Source

    The cursor's offset.

  • pub const fn set_position(self, pos: u64)§

    Source

    Move the cursor to pos; past the end, reads give nothing.

  • pub const fn inner(self) -> T§

    Source

    The underlying buffer.

  • pub const fn into_inner(self) -> T§

    Source

    The underlying buffer, giving up the cursor.

Trait implementations 9

EmptySource

pub struct Empty

empty(): a stream at its end.

Trait implementations 4

RepeatSource

pub struct Repeat

repeat(byte): an endless stream of one byte.

Trait implementations 3

SinkSource

pub struct Sink

sink(): a writer that discards everything.

Trait implementations 3

StderrSource

pub struct Stderr

Standard error, unbuffered, shared with eprint.

Trait implementations 2

StdinSource

pub struct Stdin

The process's standard input, read through the runtime's shared buffer. fill_buf holds at most the rest of one line in the handle, so lines a handle has not consumed stay with that handle.

Trait implementations 3

StdoutSource

pub struct Stdout

Standard output, through the runtime's buffer shared with print: flushed at a newline on a terminal, when full, and at exit.

Trait implementations 2

IoErrorSource

pub struct IoError

An I/O failure: its kind, the OS error code (none when the library raised it or a name did not resolve) and a readable message.

  • pub const fn new(kind: IoErrorKind, message: str) -> IoError§

    Source

    A library-raised error: no OS code.

  • pub fn from_os_code(code: i32, context: str = "") -> IoError§

    Source

    The error for OS error code (errno; Windows: a Win32 or Winsock code), its kind and message as the OS gives them; context (a path, a call) prefixes the message unless empty.

  • pub fn last_os_error() -> IoError§

    Source

    The error the last #[errno] C call on this thread reported (Windows: its GetLastError when set, else errno), as from_os_code gives it.

  • pub const fn kind(self) -> IoErrorKind§

    Source

    What kind of failure this is.

  • pub const fn os_code(self) -> i32 | none§

    Source

    The platform's code (errno; Windows: GetLastError / WSA), or none when the library raised the error or a name did not resolve.

  • pub const fn message(self) -> str§

    Source

    What went wrong, for display.

Trait implementations 3
  • impl Display for IoError
  • impl Debug for IoError
  • impl Error for IoError

BytesStreamSource

pub struct BytesStream

bytes_async(): a TryStream of the bytes, a view of the stream.

Trait implementations 4

LinesStreamSource

pub struct LinesStream

lines_async(): a TryStream of the lines, a view of the stream.

Trait implementations 4

LinesSource

pub struct Lines<R>

r.lines(): each line without \n / \r\n; a failed read throws. A view of r: it stays in the scope of r's owner.

Trait implementations 4

SplitSource

pub struct Split<R>

r.split(delim): the pieces between delim bytes; a view of r.

Trait implementations 4

BytesSource

pub struct Bytes<R>

r.bytes(): the bytes one at a time; a failed read throws, and the next next reads on. A view of r.

Trait implementations 4

TakeSource

pub struct Take<R>

r.take(limit): at most limit bytes of r; a view of r.

  • pub const fn limit(self) -> u64§

    Source

    Bytes still allowed.

  • pub const fn set_limit(self, limit: u64)§

    Source

    Allow limit more bytes.

Trait implementations 4
  • impl<R> !Send for Take<R>
  • impl<R> !Sync for Take<R>
  • impl<R> Debug for Take<R>
  • impl<R: Read> Read for Take<R>

OwnedSource

pub struct Owned<V, R>

A view V of the stream R together with the stream: what the into_ adapters return (r.into_lines()). It iterates, reads or streams as V does, closes R with itself and gives it back with into_inner.

  • pub const fn into_inner(self) -> R§

    Source

    The stream, giving up the adapter.

Trait implementations 7

ChainSource

pub struct Chain<A, B>

a.chain(b): a to its end, then b. It owns both: closing it closes both, into_inner gives them back.

  • pub const fn into_inner(self) -> (A, B)§

    Source

    The two readers, giving up the chain.

Trait implementations 4

Enums

IoErrorKindSource

pub enum IoErrorKind

What went wrong, from the OS error (os_code) or the library itself.

Trait implementations 2

SeekFromSource

pub enum SeekFrom

A position for Seek::seek.

Traits

CloseSource

pub trait Close

A resource whose close can fail: a file, a socket, a pipe, a buffered writer. Its owner closes it, try x.close() or using; leaving its scope open is an error, since a close error would be lost.

  • fn close(self) throws (IoError)§

    Source

    Close now, throwing the error the OS reports; later use fails.

ReadSource

pub trait Read

A source of bytes.

  • fn read(ref self, buf: SliceMut<u8>) throws (IoError) -> usize§

    Source

    Read up to buf.len() bytes into buf[0..n]; 0 is the end of the stream (or an empty buf).

  • fn read_vectored(ref self, bufs: Slice<SliceMut<u8>>) throws (IoError) -> usize§

    Source

    Scatter read: the default reads into the first non-empty buffer.

  • fn read_exact(ref self, buf: SliceMut<u8>) throws (IoError)§

    Source

    Fill all of buf, or UnexpectedEof.

  • fn read_to_end_into(ref self, out: List<u8>) throws (IoError) -> usize§

    Source

    Append every remaining byte to out, reading into its spare capacity (no zero fill first); the count.

  • fn read_to_end(ref self) throws (IoError) -> List<u8>§

    Source

    Every remaining byte.

  • fn read_to_str(ref self) throws (IoError) -> str§

    Source

    Every remaining byte as text (InvalidData if not UTF-8).

  • const fn bytes(ref self) -> Bytes<Self>§

    Source

    The remaining bytes one at a time. Each is one read: wrap an unbuffered stream in BufReader first.

  • const fn take(ref self, limit: u64) -> Take<Self>§

    Source

    At most limit more bytes, then the end.

  • const fn into_bytes(self) -> Owned<Bytes<Self>, Self>§

    Source

    bytes owning the stream: it may leave the scope or move to a task, closes the stream with itself, and into_inner gives it back.

  • const fn into_take(self, limit: u64) -> Owned<Take<Self>, Self>§

    Source

    take owning the stream.

  • const fn chain<R: Read>(self, next: R) -> Chain<Self, R>§

    Source

    This stream to its end, then next. The chain owns both and closes both.

BufReadSource

pub trait BufRead: Read

A Read with a buffer: line and delimiter reads cost no call per byte.

  • fn fill_buf(ref self) throws (IoError) -> Slice<u8>§

    Source

    The buffered bytes, refilled when empty; empty at the end. Valid until the next call on the stream.

  • fn consume(ref self, n: usize)§

    Source

    Mark n buffered bytes as read; panics past what is buffered.

  • fn read_until(ref self, delim: u8, out: List<u8>) throws (IoError) -> usize§

    Source

    Append bytes through delim (included) to out; the count, 0 at the end.

  • fn read_line(ref self, out: StrBuf) throws (IoError) -> usize§

    Source

    The next line, \n included, appended to out; the byte count, 0 at the end. InvalidData if the line is not UTF-8.

  • fn skip_until(ref self, delim: u8) throws (IoError) -> usize§

    Source

    Skip bytes through delim (included); the count.

  • const fn lines(ref self) -> Lines<Self>§

    Source

    The remaining lines, without \n / \r\n.

  • const fn split(ref self, delim: u8) -> Split<Self>§

    Source

    The pieces between delim bytes, delim removed.

  • const fn into_lines(self) -> Owned<Lines<Self>, Self>§

    Source

    lines owning the stream (Read::into_bytes).

  • const fn into_split(self, delim: u8) -> Owned<Split<Self>, Self>§

    Source

    split owning the stream.

WriteSource

pub trait Write

A sink of bytes.

  • fn write<B: AsBytes>(ref self, buf: B) throws (IoError) -> usize§

    Source

    Write some prefix of buf (a str, a List<u8>, a Slice<u8>, ..); its length.

  • fn flush(ref self) throws (IoError)§

    Source

    Hand buffered bytes on.

  • fn write_vectored(ref self, bufs: Slice<Slice<u8>>) throws (IoError) -> usize§

    Source

    Gather write: the default writes the first non-empty buffer.

  • fn write_all<B: AsBytes>(ref self, buf: B) throws (IoError)§

    Source

    Write all of buf; WriteZero if a write takes nothing.

  • fn write_fmt(ref self, args: fmt::Arguments) throws (IoError)§

    Source

    Write an f-string piece by piece, its holes through Display into the writer, never as one str of the whole text: try w.write_fmt(f"{name}: {value}\n").

  • fn write_display<T: Display>(ref self, x: T) throws (IoError)§

    Source

    Write x as print shows it (Display), in chunks as the text is made, so a large value is never held whole. A failed write is thrown as it is; a Display that fails on its own is Other.

SeekSource

pub trait Seek

Moves a byte cursor: files, Cursor and buffered readers.

  • fn seek(ref self, pos: SeekFrom) throws (IoError) -> u64§

    Source

    Move the cursor; the new offset from the start.

  • fn rewind(ref self) throws (IoError)§

    Source

    Back to the start: seek(SeekFrom::Start(0)).

  • fn stream_position(ref self) throws (IoError) -> u64§

    Source

    The offset from the start: seek(SeekFrom::Current(0)).

AsyncReadSource

pub trait AsyncRead

Read that suspends the task instead of blocking the thread.

AsyncBufReadSource

pub trait AsyncBufRead: AsyncRead

BufRead that suspends the task.

  • async fn fill_buf_async(ref self) throws (IoError) -> Slice<u8>§

    Source

    As BufRead::fill_buf.

  • fn consume(ref self, n: usize)§

    Source

    As BufRead::consume (it never waits).

  • async fn read_until_async(ref self, delim: u8, out: List<u8>) throws (IoError) -> usize§

    Source

    As BufRead::read_until.

  • async fn read_line_async(ref self, out: StrBuf) throws (IoError) -> usize§

    Source

    As BufRead::read_line.

  • const fn lines_async(ref self) -> LinesStream§

    Source

    The remaining lines as a stream, without \n / \r\n.

  • const fn into_lines_async(self) -> Owned<LinesStream, Self>§

    Source

    lines_async owning the stream (Read::into_bytes).

AsyncWriteSource

pub trait AsyncWrite

Write that suspends the task.

AsyncSeekSource

pub trait AsyncSeek

Seek that suspends the task.

AsyncCloseSource

pub trait AsyncClose

Close that suspends the task while the resource closes.

  • async fn close_async(self) throws (IoError)§

    Source

    Close without blocking the thread, throwing the error the OS reports; later use fails.

Functions

emptySource

pub const fn empty() -> Empty

A reader at its end.

repeatSource

pub const fn repeat(byte: u8) -> Repeat

A reader of byte forever.

sinkSource

pub const fn sink() -> Sink

A writer that discards everything.

stderrSource

pub const fn stderr() -> Stderr

A handle to standard error.

stdinSource

pub const fn stdin() -> Stdin

A handle to standard input.

stdoutSource

pub const fn stdout() -> Stdout

A handle to standard output.

inputSource

pub fn input(prompt: str = "") throws (IoError) -> str | none

prompt written to stdout (flushed), then the next line of standard input without its \n / \r\n; none at the end. Python: input(prompt), which raises at the end instead.

read_lineSource

pub fn read_line() throws (IoError) -> str | none

The next line of standard input without its \n / \r\n; none at the end.

copySource

pub fn copy<R: Read, W: Write>(ref r: R, ref w: W) throws (IoError) -> u64

Copy r to its end into w; the byte count. Between two OS handles the runtime lets the kernel move the bytes where their kinds allow (copy_file_range / sendfile / splice); other streams copy through read / write_all.

copy_asyncSource

pub async fn copy_async<R: AsyncRead, W: AsyncWrite>(r: R, w: W) throws (IoError) -> u64

copy that suspends the task. Awaited at once, the call gives r and w back to the caller.