fn deserialize(buffer: &[u8]) -> Result<Self::Item, SerializeError> {
        if buffer.len() < User::MIN_BYTE_LEN {
            Err(SerializeError::NotEnoughData)
        } else {
            let id = crate::parse_id_from_bytes(&buffer[..USER_ID_SIZE]);
            let mut buffer_index = USER_ID_SIZE + crate::MAX_PASS_BYTE_LEN;
            let password =
                crate::parse_string_from_bytes(&buffer[USER_ID_SIZE..buffer_index]).to_string();
            let friends_count = buffer[buffer_index] as usize;
            buffer_index += 1;
            let invitations_count = buffer[buffer_index] as usize;
            buffer_index += 1;
            let mut friends = HashSet::new();
            let mut invitations = HashSet::new();

            // TODO: Add checks for buffer boundry so we cannot read outside buffer and cause
            // panic.
            let mut is_parsing_friends = true;
            for count in [friends_count, invitations_count] {
                for _ in 0..count {
                    let contact_id = crate::parse_id_from_bytes(
                        &buffer[buffer_index..buffer_index + USER_ID_SIZE],
                    );
                    if is_parsing_friends {
                        friends.insert(contact_id);
                    } else {
                        invitations.insert(contact_id);
                    }
                    buffer_index += USER_ID_SIZE;
                }

                is_parsing_friends = false;
            }

            Ok(User {
                id,
                password,
                friends,
                invitations,
            })
        }
    }
/// Tries to parse user data from u8 slice. It returns `Self` on success and `SerializeError` on
/// any error.
Tries to parse user data from u8 slice. It returns `Self` on success and `SerializeError` on
any error.
Tries to parse user data from u8 slice . It returns ` Self ` on success and ` SerializeError ` on any error .
fn deserialize(buffer: &[u8]) -> Result<Self::Item, SerializeError> {
        if buffer.len() < User::MIN_BYTE_LEN {
            Err(SerializeError::NotEnoughData)
        } else {
            let id = crate::parse_id_from_bytes(&buffer[..USER_ID_SIZE]);
            let mut buffer_index = USER_ID_SIZE + crate::MAX_PASS_BYTE_LEN;
            let password =
                crate::parse_string_from_bytes(&buffer[USER_ID_SIZE..buffer_index]).to_string();
            let friends_count = buffer[buffer_index] as usize;
            buffer_index += 1;
            let invitations_count = buffer[buffer_index] as usize;
            buffer_index += 1;
            let mut friends = HashSet::new();
            let mut invitations = HashSet::new();
            // TODO: Add checks for buffer boundry so we cannot read outside buffer and cause
            // panic.
            let mut is_parsing_friends = true;
            for count in [friends_count, invitations_count] {
                for _ in 0..count {
                    let contact_id = crate::parse_id_from_bytes(
                        &buffer[buffer_index..buffer_index + USER_ID_SIZE],
                    );
                    if is_parsing_friends {
                        friends.insert(contact_id);
                    } else {
                        invitations.insert(contact_id);
                    }
                    buffer_index += USER_ID_SIZE;
                }
                is_parsing_friends = false;
            }
            Ok(User {
                id,
                password,
                friends,
                invitations,
            })
        }
    }
fn deserialize ( buffer : & [ u8 ] ) -> Result < Self :: Item , SerializeError > { if buffer . len ( ) < User :: MIN_BYTE_LEN { Err ( SerializeError :: NotEnoughData ) } else { let id = crate :: parse_id_from_bytes ( & buffer [ .. USER_ID_SIZE ] ) ; let mut buffer_index = USER_ID_SIZE + crate :: MAX_PASS_BYTE_LEN ; let password = crate :: parse_string_from_bytes ( & buffer [ USER_ID_SIZE .. buffer_index ] ) . to_string ( ) ; let friends_count = buffer [ buffer_index ] as usize ; buffer_index += 1 ; let invitations_count = buffer [ buffer_index ] as usize ; buffer_index += 1 ; let mut friends = HashSet :: new ( ) ; let mut invitations = HashSet :: new ( ) ; // TODO: Add checks for buffer boundry so we cannot read outside buffer and cause // panic. let mut is_parsing_friends = true ; for count in [ friends_count , invitations_count ] { for _ in 0 .. count { let contact_id = crate :: parse_id_from_bytes ( & buffer [ buffer_index .. buffer_index + USER_ID_SIZE ] , ) ; if is_parsing_friends { friends . insert ( contact_id ) ; } else { invitations . insert ( contact_id ) ; } buffer_index += USER_ID_SIZE ; } is_parsing_friends = false ; } Ok ( User { id , password , friends , invitations , } ) } }
Tries to parse user data from u8 slice.
Tries to parse user data from u8 slice .
buffer
&[u8]