Streams and other byte I/O functionality
Modules
Protocols
Structs
/dev/null
).
limit
bytes are read.
Functions
-
fn take<R>(self: &mut R, limit: u64) -> TakeAdapter<R>
R: Readable<R>Returns an adapter for a stream that returns EOF after
limit
bytes have been read.Example
use std::io::{SliceReader, take}; use std::string::StringBuf; let reader = SliceReader::new("Hello, world!"); let buf = StringBuf::new(); defer buf.free(); reader.take(5).read_to_end(&buf).unwrap(); assert_eq!(buf[..] as &[u8], "Hello");
Run this example -
fn chain<R1, R2>(self: &mut R1, other: &mut R2) -> ChainAdapter<R1, R2>
R1: Readable<R1>R2: Readable<R2>Chains the two streams together
The resulting stream will first read all bytes from
self
, then all bytes fromother
.Example
use std::io::{SliceReader, chain}; use std::string::StringBuf; let reader1 = SliceReader::new("Hello, "); let reader2 = SliceReader::new("world!"); let buf = StringBuf::new(); defer buf.free(); reader1.chain(&reader2).read_to_end(&buf).unwrap(); assert_eq!(buf[..] as &[u8], "Hello, world!");
Run this example -
Copy the entire stream
src
intodst
Internally allocates a buffer with the size of DEFAULT_BUFFER_SIZE. If you want to use a custom buffer, use copy_using.
Example
use std::io::{copy, SliceReader, StringWriter}; use std::string::StringBuf; let str = StringBuf::new(); defer str.free(); let writer = StringWriter::new(&str); let reader = SliceReader::new("Hello, world!"); copy(&reader, &writer).unwrap(); assert_eq!(str[..] as &[u8], "Hello, world!");
Run this example -
fn copy_using<S, D>(src: &mut S, dst: &mut D, buffer: &mut [u8]) -> Result<u64>
S: Readable<S>D: Writable<D>Copy the entire stream
src
intodst
using a provided buffer. -
Reads a single byte from the stream.
Returns
Error::eof
if the stream has no bytes left. -
fn read_until<R>(r: &mut R, delim: u8, buf: &mut StringBuf) -> Result<usize>
R: BufferedReadable<R>Reads the stream into
buf
until a specific byte is encountered.The
delim
terminator is included.Example
use std::io::{SliceReader, read_until}; use std::string::StringBuf; let stream = SliceReader::new("Hello\nWorld\n"); let buf = StringBuf::new(); defer buf.free(); stream.read_until('\n', &buf).unwrap(); assert_eq!(buf[..] as &[u8], "Hello\n"); buf.clear(); stream.read_until('\n', &buf).unwrap(); assert_eq!(buf[..] as &[u8], "World\n");
Run this example -
fn lines<R>(reader: &mut R) -> LineIterator<R>
R: BufferedReadable<R>Return an iterator that yields the stream's contents line by line.
Both
\n
and\r\n
are considered line terminators and they are omitted from the yielded lines.The iterator allocates a buffer of the line's contents, so it should be freed when it is no longer needed.
Example
use std::io::{SliceReader, lines}; let stream = SliceReader::new("Hello\nWorld\n"); let lines = stream.lines(); defer lines.free(); assert_eq!(lines.next(), Option::some(Result::ok("Hello"))); assert_eq!(lines.next(), Option::some(Result::ok("World"))); assert_eq!(lines.next(), Option::none());
Run this example
Types
Enums
Macros
Consts
-
Minimum size of the buffer used for convenience I/O macros.
Convenience I/O macros such as print and println use a small buffer to format the output. This buffer is allocated on the stack and is mostly there to prevent formatters that write single characters from issuing a lot of syscalls.
The actual buffer size is the maximum of this constant and the length of the format string plus this constant.
There is no buffering between separate invocations of the macros, i.e. print will flush the buffer after each invocation.
If you require larger buffers, consider using the BufferedWriter type directly.
-
const DEFAULT_BUFFER_SIZE: usize = /* ... */
Default buffer size for I/O methods that allocate a buffer.