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.
A buffered
innerwith the default capacity.A buffered
innerwith acap-byte buffer.The inner stream; buffered bytes are dropped.
Bytes buffered and not yet read.
The buffer's size in bytes.
Trait implementations 7
impl<R> Debug for BufReader<R>impl<R: Close> Close for BufReader<R>impl<R: AsyncClose> AsyncClose for BufReader<R>impl<R: Read> Read for BufReader<R>impl<R: Read> BufRead for BufReader<R>impl<R: AsyncRead> AsyncRead for BufReader<R>impl<R: AsyncRead> AsyncBufRead for BufReader<R>
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.
A buffered
innerwith the default capacity.A buffered
innerwith acap-byte buffer.Bytes buffered and not yet written.
The buffer's size in bytes.
Write out what is buffered, then hand back the inner writer.
Trait implementations 6
impl<W> Debug for BufWriter<W>impl<W: Write> Write for BufWriter<W>impl<W: AsyncWrite> AsyncWrite for BufWriter<W>impl<W: Write + Close> Close for BufWriter<W>impl<W: AsyncWrite + AsyncClose> AsyncClose for BufWriter<W>impl<W: Write> Drop for BufWriter<W>
LineWriterSource
pub struct LineWriter<W>Buffers like BufWriter, and writes out through the last \n when a
write holds one.
innerbehind a buffer flushed at each newline.Write out what is buffered, then hand back the inner writer.
Trait implementations 6
impl<W> Debug for LineWriter<W>impl<W: Write> Write for LineWriter<W>impl<W: AsyncWrite> AsyncWrite for LineWriter<W>impl<W: Write + Close> Close for LineWriter<W>impl<W: AsyncWrite + AsyncClose> AsyncClose for LineWriter<W>impl<W: Write> Drop for LineWriter<W>
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.
A cursor over
innerat position 0.The cursor's offset.
Move the cursor to
pos; past the end, reads give nothing.The underlying buffer.
The underlying buffer, giving up the cursor.
Trait implementations 9
impl<T> Debug for Cursor<T>impl<T: AsBytes> Read for Cursor<T>impl<T: AsBytes> BufRead for Cursor<T>impl<T: AsBytes> Seek for Cursor<T>impl<T: AsBytes> AsyncRead for Cursor<T>impl<T: AsBytes> AsyncBufRead for Cursor<T>impl<T: AsBytes> AsyncSeek for Cursor<T>impl Write for Cursor<List<u8>>impl AsyncWrite for Cursor<List<u8>>
EmptySource
pub struct Emptyempty(): a stream at its end.
RepeatSource
pub struct Repeatrepeat(byte): an endless stream of one byte.
SinkSource
pub struct Sinksink(): a writer that discards everything.
Trait implementations 3
impl Debug for Sinkimpl Write for Sinkimpl AsyncWrite for Sink
StderrSource
pub struct StderrStandard error, unbuffered, shared with eprint.
StdinSource
pub struct StdinThe 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.
StdoutSource
pub struct StdoutStandard output, through the runtime's buffer shared with print:
flushed at a newline on a terminal, when full, and at exit.
IoErrorSource
pub struct IoErrorAn 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.
Sourcepub const fn new(§kind: IoErrorKind, message: str) -> IoError A library-raised error: no OS code.
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.The error the last
#[errno]C call on this thread reported (Windows: itsGetLastErrorwhen set, else errno), asfrom_os_codegives it.
Sourcepub const fn kind(§self) -> IoErrorKind What kind of failure this is.
The platform's code (errno; Windows:
GetLastError/ WSA), ornonewhen the library raised the error or a name did not resolve.What went wrong, for display.
BytesStreamSource
pub struct BytesStreambytes_async(): a TryStream of the bytes, a view of the stream.
LinesStreamSource
pub struct LinesStreamlines_async(): a TryStream of the lines, a view of the stream.
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
impl<R> !Send for Lines<R>impl<R> !Sync for Lines<R>impl<R> Debug for Lines<R>impl<R: BufRead> TryIterator for Lines<R>
SplitSource
pub struct Split<R>r.split(delim): the pieces between delim bytes; a view of r.
Trait implementations 4
impl<R> !Send for Split<R>impl<R> !Sync for Split<R>impl<R> Debug for Split<R>impl<R: BufRead> TryIterator for Split<R>
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
impl<R> !Send for Bytes<R>impl<R> !Sync for Bytes<R>impl<R> Debug for Bytes<R>impl<R: Read> TryIterator for Bytes<R>
TakeSource
pub struct Take<R>r.take(limit): at most limit bytes of r; a view of r.
Bytes still allowed.
Allow
limitmore bytes.
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.
The stream, giving up the adapter.
Trait implementations 7
unsafe impl<V, R: Send> Send for Owned<V, R>impl<V, R> Debug for Owned<V, R>impl<V, R: Close> Close for Owned<V, R>impl<V, R: AsyncClose> AsyncClose for Owned<V, R>impl<V: TryIterator, R> TryIterator for Owned<V, R>impl<V: TryStream, R> TryStream for Owned<V, R>impl<V: Read, R> Read for Owned<V, R>
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.
The two readers, giving up the chain.
Trait implementations 4
impl<A, B> Debug for Chain<A, B>impl<A: Close, B: Close> Close for Chain<A, B>impl<A: AsyncClose, B: AsyncClose> AsyncClose for Chain<A, B>impl<A: Read, B: Read> Read for Chain<A, B>
Enums
IoErrorKindSource
pub enum IoErrorKindWhat went wrong, from the OS error (os_code) or the library itself.
A write returned 0 before everything was written.
Too many symbolic links, or a final symbolic link where an open said not to follow one (
follow_symlinks: false).The file or device is in use: another handle's share mode denies this access (Windows), or it is busy (
EBUSY,ETXTBSY).The kind of OS error
code, as the runtime classifies it.
SeekFromSource
pub enum SeekFromA position for Seek::seek.
Traits
CloseSource
pub trait CloseA 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.
Close now, throwing the error the OS reports; later use fails.
ReadSource
pub trait ReadA source of bytes.
Read up to
buf.len()bytes intobuf[0..n];0is the end of the stream (or an emptybuf).Scatter read: the default reads into the first non-empty buffer.
Fill all of
buf, orUnexpectedEof.Append every remaining byte to
out, reading into its spare capacity (no zero fill first); the count.Every remaining byte.
Every remaining byte as text (
InvalidDataif not UTF-8).The remaining bytes one at a time. Each is one
read: wrap an unbuffered stream inBufReaderfirst.At most
limitmore bytes, then the end.bytesowning the stream: it may leave the scope or move to a task, closes the stream with itself, andinto_innergives it back.takeowning the stream.This stream to its end, then
next. The chain owns both and closes both.
BufReadSource
pub trait BufRead: ReadA Read with a buffer: line and delimiter reads cost no call per
byte.
The buffered bytes, refilled when empty; empty at the end. Valid until the next call on the stream.
Mark
nbuffered bytes as read; panics past what is buffered.Append bytes through
delim(included) toout; the count,0at the end.The next line,
\nincluded, appended toout; the byte count,0at the end.InvalidDataif the line is not UTF-8.Skip bytes through
delim(included); the count.The remaining lines, without
\n/\r\n.The pieces between
delimbytes,delimremoved.linesowning the stream (Read::into_bytes).splitowning the stream.
WriteSource
pub trait WriteA sink of bytes.
Write some prefix of
buf(astr, aList<u8>, aSlice<u8>, ..); its length.Hand buffered bytes on.
Gather write: the default writes the first non-empty buffer.
Write all of
buf;WriteZeroif a write takes nothing.Write an f-string piece by piece, its holes through
Displayinto the writer, never as onestrof the whole text:try w.write_fmt(f"{name}: {value}\n").Write
xasprintshows 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; aDisplaythat fails on its own isOther.
SeekSource
pub trait SeekMoves a byte cursor: files, Cursor and buffered readers.
Move the cursor; the new offset from the start.
Back to the start:
seek(SeekFrom::Start(0)).The offset from the start:
seek(SeekFrom::Current(0)).
AsyncReadSource
pub trait AsyncReadRead that suspends the task instead of blocking the thread.
As
Read::read.As
Read::read_vectored.As
Read::read_exact.As
Read::read_to_end_into.As
Read::read_to_end.As
Read::read_to_str.
Sourceconst fn bytes_async(§ref self) -> BytesStream The remaining bytes as a stream, one
read_asynceach.
Sourceconst fn into_bytes_async(§self) -> Owned<BytesStream, Self> bytes_asyncowning the stream (Read::into_bytes).
AsyncBufReadSource
pub trait AsyncBufRead: AsyncReadBufRead that suspends the task.
As
BufRead::fill_buf.As
BufRead::consume(it never waits).As
BufRead::read_until.As
BufRead::read_line.
Sourceconst fn lines_async(§ref self) -> LinesStream The remaining lines as a stream, without
\n/\r\n.
Sourceconst fn into_lines_async(§self) -> Owned<LinesStream, Self> lines_asyncowning the stream (Read::into_bytes).
AsyncWriteSource
pub trait AsyncWriteWrite that suspends the task.
As
Write::write.As
Write::flush.As
Write::write_vectored.As
Write::write_all.
AsyncSeekSource
pub trait AsyncSeekSeek that suspends the task.
As
Seek::seek.
AsyncCloseSource
pub trait AsyncCloseClose that suspends the task while the resource closes.
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.