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>
impl<T: AsRef<[u8]>> Cursor<T>
Sourcepub fn new(buffer: T) -> Self
pub fn new(buffer: T) -> Self
Constructs a new cursor object on top of a slice.
Examples found in repository?
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}
Sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
Returns the remaining length in bytes of the underlying buffer.
Examples found in repository?
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}
Sourcepub fn check_remaining(&self, expected: usize) -> Result<(), BufferTooSmall>
pub fn check_remaining(&self, expected: usize) -> Result<(), BufferTooSmall>
Checks if the underlying buffer has the expected amount of space left.
Sourcepub fn position(&self) -> usize
pub fn position(&self) -> usize
Returns the cursor position in the underlying buffer.
Examples found in repository?
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}
Sourcepub fn set_position(&mut self, position: usize)
pub fn set_position(&mut self, position: usize)
Sets the cursor position in the underlying buffer.
Examples found in repository?
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}
Sourcepub fn read_bytes(&mut self, dst: &mut [u8])
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?
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}
Sourcepub fn read_u8(&mut self) -> u8
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?
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}
Sourcepub fn read_u16<B: ByteOrder>(&mut self) -> u16
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?
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}
Sourcepub fn read_u24<B: ByteOrder>(&mut self) -> u32
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.
Sourcepub fn read_u32<B: ByteOrder>(&mut self) -> u32
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?
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}
Sourcepub fn read_u64<B: ByteOrder>(&mut self) -> u64
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.
Sourcepub fn peek_u8(&self, offset: usize) -> u8
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.
Sourcepub fn peek_u16<B: ByteOrder>(&self, offset: usize) -> u16
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§impl Cursor<&mut [u8]>
impl Cursor<&mut [u8]>
Sourcepub fn write_bytes(&mut self, src: &[u8])
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?
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}
Sourcepub fn write_u8(&mut self, val: u8)
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?
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}
Sourcepub fn write_u16<B: ByteOrder>(&mut self, val: u16)
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?
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}
Sourcepub fn write_u24<B: ByteOrder>(&mut self, val: u32)
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.
Sourcepub fn write_u32<B: ByteOrder>(&mut self, val: u32)
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?
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}