Skip to main content

tcplane/utils/list/
fn.rs

1/// Removes trailing zero bytes from a byte vector.
2///
3/// This function truncates the vector at the last non-zero byte,
4/// or clears it entirely if all bytes are zero.
5///
6/// # Arguments
7///
8/// - `&mut Vec<u8>` - The byte vector to process.
9///
10/// # Returns
11///
12/// - `Vec<u8>` - A clone of the modified vector.
13pub fn remove_trailing_zeros(data: &mut Vec<u8>) -> Vec<u8> {
14    if let Some(last_non_zero_pos) = data.iter().rposition(|&x| x != 0) {
15        data.truncate(last_non_zero_pos + 1);
16    } else {
17        data.clear();
18    }
19    data.clone()
20}