Struct otter_api_tests::io::Cursor1.0.0[][src]

pub struct Cursor<T> { /* fields omitted */ }
Expand description

A Cursor wraps an in-memory buffer and provides it with a Seek implementation.

Cursors are used with in-memory buffers, anything implementing AsRef<[u8]>, to allow them to implement Read and/or Write, allowing these buffers to be used anywhere you might use a reader or writer that does actual I/O.

The standard library implements some I/O traits on various types which are commonly used as a buffer, like Cursor<Vec<u8>> and Cursor<&[u8]>.

Examples

We may want to write bytes to a File in our production code, but use an in-memory buffer in our tests. We can do this with Cursor:

use std::io::prelude::*;
use std::io::{self, SeekFrom};
use std::fs::File;

// a library function we've written
fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> io::Result<()> {
    writer.seek(SeekFrom::End(-10))?;

    for i in 0..10 {
        writer.write(&[i])?;
    }

    // all went well
    Ok(())
}

// Here's some code that uses this library function.
//
// We might want to use a BufReader here for efficiency, but let's
// keep this example focused.
let mut file = File::create("foo.txt")?;

write_ten_bytes_at_end(&mut file)?;

// now let's write a test
#[test]
fn test_writes_bytes() {
    // setting up a real File is much slower than an in-memory buffer,
    // let's use a cursor instead
    use std::io::Cursor;
    let mut buff = Cursor::new(vec![0; 15]);

    write_ten_bytes_at_end(&mut buff).unwrap();

    assert_eq!(&buff.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}

Implementations

Creates a new cursor wrapping the provided underlying in-memory buffer.

Cursor initial position is 0 even if underlying buffer (e.g., Vec) is not empty. So writing to cursor starts with overwriting Vec content, not with appending to it.

Examples

use std::io::Cursor;

let buff = Cursor::new(Vec::new());

Consumes this cursor, returning the underlying value.

Examples

use std::io::Cursor;

let buff = Cursor::new(Vec::new());

let vec = buff.into_inner();

Gets a reference to the underlying value in this cursor.

Examples

use std::io::Cursor;

let buff = Cursor::new(Vec::new());

let reference = buff.get_ref();

Gets a mutable reference to the underlying value in this cursor.

Care should be taken to avoid modifying the internal I/O state of the underlying value as it may corrupt this cursor’s position.

Examples

use std::io::Cursor;

let mut buff = Cursor::new(Vec::new());

let reference = buff.get_mut();

Returns the current position of this cursor.

Examples

use std::io::Cursor;
use std::io::prelude::*;
use std::io::SeekFrom;

let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);

assert_eq!(buff.position(), 0);

buff.seek(SeekFrom::Current(2)).unwrap();
assert_eq!(buff.position(), 2);

buff.seek(SeekFrom::Current(-1)).unwrap();
assert_eq!(buff.position(), 1);

Sets the position of this cursor.

Examples

use std::io::Cursor;

let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);

assert_eq!(buff.position(), 0);

buff.set_position(2);
assert_eq!(buff.position(), 2);

buff.set_position(4);
assert_eq!(buff.position(), 4);

Trait Implementations

Attempts to return the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to poll_read. Read more

Attempts to read from the AsyncRead into buf. Read more

Attempts to seek to an offset, in bytes, in a stream. Read more

Waits for a seek operation to complete. Read more

Attempt to write bytes from buf into the object. Read more

Like poll_write, except that it writes from a slice of buffers. Read more

Determines if this writer has an efficient poll_write_vectored implementation. Read more

Attempts to flush the object, ensuring that any buffered data reach their destination. Read more

Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down. Read more

Attempt to write bytes from buf into the object. Read more

Like poll_write, except that it writes from a slice of buffers. Read more

Determines if this writer has an efficient poll_write_vectored implementation. Read more

Attempts to flush the object, ensuring that any buffered data reach their destination. Read more

Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down. Read more

Attempt to write bytes from buf into the object. Read more

Like poll_write, except that it writes from a slice of buffers. Read more

Determines if this writer has an efficient poll_write_vectored implementation. Read more

Attempts to flush the object, ensuring that any buffered data reach their destination. Read more

Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down. Read more

Attempt to write bytes from buf into the object. Read more

Like poll_write, except that it writes from a slice of buffers. Read more

Determines if this writer has an efficient poll_write_vectored implementation. Read more

Attempts to flush the object, ensuring that any buffered data reach their destination. Read more

Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down. Read more

Returns the number of bytes between the current position and the end of the buffer. Read more

