Cursor in std::io - Rust (original) (raw)
Struct Cursor
1.0.0 · Source
pub struct Cursor<T> { /* private fields */ }
Expand description
A Cursor
wraps an in-memory buffer and provides it with aSeek implementation.
Cursor
s are used with in-memory buffers, anything implementing[AsRef](../convert/trait.AsRef.html "trait std::convert::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](../vec/struct.Vec.html "struct std::vec::Vec")<u8>>
andCursor<[&[u8]](../slice/index.html "slice")>
.
§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 withCursor
:
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>(mut writer: 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")?;
// First, we need to allocate 10 bytes to be able to write into.
file.set_len(10)?;
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]);
}
1.0.0 (const: 1.79.0) · Source
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 Veccontent, not with appending to it.
§Examples
use std::io::Cursor;
let buff = Cursor::new(Vec::new());
1.0.0 · Source
Consumes this cursor, returning the underlying value.
§Examples
use std::io::Cursor;
let buff = Cursor::new(Vec::new());
let vec = buff.into_inner();
1.0.0 (const: 1.79.0) · Source
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();
1.0.0 (const: 1.86.0) · Source
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();
1.0.0 (const: 1.79.0) · Source
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);
1.0.0 (const: 1.86.0) · Source
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);
🔬This is a nightly-only experimental API. (cursor_split
#86369)
Splits the underlying slice at the cursor position and returns them.
§Examples
#![feature(cursor_split)]
use std::io::Cursor;
let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
assert_eq!(buff.split(), ([].as_slice(), [1, 2, 3, 4, 5].as_slice()));
buff.set_position(2);
assert_eq!(buff.split(), ([1, 2].as_slice(), [3, 4, 5].as_slice()));
buff.set_position(6);
assert_eq!(buff.split(), ([1, 2, 3, 4, 5].as_slice(), [].as_slice()));
🔬This is a nightly-only experimental API. (cursor_split
#86369)
Splits the underlying slice at the cursor position and returns them mutably.
§Examples
#![feature(cursor_split)]
use std::io::Cursor;
let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);
assert_eq!(buff.split_mut(), ([].as_mut_slice(), [1, 2, 3, 4, 5].as_mut_slice()));
buff.set_position(2);
assert_eq!(buff.split_mut(), ([1, 2].as_mut_slice(), [3, 4, 5].as_mut_slice()));
buff.set_position(6);
assert_eq!(buff.split_mut(), ([1, 2, 3, 4, 5].as_mut_slice(), [].as_mut_slice()));
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
🔬This is a nightly-only experimental API. (buf_read_has_data_left
#86423)
Checks if the underlying Read
has any data left to be read. Read more
Reads all bytes into buf
until the delimiter byte
or EOF is reached. Read more
Skips all bytes 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 String
buffer. Read more
Returns an iterator over the contents of this reader split on the bytebyte
. Read more
Returns an iterator over the lines of this reader. Read more
Tests for self
and other
values to be equal, and is used by ==
.
Tests for !=
. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
🔬This is a nightly-only experimental API. (read_buf
#78485)
Pull some bytes from this source into the specified buffer. Read more
Like read
, except that it reads into a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector
#69941)
Determines if this Read
er has an efficient read_vectored
implementation. Read more
Reads the exact number of bytes required to fill buf
. Read more
🔬This is a nightly-only experimental API. (read_buf
#78485)
Reads the exact number of bytes required to fill cursor
. 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 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 adapter which will chain this stream with another. Read more
Creates an adapter which will read at most limit
bytes from it. 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
🔬This is a nightly-only experimental API. (can_vector
#69941)
Flushes 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
#70436)
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” adapter for this instance of Write
. 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
🔬This is a nightly-only experimental API. (can_vector
#69941)
Flushes 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
#70436)
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” adapter for this instance of Write
. 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
🔬This is a nightly-only experimental API. (can_vector
#69941)
Flushes 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
#70436)
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” adapter for this instance of Write
. 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
🔬This is a nightly-only experimental API. (can_vector
#69941)
Flushes 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
#70436)
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” adapter for this instance of Write
. 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
🔬This is a nightly-only experimental API. (can_vector
#69941)
Flushes 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
#70436)
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” adapter for this instance of Write
. Read more