Struct Reader

Source
pub struct Reader<'a> { /* private fields */ }
Expand description

§Safety

The caller is responsible for ensuring that the bytes slice is valid.

§Examples

use core::array;
use solana_bytes_reader::{Reader, ReadBytes};
 
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let bytes: [u8; 32] = array::from_fn(|i| if i % 4 == 0 { i as u8 } else { 0u8 });
     
    let mut reader: Reader = Reader::new(&bytes);
    let first_u32: u32 = reader.read_u32()?;
    let second_u32: u32 = reader.read_u32()?;
    let third_u32: u32 = reader.read_u32()?;
 
    assert_eq!(first_u32, 0u32);
    assert_eq!(second_u32, 4u32);
    assert_eq!(third_u32, 8u32);
     
    Ok(())
}

It’s recommended to initialize Reader and use it’s methods if there are more than 2 method calls.

Otherwise there’s no need in this struct and functions can be used instead.

Implementations§

Source§

impl<'a> Reader<'a>

Source

pub fn new(bytes: &'a [u8]) -> Self

§Panics

if bytes slice is empty.

Source

pub fn new_with_offset(bytes: &'a [u8], offset: usize) -> Self

§Panics

if offset equals/greater than bytes slice OR if bytes slice is empty.

Source

pub fn bytes(&self) -> &'a [u8]

Source

pub fn offset(&self) -> usize

Source

pub fn remaining(&self) -> usize

Returns remaining bytes.

Source

pub fn set_offset(&mut self, new_offset: usize)

§Panics

if new_offset equals/greater than bytes slice.

Trait Implementations§

Source§

impl<'a> Clone for Reader<'a>

Source§

fn clone(&self) -> Reader<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Reader<'a>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PeekIntoBytes for Reader<'_>

Source§

type Error = ProgramError

Source§

fn peek_u64(&self) -> Result<u64, Self::Error>

Safety Read more
Source§

fn peek_i64(&self) -> Result<i64, Self::Error>

Safety Read more
Source§

fn peek_u32(&self) -> Result<u32, Self::Error>

Safety Read more
Source§

fn peek_i32(&self) -> Result<i32, Self::Error>

Safety Read more
Source§

fn peek_u16(&self) -> Result<u16, Self::Error>

Safety Read more
Source§

fn peek_i16(&self) -> Result<i16, Self::Error>

Safety Read more
Source§

fn peek_bool(&self) -> Result<bool, ProgramError>

Safety Read more
Source§

fn peek_u8(&self) -> Result<u8, Self::Error>

Safety Read more
Source§

fn peek_i8(&self) -> Result<i8, Self::Error>

Safety Read more
Source§

fn peek_bytes<const N: usize>(&self) -> Result<[u8; N], Self::Error>

Peeks into const N amount of bytes. Read more
Source§

impl ReadBytes for Reader<'_>

Source§

type Error = ProgramError

Source§

fn read_u64(&mut self) -> Result<u64, Self::Error>

Safety Read more
Source§

fn read_i64(&mut self) -> Result<i64, Self::Error>

Safety Read more
Source§

fn read_u32(&mut self) -> Result<u32, Self::Error>

Safety Read more
Source§

fn read_i32(&mut self) -> Result<i32, Self::Error>

Safety Read more
Source§

fn read_u16(&mut self) -> Result<u16, Self::Error>

Safety Read more
Source§

fn read_i16(&mut self) -> Result<i16, Self::Error>

Safety Read more
Source§

fn read_bool(&mut self) -> Result<bool, Self::Error>

Safety Read more
Source§

fn read_u8(&mut self) -> Result<u8, Self::Error>

Safety Read more
Source§

fn read_i8(&mut self) -> Result<i8, Self::Error>

Safety Read more
Source§

fn read_bytes<const N: usize>(&mut self) -> Result<[u8; N], Self::Error>

Reads const N amount of bytes. Read more
Source§

fn skip(&mut self, bytes_to_skip: usize)

Increments self.offset by bytes_to_skip. Read more
Source§

impl<'a> Copy for Reader<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> RefUnwindSafe for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnwindSafe for Reader<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.