Returns a slice starting at the current position and of length between 0 and Buf::remaining(). Note that this can return shorter slice (this allows non-continuous internal representation). Read more

Advance the internal cursor of the Buf Read more

Fills dst with potentially multiple slices starting at self’s current position. Read more

Returns true if there are any more bytes to consume Read more

Copies bytes from self into dst. Read more

Gets an unsigned 8 bit integer from self. Read more

Gets a signed 8 bit integer from self. Read more

Gets an unsigned 16 bit integer from self in big-endian byte order. Read more

Gets an unsigned 16 bit integer from self in little-endian byte order. Read more

Gets a signed 16 bit integer from self in big-endian byte order. Read more

Gets a signed 16 bit integer from self in little-endian byte order. Read more

Gets an unsigned 32 bit integer from self in the big-endian byte order. Read more

Gets an unsigned 32 bit integer from self in the little-endian byte order. Read more

Gets a signed 32 bit integer from self in big-endian byte order. Read more

Gets a signed 32 bit integer from self in little-endian byte order. Read more

Gets an unsigned 64 bit integer from self in big-endian byte order. Read more

Gets an unsigned 64 bit integer from self in little-endian byte order. Read more

Gets a signed 64 bit integer from self in big-endian byte order. Read more

Gets a signed 64 bit integer from self in little-endian byte order. Read more

Gets an unsigned 128 bit integer from self in big-endian byte order. Read more

Gets an unsigned 128 bit integer from self in little-endian byte order. Read more

Gets a signed 128 bit integer from self in big-endian byte order. Read more

Gets a signed 128 bit integer from self in little-endian byte order. Read more

Gets an unsigned n-byte integer from self in big-endian byte order. Read more

Gets an unsigned n-byte integer from self in little-endian byte order. Read more

Gets a signed n-byte integer from self in big-endian byte order. Read more

Gets a signed n-byte integer from self in little-endian byte order. Read more

Gets an IEEE754 single-precision (4 bytes) floating point number from self in big-endian byte order. Read more

Gets an IEEE754 single-precision (4 bytes) floating point number from self in little-endian byte order. Read more

Gets an IEEE754 double-precision (8 bytes) floating point number from self in big-endian byte order. Read more

Gets an IEEE754 double-precision (8 bytes) floating point number from self in little-endian byte order. Read more

Consumes len bytes inside self and returns new instance of Bytes with this data. Read more

Creates an adaptor which will read at most limit bytes from self. Read more

Creates an adaptor which will chain this buffer with another. Read more

Creates an adaptor which implements the Read trait for self. Read more

Returns the contents of the internal buffer, filling it with more data from the inner reader if it is empty. Read more

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to read. Read more

Read all bytes into buf until the delimiter byte or EOF is reached. Read more

Read all bytes until a newline (the 0xA byte) is reached, and append them to the provided buffer. Read more

Returns an iterator over the contents of this reader split on the byte byte. Read more

Returns an iterator over the lines of this reader. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more

Like read, except that it reads into a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Reader has an efficient read_vectored implementation. Read more

Read the exact number of bytes required to fill buf. Read more

🔬 This is a nightly-only experimental API. (read_initializer)

Determines if this Reader can work with buffers of uninitialized memory. Read more

Read all bytes until EOF in this source, placing them into buf. Read more

Read all bytes until EOF in this source, appending them to buf. Read more

Creates a “by reference” adaptor for this instance of Read. Read more

Transforms this Read instance to an Iterator over its bytes. Read more

Creates an adaptor which will chain this stream with another. Read more

Creates an adaptor which will read at most limit bytes from it. Read more

Seek to an offset, in bytes, in a stream. Read more

🔬 This is a nightly-only experimental API. (seek_stream_len)

Returns the length of this stream (in bytes). Read more

Returns the current seek position from the start of the stream. Read more

🔬 This is a nightly-only experimental API. (seek_rewind)

Rewind to the beginning of a stream. Read more

Write a buffer into this writer, returning how many bytes were written. Read more

Like write, except that it writes from a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Attempts to write an entire buffer into this writer. Read more

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a “by reference” adaptor for this instance of Write. Read more

Write a buffer into this writer, returning how many bytes were written. Read more

Like write, except that it writes from a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Attempts to write an entire buffer into this writer. Read more

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a “by reference” adaptor for this instance of Write. Read more

Write a buffer into this writer, returning how many bytes were written. Read more

Like write, except that it writes from a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Attempts to write an entire buffer into this writer. Read more

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a “by reference” adaptor for this instance of Write. Read more

Write a buffer into this writer, returning how many bytes were written. Read more

