Struct Cursor

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

A std::io::Cursor like buffer interface with byteorder support and no_std compatibility.

Implementations§

Source§

impl<T: AsRef<[u8]>> Cursor<T>

Source

pub fn new(buffer: T) -> Self

Constructs a new cursor object on top of a slice.

Examples found in repository?
examples/basic.rs (line 5)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn len(&self) -> usize

Returns the length of the underlying buffer.

Source

pub fn remaining(&self) -> usize

Returns the remaining length in bytes of the underlying buffer.

Examples found in repository?
examples/basic.rs (line 12)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn check_remaining(&self, expected: usize) -> Result<(), BufferTooSmall>

Checks if the underlying buffer has the expected amount of space left.

Source

pub fn position(&self) -> usize

Returns the cursor position in the underlying buffer.

Examples found in repository?
examples/basic.rs (line 23)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn set_position(&mut self, position: usize)

Sets the cursor position in the underlying buffer.

Examples found in repository?
examples/basic.rs (line 13)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn skip(&mut self, count: usize)

Advances it cursor position by the given amount of bytes.

Source

pub fn read_bytes(&mut self, dst: &mut [u8])

Reads data from the underlying buffer to the given slice and advances cursor position. Panics if there is not enough data remaining to fill the slice.

Examples found in repository?
examples/basic.rs (line 20)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn read_u8(&mut self) -> u8

Reads a 8bit integer value from the underlying buffer and advances cursor position. Panics if there is not enough data remaining.

Examples found in repository?
examples/basic.rs (line 15)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn read_u16<B: ByteOrder>(&mut self) -> u16

Reads a 16bit integer value from the underlying buffer and advances cursor position. Panics if there is not enough data remaining.

Examples found in repository?
examples/basic.rs (line 16)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn read_u24<B: ByteOrder>(&mut self) -> u32

Reads a 24bit integer value from the underlying buffer and advances cursor position. Panics if there is not enough data remaining.

Source

pub fn read_u32<B: ByteOrder>(&mut self) -> u32

Reads a 32bit integer value from the underlying buffer and advances cursor position. Panics if there is not enough data remaining.

Examples found in repository?
examples/basic.rs (line 17)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn read_u64<B: ByteOrder>(&mut self) -> u64

Reads a 64bit integer value from the underlying buffer and advances cursor position. Panics if there is not enough data remaining.

Source

pub fn peek_u8(&self, offset: usize) -> u8

Reads a 8bit integer value from the underlying buffer at a given offset from the cursor position without advancing the cursor position. Panics if there is not enough data remaining.

Source

pub fn peek_u16<B: ByteOrder>(&self, offset: usize) -> u16

Reads a 16bit integer value from the underlying buffer at a given offset from the cursor position without advancing the cursor position. Panics if there is not enough data remaining.

Source

pub fn peek_u24<B: ByteOrder>(&self, offset: usize) -> u32

Reads a 24bit integer value from the underlying buffer at a given offset from the cursor position without advancing the cursor position. Panics if there is not enough data remaining.

Source

pub fn peek_u32<B: ByteOrder>(&self, offset: usize) -> u32

Reads a 32bit integer value from the underlying buffer at a given offset from the cursor position without advancing the cursor position. Panics if there is not enough data remaining.

Source§

impl Cursor<&mut [u8]>

Source

pub fn write_bytes(&mut self, src: &[u8])

Writes the given slice to the underlying buffer and advances cursor position. Panics if there is not enough space remaining to write the slice.

Examples found in repository?
examples/basic.rs (line 10)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn write_u8(&mut self, val: u8)

Writes a 8bit integer value to the underlying buffer and advances cursor position. Panics if there is not enough space remaining.

Examples found in repository?
examples/basic.rs (line 7)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn write_u16<B: ByteOrder>(&mut self, val: u16)

Writes a 16bit integer value to the underlying buffer and advances cursor position. Panics if there is not enough space remaining.

Examples found in repository?
examples/basic.rs (line 8)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn write_u24<B: ByteOrder>(&mut self, val: u32)

Writes a 24bit integer value to the underlying buffer and advances cursor position. Panics if there is not enough space remaining.

Source

pub fn write_u32<B: ByteOrder>(&mut self, val: u32)

Writes a 32bit integer value to the underlying buffer and advances cursor position. Panics if there is not enough space remaining.

Examples found in repository?
examples/basic.rs (line 9)
3fn main() {
4    let mut buffer = [0u8; 16];
5    let mut cursor = Cursor::new(&mut buffer[..]);
6
7    cursor.write_u8(1);
8    cursor.write_u16::<BigEndian>(5);
9    cursor.write_u32::<LittleEndian>(16);
10    cursor.write_bytes(&[1, 2, 3, 4, 5]);
11
12    println!("Remaining space in cursor is {} bytes", cursor.remaining());
13    cursor.set_position(0);
14
15    let x = cursor.read_u8();
16    let y = cursor.read_u16::<BigEndian>();
17    let z = cursor.read_u32::<LittleEndian>();
18
19    let mut w = [0u8; 5];
20    cursor.read_bytes(&mut w);
21
22    println!("Read-back from buffer: {x}, {y}, {z}, {w:?}");
23    println!("Cursor is now at position {}", cursor.position());
24}
Source

pub fn write_u64<B: ByteOrder>(&mut self, val: u64)

Writes a 64bit integer value to the underlying buffer and advances cursor position. Panics if there is not enough space remaining.

Trait Implementations§

Source§

impl<T: Debug> Debug for Cursor<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Cursor<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Cursor<T>
where T: RefUnwindSafe,

§

impl<T> Send for Cursor<T>
where T: Send,

§

impl<T> Sync for Cursor<T>
where T: Sync,

§

impl<T> Unpin for Cursor<T>
where T: Unpin,

§

impl<T> UnwindSafe for Cursor<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.