Struct std::io::MemWriter
pub struct MemWriter { // some fields omitted }
Writes to an owned, growable byte vector
Example
use std::io::MemWriter; let mut w = MemWriter::new(); w.write([0, 1, 2]); assert_eq!(w.unwrap(), ~[0, 1, 2]);
Methods
impl MemWriter
fn new() -> MemWriter
Create a new MemWriter
.
fn with_capacity(n: uint) -> MemWriter
Create a new MemWriter
, allocating at least n
bytes for
the internal buffer.
fn get_ref<'a>(&'a self) -> &'a [u8]
Acquires an immutable reference to the underlying buffer of this
MemWriter
.
No method is exposed for acquiring a mutable reference to the buffer
because it could corrupt the state of this MemWriter
.
fn unwrap(self) -> ~[u8]
Unwraps this MemWriter
, returning the underlying buffer
Trait Implementations
impl Writer for MemWriter
fn write(&mut self, buf: &[u8]) -> IoResult<()>
Write the entirety of a given buffer
Errors
If an error happens during the I/O operation, the error is returned as
Err
. Note that it is considered an error if the entire buffer could
not be written, and if an error is returned then it is unknown how much
data (if any) was actually written.
fn flush(&mut self) -> IoResult<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination.
This is by default a no-op and implementers of the Writer
trait should
decide whether their stream needs to be buffered or not.
fn write_str(&mut self, s: &str) -> IoResult<()>
Write a rust string into this sink.
The bytes written will be the UTF-8 encoded version of the input string.
If other encodings are desired, it is recommended to compose this stream
with another performing the conversion, or to use write
with a
converted byte-array instead.
fn write_line(&mut self, s: &str) -> IoResult<()>
Writes a string into this sink, and then writes a literal newline (\n
)
byte afterwards. Note that the writing of the newline is not atomic in
the sense that the call to write
is invoked twice (once with the
string and once with a newline character).
If other encodings or line ending flavors are desired, it is recommended
that the write
method is used specifically instead.
fn write_char(&mut self, c: char) -> IoResult<()>
Write a single char, encoded as UTF-8.
fn write_int(&mut self, n: int) -> IoResult<()>
Write the result of passing n through int::to_str_bytes
.
fn write_uint(&mut self, n: uint) -> IoResult<()>
Write the result of passing n through uint::to_str_bytes
.
fn write_le_uint(&mut self, n: uint) -> IoResult<()>
Write a little-endian uint (number of bytes depends on system).
fn write_le_int(&mut self, n: int) -> IoResult<()>
Write a little-endian int (number of bytes depends on system).
fn write_be_uint(&mut self, n: uint) -> IoResult<()>
Write a big-endian uint (number of bytes depends on system).
fn write_be_int(&mut self, n: int) -> IoResult<()>
Write a big-endian int (number of bytes depends on system).
fn write_be_u64(&mut self, n: u64) -> IoResult<()>
Write a big-endian u64 (8 bytes).
fn write_be_u32(&mut self, n: u32) -> IoResult<()>
Write a big-endian u32 (4 bytes).
fn write_be_u16(&mut self, n: u16) -> IoResult<()>
Write a big-endian u16 (2 bytes).
fn write_be_i64(&mut self, n: i64) -> IoResult<()>
Write a big-endian i64 (8 bytes).
fn write_be_i32(&mut self, n: i32) -> IoResult<()>
Write a big-endian i32 (4 bytes).
fn write_be_i16(&mut self, n: i16) -> IoResult<()>
Write a big-endian i16 (2 bytes).
fn write_be_f64(&mut self, f: f64) -> IoResult<()>
Write a big-endian IEEE754 double-precision floating-point (8 bytes).
fn write_be_f32(&mut self, f: f32) -> IoResult<()>
Write a big-endian IEEE754 single-precision floating-point (4 bytes).
fn write_le_u64(&mut self, n: u64) -> IoResult<()>
Write a little-endian u64 (8 bytes).
fn write_le_u32(&mut self, n: u32) -> IoResult<()>
Write a little-endian u32 (4 bytes).
fn write_le_u16(&mut self, n: u16) -> IoResult<()>
Write a little-endian u16 (2 bytes).
fn write_le_i64(&mut self, n: i64) -> IoResult<()>
Write a little-endian i64 (8 bytes).
fn write_le_i32(&mut self, n: i32) -> IoResult<()>
Write a little-endian i32 (4 bytes).
fn write_le_i16(&mut self, n: i16) -> IoResult<()>
Write a little-endian i16 (2 bytes).
fn write_le_f64(&mut self, f: f64) -> IoResult<()>
Write a little-endian IEEE754 double-precision floating-point (8 bytes).
fn write_le_f32(&mut self, f: f32) -> IoResult<()>
Write a little-endian IEEE754 single-precision floating-point (4 bytes).
fn write_u8(&mut self, n: u8) -> IoResult<()>
Write a u8 (1 byte).
fn write_i8(&mut self, n: i8) -> IoResult<()>
Write a i8 (1 byte).
fn by_ref<'a>(&'a mut self) -> RefWriter<'a, Self>
Creates a wrapper around a mutable reference to the writer.
This is useful to allow applying wrappers while still retaining ownership of the original value.
impl Seek for MemWriter
fn tell(&self) -> IoResult<u64>
Return position of file cursor in the stream
fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()>
Seek to an offset in a stream
A successful seek clears the EOF indicator. Seeking beyond EOF is allowed, but seeking before position 0 is not allowed.
Errors
- Seeking to a negative offset is considered an error
- Seeking past the end of the stream does not modify the underlying stream, but the next write may cause the previous data to be filled in with a bit pattern.