queue-file icon indicating copy to clipboard operation
queue-file copied to clipboard

Mismatched ensure! in Element impl

Open catemiko opened this issue 2 years ago • 0 comments

I found out that field len of Element have usize type actually. But it is tested against i32. Same for pos field.

#[derive(Copy, Clone, Debug)]
struct Element {
    pos: u64,
    len: usize,
}

impl Element {
    const EMPTY: Self = Self { pos: 0, len: 0 };
    const HEADER_LENGTH: usize = 4;

    #[inline]
    fn new(pos: u64, len: usize) -> Result<Self> {
        ensure!(i64::try_from(pos).is_ok(), CorruptedFileSnafu { //Why u64 testing against i64?
            msg: "element position must be less or equal to i64::MAX"
        });
        ensure!(i32::try_from(len).is_ok(), ElementTooBigSnafu); //Why usize testing against i32?

        Ok(Self { pos, len })
    }
}

catemiko avatar May 27 '23 05:05 catemiko