Like write, except that it writes from a slice of buffers. Read more

🔬 This is a nightly-only experimental API. (can_vector)

Determines if this Writer has an efficient write_vectored implementation. Read more

Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Attempts to write an entire buffer into this writer. Read more

🔬 This is a nightly-only experimental API. (write_all_vectored)

Attempts to write multiple buffers into this writer. Read more

Writes a formatted string into this writer, returning any error encountered. Read more

Creates a “by reference” adaptor for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Reads all bytes into buf until the delimiter byte or EOF is reached. Read more

Reads all bytes until a newline (the 0xA byte) is reached, and append them to the provided buffer. Read more

Returns a stream of the contents of this reader split on the byte byte. Read more

Returns a stream over the lines of this reader. This method is the async equivalent to BufRead::lines. Read more

Creates a new AsyncRead instance that chains this stream with next. Read more

Pulls some bytes from this source into the specified buffer, returning how many bytes were read. Read more

Pulls some bytes from this source into the specified buffer, advancing the buffer’s internal cursor. Read more

Reads the exact number of bytes required to fill buf. Read more

Reads an unsigned 8 bit integer from the underlying reader. Read more

Reads a signed 8 bit integer from the underlying reader. Read more

Reads an unsigned 16-bit integer in big-endian order from the underlying reader. Read more

Reads a signed 16-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 32-bit integer in big-endian order from the underlying reader. Read more

Reads a signed 32-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 64-bit integer in big-endian order from the underlying reader. Read more

Reads an signed 64-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 128-bit integer in big-endian order from the underlying reader. Read more

Reads an signed 128-bit integer in big-endian order from the underlying reader. Read more

Reads an unsigned 16-bit integer in little-endian order from the underlying reader. Read more

Reads a signed 16-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 32-bit integer in little-endian order from the underlying reader. Read more

Reads a signed 32-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 64-bit integer in little-endian order from the underlying reader. Read more

Reads an signed 64-bit integer in little-endian order from the underlying reader. Read more

Reads an unsigned 128-bit integer in little-endian order from the underlying reader. Read more

Reads an signed 128-bit integer in little-endian order from the underlying reader. Read more

Reads all bytes until EOF in this source, placing them into buf. Read more

Reads all bytes until EOF in this source, appending them to buf. Read more

Creates an adaptor which reads at most limit bytes from it. Read more

Creates a future which will seek an IO object, and then yield the new position in the object and the object itself. Read more

Creates a future which will return the current seek position from the start of the stream. Read more

Writes a buffer into this writer, returning how many bytes were written. Read more

Like write, except that it writes from a slice of buffers. Read more

Writes a buffer into this writer, advancing the buffer’s internal cursor. Read more

Attempts to write an entire buffer into this writer Read more

Attempts to write an entire buffer into this writer. Read more

Writes an unsigned 8-bit integer to the underlying writer. Read more

Writes an unsigned 8-bit integer to the underlying writer. Read more

Writes an unsigned 16-bit integer in big-endian order to the underlying writer. Read more

Writes a signed 16-bit integer in big-endian order to the underlying writer. Read more

Writes an unsigned 32-bit integer in big-endian order to the underlying writer. Read more

Writes a signed 32-bit integer in big-endian order to the underlying writer. Read more

Writes an unsigned 64-bit integer in big-endian order to the underlying writer. Read more

Writes an signed 64-bit integer in big-endian order to the underlying writer. Read more

Writes an unsigned 128-bit integer in big-endian order to the underlying writer. Read more

Writes an signed 128-bit integer in big-endian order to the underlying writer. Read more

Writes an unsigned 16-bit integer in little-endian order to the underlying writer. Read more

Writes a signed 16-bit integer in little-endian order to the underlying writer. Read more

Writes an unsigned 32-bit integer in little-endian order to the underlying writer. Read more

Writes a signed 32-bit integer in little-endian order to the underlying writer. Read more

Writes an unsigned 64-bit integer in little-endian order to the underlying writer. Read more

Writes an signed 64-bit integer in little-endian order to the underlying writer. Read more

Writes an unsigned 128-bit integer in little-endian order to the underlying writer. Read more

Writes an signed 128-bit integer in little-endian order to the underlying writer. Read more

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more

Shuts down the output stream, ensuring that the value can be dropped cleanly. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns an iterator over the lines of this reader, where each line is represented as a byte string. Read more

Returns an iterator over byte-terminated records of this reader, where each record is represented as a byte string. Read more

Executes the given closure on each line in the underlying reader. Read more

