Struct ByteReader

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

Implementations§

Source§

impl<'a> ByteReader<'a>

Source

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

Examples found in repository?
examples/transcode.rs (line 43)
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> ByteReaderResult<()> {
    #[rustfmt::skip]
    let bytes: Vec<u8> = vec![
        // Game ID (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,

        // Join Code (String)
        4, 84, 111, 100, 100,

        // Lobby size (u8)
        2,

        // Account 1
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,

        // Account 2
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,
    ];

    let mut byte_reader: ByteReader = ByteReader::new(&bytes);

    let game_id: Uuid = byte_reader.read_uuid()?;
    let join_code: String = byte_reader.read_string()?;

    let mut lobby: Vec<Account> = Vec::new();
    let lobby_len: u8 = byte_reader.read_u8()?;
    for _ in 0..lobby_len {
        lobby.push(Account {
            account_id: byte_reader.read_uuid()?,
            username: byte_reader.read_string()?,
        });
    }

    println!(
        "{:?}",
        Payload {
            game_id,
            join_code,
            lobby,
        }
    );
    Ok(())
}
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)
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> ByteReaderResult<()> {
    #[rustfmt::skip]
    let bytes: Vec<u8> = vec![
        // Game ID (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,

        // Join Code (String)
        4, 84, 111, 100, 100,

        // Lobby size (u8)
        2,

        // Account 1
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,

        // Account 2
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,
    ];

    let mut byte_reader: ByteReader = ByteReader::new(&bytes);

    let game_id: Uuid = byte_reader.read_uuid()?;
    let join_code: String = byte_reader.read_string()?;

    let mut lobby: Vec<Account> = Vec::new();
    let lobby_len: u8 = byte_reader.read_u8()?;
    for _ in 0..lobby_len {
        lobby.push(Account {
            account_id: byte_reader.read_uuid()?,
            username: byte_reader.read_string()?,
        });
    }

    println!(
        "{:?}",
        Payload {
            game_id,
            join_code,
            lobby,
        }
    );
    Ok(())
}
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_u32(&mut self) -> ByteReaderResult<u32>

§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_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)
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> ByteReaderResult<()> {
    #[rustfmt::skip]
    let bytes: Vec<u8> = vec![
        // Game ID (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,

        // Join Code (String)
        4, 84, 111, 100, 100,

        // Lobby size (u8)
        2,

        // Account 1
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,

        // Account 2
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,
    ];

    let mut byte_reader: ByteReader = ByteReader::new(&bytes);

    let game_id: Uuid = byte_reader.read_uuid()?;
    let join_code: String = byte_reader.read_string()?;

    let mut lobby: Vec<Account> = Vec::new();
    let lobby_len: u8 = byte_reader.read_u8()?;
    for _ in 0..lobby_len {
        lobby.push(Account {
            account_id: byte_reader.read_uuid()?,
            username: byte_reader.read_string()?,
        });
    }

    println!(
        "{:?}",
        Payload {
            game_id,
            join_code,
            lobby,
        }
    );
    Ok(())
}
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)
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
fn main() -> ByteReaderResult<()> {
    #[rustfmt::skip]
    let bytes: Vec<u8> = vec![
        // Game ID (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,

        // Join Code (String)
        4, 84, 111, 100, 100,

        // Lobby size (u8)
        2,

        // Account 1
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,

        // Account 2
        // (UUIDv4)
        26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
        // (String)
        4, 84, 111, 100, 100,
    ];

    let mut byte_reader: ByteReader = ByteReader::new(&bytes);

    let game_id: Uuid = byte_reader.read_uuid()?;
    let join_code: String = byte_reader.read_string()?;

    let mut lobby: Vec<Account> = Vec::new();
    let lobby_len: u8 = byte_reader.read_u8()?;
    for _ in 0..lobby_len {
        lobby.push(Account {
            account_id: byte_reader.read_uuid()?,
            username: byte_reader.read_string()?,
        });
    }

    println!(
        "{:?}",
        Payload {
            game_id,
            join_code,
            lobby,
        }
    );
    Ok(())
}
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.