pub struct Cursor<T> { }
Expand description
A Cursor
wraps an in-memory buffer and provides it with a Seek
implementation.
Cursor
s 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]>
.
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;
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])?;
}
Ok(())
}
let mut file = File::create("foo.txt")?;
file.set_len(10)?;
write_ten_bytes_at_end(&mut file)?;
#[test]
fn test_writes_bytes() {
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]);
}
Source§ 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 Vec
content, not with appending to it.
use std::io::Cursor;
let buff = Cursor::new(Vec::new());
1.0.0 · Source
Consumes this cursor, returning the underlying value.
§Examplesuse 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.
§Examplesuse 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.
§Examplesuse 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.
§Examplesuse 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.
§Examplesuse 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);
Source§ Source ð¬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()));
Source§ Source ð¬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()));
1.0.0 · Source§ Source§
Returns the contents of the internal buffer, filling it with more data, via
Read
methods, if empty.
Read more Source§Marks the given
amount
of additional bytes from the internal buffer as having been read. Subsequent calls to
read
only return bytes that have not been marked as read.
Read more Source§ ð¬This is a nightly-only experimental API. (buf_read_has_data_left
#86423)
Checks if there is any data left to be
read
.
Read more 1.0.0 · Source§Reads all bytes into
buf
until the delimiter
byte
or EOF is reached.
Read more 1.83.0 · Source§Skips all bytes until the delimiter
byte
or EOF is reached.
Read more 1.0.0 · Source§Reads all bytes until a newline (the
0xA
byte) is reached, and append them to the provided
String
buffer.
Read more 1.0.0 · Source§Returns an iterator over the contents of this reader split on the byte
byte
.
Read more 1.0.0 · Source§Returns an iterator over the lines of this reader.
Read more 1.0.0 · Source§ 1.0.0 · Source§ 1.0.0 · Source§ 1.0.0 · Source§ Source§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 Source§ ð¬This is a nightly-only experimental API. (read_buf
#78485)
Pull some bytes from this source into the specified buffer.
Read more Source§Like
read
, except that it reads into a slice of buffers.
Read more Source§ ð¬This is a nightly-only experimental API. (can_vector
#69941)
Determines if this
Read
er has an efficient
read_vectored
implementation.
Read more Source§Reads the exact number of bytes required to fill
buf
.
Read more Source§ ð¬This is a nightly-only experimental API. (read_buf
#78485)
Reads the exact number of bytes required to fill
cursor
.
Read more Source§Reads all bytes until EOF in this source, placing them into
buf
.
Read more Source§Reads all bytes until EOF in this source, appending them to
buf
.
Read more 1.0.0 · Source§Creates a âby referenceâ adaptor for this instance of
Read
.
Read more 1.0.0 · Source§ 1.0.0 · Source§Creates an adapter which will chain this stream with another.
Read more 1.0.0 · Source§Creates an adapter which will read at most
limit
bytes from it.
Read more 1.0.0 · Source§ 1.0.0 · Source§ Source§Writes a buffer into this writer, returning how many bytes were written.
Read more Source§ Source§ ð¬This is a nightly-only experimental API. (can_vector
#69941) Source§
Attempts to write an entire buffer into this writer.
Read more Source§ ð¬This is a nightly-only experimental API. (write_all_vectored
#70436)
Attempts to write multiple buffers into this writer.
Read more Source§Flushes this output stream, ensuring that all intermediately buffered contents reach their destination.
Read more 1.0.0 · Source§Writes a formatted string into this writer, returning any error encountered.
Read more 1.0.0 · Source§Creates a âby referenceâ adapter for this instance of
Write
.
Read more 1.25.0 · Source§ Source§Writes a buffer into this writer, returning how many bytes were written.
Read more Source§ Source§ ð¬This is a nightly-only experimental API. (can_vector
#69941) Source§
Attempts to write an entire buffer into this writer.
Read more Source§ ð¬This is a nightly-only experimental API. (write_all_vectored
#70436)
Attempts to write multiple buffers into this writer.
Read more Source§Flushes this output stream, ensuring that all intermediately buffered contents reach their destination.
Read more 1.0.0 · Source§Writes a formatted string into this writer, returning any error encountered.
Read more 1.0.0 · Source§Creates a âby referenceâ adapter for this instance of
Write
.
Read more 1.61.0 · Source§ Source§Writes a buffer into this writer, returning how many bytes were written.
Read more Source§ Source§ ð¬This is a nightly-only experimental API. (can_vector
#69941) Source§
Attempts to write an entire buffer into this writer.
Read more Source§ ð¬This is a nightly-only experimental API. (write_all_vectored
#70436)
Attempts to write multiple buffers into this writer.
Read more Source§Flushes this output stream, ensuring that all intermediately buffered contents reach their destination.
Read more 1.0.0 · Source§Writes a formatted string into this writer, returning any error encountered.
Read more 1.0.0 · Source§Creates a âby referenceâ adapter for this instance of
Write
.
Read more 1.5.0 · Source§ Source§Writes a buffer into this writer, returning how many bytes were written.
Read more Source§ Source§ ð¬This is a nightly-only experimental API. (can_vector
#69941) Source§
Attempts to write an entire buffer into this writer.
Read more Source§ ð¬This is a nightly-only experimental API. (write_all_vectored
#70436)
Attempts to write multiple buffers into this writer.
Read more Source§Flushes this output stream, ensuring that all intermediately buffered contents reach their destination.
Read more 1.0.0 · Source§Writes a formatted string into this writer, returning any error encountered.
Read more 1.0.0 · Source§Creates a âby referenceâ adapter for this instance of
Write
.
Read more 1.0.0 · Source§ Source§Writes a buffer into this writer, returning how many bytes were written.
Read more Source§ Source§ ð¬This is a nightly-only experimental API. (can_vector
#69941) Source§
Attempts to write an entire buffer into this writer.
Read more Source§ ð¬This is a nightly-only experimental API. (write_all_vectored
#70436)
Attempts to write multiple buffers into this writer.
Read more Source§Flushes this output stream, ensuring that all intermediately buffered contents reach their destination.
Read more 1.0.0 · Source§Writes a formatted string into this writer, returning any error encountered.
Read more 1.0.0 · Source§Creates a âby referenceâ adapter for this instance of
Write
.
Read more 1.0.0 · Source§ 1.0.0 · Source§RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4