eculocate

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

helpers.rs (973B)


      1 #[inline(always)]
      2 /// Helper funcion!
      3 /// Change seq to partition number
      4 pub const fn seq_to_part(seq: u32, partitions_count: usize) -> usize {
      5     ((seq as usize).saturating_sub(1)) % partitions_count
      6 }
      7 
      8 #[inline(always)]
      9 /// Helper funcion!
     10 /// If crc of seq is correct it returns seq, otherwise default value is returned
     11 pub fn seq_or_default(seq: &[u8], crc: u32, default: u32) -> u32 {
     12     let crc_calc = crate::crc32::calc_crc32(seq, 0xFFFFFFFF);
     13     if crc == crc_calc {
     14         return u32::from_le_bytes(seq.try_into().expect("Wrong size?"));
     15     }
     16 
     17     default
     18 }
     19 
     20 #[inline(always)]
     21 /// Helper function!
     22 /// Check if crc is correct for given seq
     23 pub fn is_crc_seq_correct(seq: u32, crc: u32) -> bool {
     24     let bytes = unsafe {
     25         let mut buf = [0; 4]; //u32
     26         core::ptr::copy_nonoverlapping(&seq as *const u32 as *const u8, buf.as_mut_ptr(), 4);
     27 
     28         buf
     29     };
     30 
     31     let crc_calc = crate::crc32::calc_crc32(&bytes, 0xFFFFFFFF);
     32     crc == crc_calc
     33 }