pub struct Cursor<T> { /* private fields */ }
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.
acid_io
implements some I/O traits on various types which are commonly
used as a buffer, like Cursor<Vec<u8>>
and Cursor<&[u8]>
.
Examples
use acid_io::{self, Read, Seek, SeekFrom, Write};
// a library function we've written
fn write_ten_bytes_at_end<W: Write + Seek>(writer: &mut W) -> acid_io::Result<()> {
writer.seek(SeekFrom::End(-10))?;
for i in 0..10 {
writer.write(&[i])?;
}
// all went well
Ok(())
}
// now let's write a test
#[test]
fn test_writes_bytes() {
use acid_io::Cursor;
let mut buf = Cursor::new(vec![0; 15]);
write_ten_bytes_at_end(&mut buf).unwrap();
assert_eq!(&buf.get_ref()[5..15], &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}
Implementations§
source§impl<T> Cursor<T>
impl<T> Cursor<T>
sourcepub const fn new(inner: T) -> Cursor<T>
pub const fn new(inner: T) -> Cursor<T>
Creates a new cursor wrapping the provided underlying in-memory buffer.
The initial position of the cursor is 0
even if the underlying buffer
is not empty.
Examples
use acid_io::Cursor;
let buf = Cursor::new(Vec::new());
sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes this cursor, returning the underlying value.
Examples
use acid_io::Cursor;
let buf = Cursor::new(Vec::new());
let vec = buf.into_inner();
sourcepub const fn get_ref(&self) -> &T
pub const fn get_ref(&self) -> &T
Gets a reference to the underlying value in this cursor.
Examples
use acid_io::Cursor;
let buf = Cursor::new(Vec::new());
let reference = buf.get_ref();
sourcepub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
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 acid_io::Cursor;
let mut buf = Cursor::new(Vec::new());
let reference = buf.get_mut();
sourcepub const fn position(&self) -> u64
pub const fn position(&self) -> u64
Returns the current position of this cursor.
Examples
use acid_io::prelude::*;
use acid_io::{Cursor, SeekFrom};
let mut buf = Cursor::new(vec![1, 2, 3, 4, 5]);
assert_eq!(buf.position(), 0);
buf.seek(SeekFrom::Current(2)).unwrap();
assert_eq!(buf.position(), 2);
buf.seek(SeekFrom::Current(-1)).unwrap();
assert_eq!(buf.position(), 1);
sourcepub fn set_position(&mut self, pos: u64)
pub fn set_position(&mut self, pos: u64)
Sets the position of this cursor.
Examples
use acid_io::Cursor;
let mut buf = Cursor::new(vec![1, 2, 3, 4, 5]);
assert_eq!(buf.position(), 0);
buf.set_position(2);
assert_eq!(buf.position(), 2);
buf.set_position(4);
assert_eq!(buf.position(), 4);
source§impl<T> Cursor<T>where
T: AsRef<[u8]>,
impl<T> Cursor<T>where T: AsRef<[u8]>,
sourcepub fn remaining_slice(&self) -> &[u8]
pub fn remaining_slice(&self) -> &[u8]
Returns the remaining slice.
Examples
use acid_io::Cursor;
let mut buf = Cursor::new(vec![1, 2, 3, 4, 5]);
assert_eq!(buf.remaining_slice(), &[1, 2, 3, 4, 5]);
buf.set_position(2);
assert_eq!(buf.remaining_slice(), &[3, 4, 5]);
buf.set_position(4);
assert_eq!(buf.remaining_slice(), &[5]);
buf.set_position(6);
assert_eq!(buf.remaining_slice(), &[]);
Trait Implementations§
source§impl<T> BufRead for Cursor<T>where
T: AsRef<[u8]>,
impl<T> BufRead for Cursor<T>where T: AsRef<[u8]>,
source§fn fill_buf(&mut self) -> Result<&[u8], Error>
fn fill_buf(&mut self) -> Result<&[u8], Error>
source§fn consume(&mut self, amt: usize)
fn consume(&mut self, amt: usize)
amt
bytes have been consumed from the buffer,
so they should no longer be returned in calls to read
. Read moresource§fn read_until(
&mut self,
byte: u8,
buf: &mut Vec<u8, Global>
) -> Result<usize, Error>
fn read_until( &mut self, byte: u8, buf: &mut Vec<u8, Global> ) -> Result<usize, Error>
source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA
byte) is reached, and append
them to the provided buffer. Read moresource§impl<T> Read for Cursor<T>where
T: AsRef<[u8]>,
impl<T> Read for Cursor<T>where T: AsRef<[u8]>,
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
buf
. Read moresource§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read moresource§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere Self: Sized,
Read
. Read more