Struct ByteReader

Source
pub struct ByteReader<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> ByteReader<'a>

Source

pub fn new(data: &'a [u8], endian: Endian) -> Self

Examples found in repository?
examples/transcode.rs (line 43)
18fn main() -> ByteReaderResult<()> {
19    #[rustfmt::skip]
20    let bytes: Vec<u8> = vec![
21        // Game ID (UUIDv4)
22        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
23
24        // Join Code (String)
25        4, 84, 111, 100, 100,
26
27        // Lobby size (u8)
28        2,
29
30        // Account 1
31        // (UUIDv4)
32        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
33        // (String)
34        4, 84, 111, 100, 100,
35
36        // Account 2
37        // (UUIDv4)
38        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
39        // (String)
40        4, 84, 111, 100, 100,
41    ];
42
43    let mut byte_reader: ByteReader = ByteReader::new(&bytes, Endian::Little);
44
45    let game_id: Uuid = byte_reader.read_uuid()?;
46    let join_code: String = byte_reader.read_string()?;
47
48    let mut lobby: Vec<Account> = Vec::new();
49    let lobby_len: u8 = byte_reader.read_u8()?;
50    for _ in 0..lobby_len {
51        lobby.push(Account {
52            account_id: byte_reader.read_uuid()?,
53            username: byte_reader.read_string()?,
54        });
55    }
56
57    println!(
58        "{:?}",
59        Payload {
60            game_id,
61            join_code,
62            lobby,
63        }
64    );
65    Ok(())
66}
Source

pub fn read_u8(&mut self) -> ByteReaderResult<u8>

§Errors

Will return an error if there are not enough bytes to read.

Examples found in repository?
examples/transcode.rs (line 49)
18fn main() -> ByteReaderResult<()> {
19    #[rustfmt::skip]
20    let bytes: Vec<u8> = vec![
21        // Game ID (UUIDv4)
22        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
23
24        // Join Code (String)
25        4, 84, 111, 100, 100,
26
27        // Lobby size (u8)
28        2,
29
30        // Account 1
31        // (UUIDv4)
32        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
33        // (String)
34        4, 84, 111, 100, 100,
35
36        // Account 2
37        // (UUIDv4)
38        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
39        // (String)
40        4, 84, 111, 100, 100,
41    ];
42
43    let mut byte_reader: ByteReader = ByteReader::new(&bytes, Endian::Little);
44
45    let game_id: Uuid = byte_reader.read_uuid()?;
46    let join_code: String = byte_reader.read_string()?;
47
48    let mut lobby: Vec<Account> = Vec::new();
49    let lobby_len: u8 = byte_reader.read_u8()?;
50    for _ in 0..lobby_len {
51        lobby.push(Account {
52            account_id: byte_reader.read_uuid()?,
53            username: byte_reader.read_string()?,
54        });
55    }
56
57    println!(
58        "{:?}",
59        Payload {
60            game_id,
61            join_code,
62            lobby,
63        }
64    );
65    Ok(())
66}
Source

pub fn read_i8(&mut self) -> ByteReaderResult<i8>

§Errors

Will return an error if there are not enough bytes to read.

Source

pub fn read_u16(&mut self) -> ByteReaderResult<u16>

§Errors

Will return an error if there are not enough bytes to read.

Source

pub fn read_i16(&mut self) -> ByteReaderResult<i16>

§Errors

Will return an error if there are not enough bytes to read.

Source

pub fn read_u32(&mut self) -> ByteReaderResult<u32>

§Errors

Will return an error if there are not enough bytes to read.

Source

pub fn read_i32(&mut self) -> ByteReaderResult<i32>

§Errors

Will return an error if there are not enough bytes to read.

Source

pub fn read_u64(&mut self) -> ByteReaderResult<u64>

§Errors

Will return an error if there are not enough bytes to read.

Source

pub fn read_i64(&mut self) -> ByteReaderResult<i64>

§Errors

Will return an error if there are not enough bytes to read.

Source

pub fn read_string(&mut self) -> ByteReaderResult<String>

§Errors

Will return an error if there are not enough bytes to read or if the UTF-8 bytes are invalid.

Examples found in repository?
examples/transcode.rs (line 46)
18fn main() -> ByteReaderResult<()> {
19    #[rustfmt::skip]
20    let bytes: Vec<u8> = vec![
21        // Game ID (UUIDv4)
22        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
23
24        // Join Code (String)
25        4, 84, 111, 100, 100,
26
27        // Lobby size (u8)
28        2,
29
30        // Account 1
31        // (UUIDv4)
32        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
33        // (String)
34        4, 84, 111, 100, 100,
35
36        // Account 2
37        // (UUIDv4)
38        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
39        // (String)
40        4, 84, 111, 100, 100,
41    ];
42
43    let mut byte_reader: ByteReader = ByteReader::new(&bytes, Endian::Little);
44
45    let game_id: Uuid = byte_reader.read_uuid()?;
46    let join_code: String = byte_reader.read_string()?;
47
48    let mut lobby: Vec<Account> = Vec::new();
49    let lobby_len: u8 = byte_reader.read_u8()?;
50    for _ in 0..lobby_len {
51        lobby.push(Account {
52            account_id: byte_reader.read_uuid()?,
53            username: byte_reader.read_string()?,
54        });
55    }
56
57    println!(
58        "{:?}",
59        Payload {
60            game_id,
61            join_code,
62            lobby,
63        }
64    );
65    Ok(())
66}
Source

pub fn read_uuid(&mut self) -> ByteReaderResult<Uuid>

§Errors

Will return an error if there are not enough bytes to read or if the UUID bytes are invalid.

Examples found in repository?
examples/transcode.rs (line 45)
18fn main() -> ByteReaderResult<()> {
19    #[rustfmt::skip]
20    let bytes: Vec<u8> = vec![
21        // Game ID (UUIDv4)
22        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
23
24        // Join Code (String)
25        4, 84, 111, 100, 100,
26
27        // Lobby size (u8)
28        2,
29
30        // Account 1
31        // (UUIDv4)
32        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
33        // (String)
34        4, 84, 111, 100, 100,
35
36        // Account 2
37        // (UUIDv4)
38        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
39        // (String)
40        4, 84, 111, 100, 100,
41    ];
42
43    let mut byte_reader: ByteReader = ByteReader::new(&bytes, Endian::Little);
44
45    let game_id: Uuid = byte_reader.read_uuid()?;
46    let join_code: String = byte_reader.read_string()?;
47
48    let mut lobby: Vec<Account> = Vec::new();
49    let lobby_len: u8 = byte_reader.read_u8()?;
50    for _ in 0..lobby_len {
51        lobby.push(Account {
52            account_id: byte_reader.read_uuid()?,
53            username: byte_reader.read_string()?,
54        });
55    }
56
57    println!(
58        "{:?}",
59        Payload {
60            game_id,
61            join_code,
62            lobby,
63        }
64    );
65    Ok(())
66}
Source

pub fn read_remaining_bytes(&mut self) -> Vec<u8>

Source

pub fn is_empty(&self) -> bool

Source

pub fn remaining(&self) -> usize

Auto Trait Implementations§

§

impl<'a> Freeze for ByteReader<'a>

§

impl<'a> RefUnwindSafe for ByteReader<'a>

§

impl<'a> Send for ByteReader<'a>

§

impl<'a> Sync for ByteReader<'a>

§

impl<'a> Unpin for ByteReader<'a>

§

impl<'a> UnwindSafe for ByteReader<'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> 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.