Executes the given closure on each byte-terminated record in the underlying reader. Read more

Executes the given closure on each line in the underlying reader. Read more

Executes the given closure on each byte-terminated record in the underlying reader. Read more

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait. Read more

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait. Read more

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s. Read more

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s. Read more

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait. Read more

Use this to cast from one trait object type to another. Read more

Use this to upcast a trait to one of its supertraits. Read more

Use this to cast from one trait object type to another. This method is more customizable than the dyn_cast method. Here you can also specify the “source” trait from which the cast is defined. This can for example allow using casts from a supertrait of the current trait object. Read more

Use this to cast from one trait object type to another. With this method the type parameter is a config type that uniquely specifies which cast should be preformed. Read more

Compare self to key and return true if they are equal.

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Reads an unsigned 8 bit integer from the underlying reader. Read more

Reads a signed 8 bit integer from the underlying reader. Read more

Reads an unsigned 16 bit integer from the underlying reader. Read more

Reads a signed 16 bit integer from the underlying reader. Read more

Reads an unsigned 24 bit integer from the underlying reader. Read more

Reads a signed 24 bit integer from the underlying reader. Read more

Reads an unsigned 32 bit integer from the underlying reader. Read more

Reads a signed 32 bit integer from the underlying reader. Read more

Reads an unsigned 48 bit integer from the underlying reader. Read more

Reads a signed 48 bit integer from the underlying reader. Read more

Reads an unsigned 64 bit integer from the underlying reader. Read more

Reads a signed 64 bit integer from the underlying reader. Read more

Reads an unsigned 128 bit integer from the underlying reader. Read more

Reads a signed 128 bit integer from the underlying reader. Read more

Reads an unsigned n-bytes integer from the underlying reader. Read more

Reads a signed n-bytes integer from the underlying reader. Read more

Reads an unsigned n-bytes integer from the underlying reader.

Reads a signed n-bytes integer from the underlying reader.

Reads a IEEE754 single-precision (4 bytes) floating point number from the underlying reader. Read more

Reads a IEEE754 double-precision (8 bytes) floating point number from the underlying reader. Read more

Reads a sequence of unsigned 16 bit integers from the underlying reader. Read more

Reads a sequence of unsigned 32 bit integers from the underlying reader. Read more

Reads a sequence of unsigned 64 bit integers from the underlying reader. Read more

Reads a sequence of unsigned 128 bit integers from the underlying reader. Read more

Reads a sequence of signed 8 bit integers from the underlying reader. Read more

Reads a sequence of signed 16 bit integers from the underlying reader. Read more

Reads a sequence of signed 32 bit integers from the underlying reader. Read more

Reads a sequence of signed 64 bit integers from the underlying reader. Read more

Reads a sequence of signed 128 bit integers from the underlying reader. Read more

Reads a sequence of IEEE754 single-precision (4 bytes) floating point numbers from the underlying reader. Read more

👎 Deprecated since 1.2.0:

please use read_f32_into instead

DEPRECATED. Read more

Reads a sequence of IEEE754 double-precision (8 bytes) floating point numbers from the underlying reader. Read more

👎 Deprecated since 1.2.0:

please use read_f64_into instead

DEPRECATED. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Writes an unsigned 8 bit integer to the underlying writer. Read more

Writes a signed 8 bit integer to the underlying writer. Read more

Writes an unsigned 16 bit integer to the underlying writer. Read more

Writes a signed 16 bit integer to the underlying writer. Read more

Writes an unsigned 24 bit integer to the underlying writer. Read more

Writes a signed 24 bit integer to the underlying writer. Read more

Writes an unsigned 32 bit integer to the underlying writer. Read more

Writes a signed 32 bit integer to the underlying writer. Read more

Writes an unsigned 48 bit integer to the underlying writer. Read more

Writes a signed 48 bit integer to the underlying writer. Read more

Writes an unsigned 64 bit integer to the underlying writer. Read more

Writes a signed 64 bit integer to the underlying writer. Read more

Writes an unsigned 128 bit integer to the underlying writer.

Writes a signed 128 bit integer to the underlying writer.

Writes an unsigned n-bytes integer to the underlying writer. Read more

Writes a signed n-bytes integer to the underlying writer. Read more

Writes an unsigned n-bytes integer to the underlying writer. Read more

Writes a signed n-bytes integer to the underlying writer. Read more

Writes a IEEE754 single-precision (4 bytes) floating point number to the underlying writer. Read more

Writes a IEEE754 double-precision (8 bytes) floating point number to the underlying writer. Read more