···77 // Instead of using .generate(), use the Builder for more control
88 let config = cbindgen::Config::from_file("cbindgen.toml").unwrap_or_default();
991010- cbindgen::Builder::new()
1010+ if let Ok(b) = cbindgen::Builder::new()
1111 .with_crate(&crate_dir)
1212 .with_config(config)
1313 .generate()
1414- .expect("Unable to generate bindings")
1515- .write_to_file(PathBuf::from(crate_dir).join("include/bindle.h"));
1414+ {
1515+ b.write_to_file(PathBuf::from(crate_dir).join("include/bindle.h"));
1616+ } else {
1717+ eprintln!("WARNING: bindle.h not updated");
1818+ }
1619}
+29-13
src/lib.rs
···1313const BNDL_ALIGN: usize = 8;
1414const ENTRY_SIZE: usize = std::mem::size_of::<Entry>();
1515const FOOTER_SIZE: usize = std::mem::size_of::<Footer>();
1616-const HEADER_SIZE: u64 = 8;
1616+const HEADER_SIZE: usize = 8;
1717+1818+fn pad<
1919+ const SIZE: usize,
2020+ T: Copy + TryFrom<usize> + std::ops::Sub<T, Output = T> + std::ops::Rem<T, Output = T>,
2121+>(
2222+ n: T,
2323+) -> T
2424+where
2525+ <T as std::ops::Sub>::Output: std::ops::Rem<T>,
2626+{
2727+ if let Ok(size) = T::try_from(SIZE) {
2828+ return (size - (n % size)) % size;
2929+ }
3030+3131+ unreachable!()
3232+}
17331834#[repr(C)]
1935#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
···2642#[repr(C, packed)]
2743#[derive(FromBytes, Unaligned, IntoBytes, Immutable, Clone, Copy, Debug, Default)]
2844pub struct Entry {
2929- pub offset: [u8; 8], // Use [u8; 8] for disk stability
3030- pub compressed_size: [u8; 8],
3131- pub uncompressed_size: [u8; 8],
3232- pub crc32: [u8; 4],
3333- pub name_len: [u8; 2],
4545+ pub offset: [u8; std::mem::size_of::<u64>()], // Use [u8; 8] for disk stability
4646+ pub compressed_size: [u8; std::mem::size_of::<u64>()],
4747+ pub uncompressed_size: [u8; std::mem::size_of::<u64>()],
4848+ pub crc32: [u8; std::mem::size_of::<u32>()],
4949+ pub name_len: [u8; std::mem::size_of::<u16>()],
3450 pub compression_type: u8,
3551 pub _reserved: u8,
3652}
···120136 file,
121137 mmap: None,
122138 index: BTreeMap::new(),
123123- data_end: HEADER_SIZE,
139139+ data_end: HEADER_SIZE as u64,
124140 });
125141 }
126142···170186171187 let offset = self.data_end;
172188 let c_size = processed.len() as u64;
173173- let pad = (8 - (c_size % 8)) % 8;
189189+ let pad = pad::<8, u64>(c_size);
174190 if pad > 0 {
175191 self.file.write_all(&vec![0u8; pad as usize])?;
176192 }
···198214 for (name, entry) in &self.index {
199215 self.file.write_all(entry.as_bytes())?;
200216 self.file.write_all(name.as_bytes())?;
201201- let pad = (BNDL_ALIGN - ((ENTRY_SIZE + name.len()) % BNDL_ALIGN)) % BNDL_ALIGN;
217217+ let pad = pad::<BNDL_ALIGN, usize>(ENTRY_SIZE + name.len()); // (BNDL_ALIGN - ((ENTRY_SIZE + name.len()) % BNDL_ALIGN)) % BNDL_ALIGN;
202218 if pad > 0 {
203219 self.file.write_all(&vec![0u8; pad])?;
204220 }
···227243 .open(&tmp_path)?;
228244229245 new_file.write_all(BNDL_MAGIC)?;
230230- let mut current_offset = HEADER_SIZE;
246246+ let mut current_offset = HEADER_SIZE as u64;
231247232248 // Copy only live entries to the new file
233249 for entry in self.index.values_mut() {
···235251 self.file.seek(SeekFrom::Start(entry.offset()))?;
236252 self.file.read_exact(&mut buf)?;
237253238238- new_file.seek(SeekFrom::Start(current_offset))?;
254254+ new_file.seek(SeekFrom::Start(current_offset as u64))?;
239255 new_file.write_all(&buf)?;
240256241257 entry.offset = current_offset.to_le_bytes();
242242- let pad = (8 - (entry.compressed_size() % 8)) % 8;
258258+ let pad = pad::<8, u64>(entry.compressed_size());
243259 if pad > 0 {
244260 new_file.write_all(&vec![0u8; pad as usize])?;
245261 }
···251267 for (name, entry) in &self.index {
252268 new_file.write_all(entry.as_bytes())?;
253269 new_file.write_all(name.as_bytes())?;
254254- let pad = (BNDL_ALIGN - ((ENTRY_SIZE + name.len()) % BNDL_ALIGN)) % BNDL_ALIGN;
270270+ let pad = pad::<BNDL_ALIGN, usize>(ENTRY_SIZE + name.len());
255271 if pad > 0 {
256272 new_file.write_all(&vec![0u8; pad])?;
257273 }