1#![cfg_attr(
15 all(doc, feature = "document-features"),
16 doc = ::document_features::document_features!()
17)]
18#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
19#![deny(missing_docs, unsafe_code)]
20
21use std::{borrow::Cow, ops::Deref, path::Path};
22
23pub use memmap2::Mmap as MMap;
25
26pub trait FileData: Deref<Target = [u8]> {}
28
29impl<T> FileData for T where T: Deref<Target = [u8]> {}
30
31pub mod bundle;
33pub struct Bundle {
35 pub pack: data::File,
37 pub index: index::File,
39}
40
41pub mod find;
43
44pub mod cache;
46pub mod data;
48
49mod find_traits;
50pub use find_traits::{Find, FindExt};
51
52pub mod index;
54pub mod multi_index;
56
57pub mod verify;
59
60#[cfg(feature = "testing")]
62pub mod testing;
63
64mod mmap {
65 use std::path::Path;
66
67 pub fn read_only(path: &Path) -> std::io::Result<memmap2::Mmap> {
68 let file = std::fs::File::open(path)?;
69 #[expect(unsafe_code)]
71 unsafe {
72 memmap2::MmapOptions::new().map_copy_read_only(&file)
73 }
74 }
75}
76
77fn source_name(path: &Path) -> Cow<'_, str> {
81 if path.as_os_str().is_empty() {
82 Cow::Borrowed("<memory>")
83 } else if let Some(name) = path.file_name() {
84 name.to_string_lossy()
85 } else {
86 path.as_os_str().to_string_lossy()
87 }
88}
89
90#[inline]
91fn read_u32(b: &[u8]) -> u32 {
92 u32::from_be_bytes(b.try_into().unwrap())
93}
94
95#[inline]
96fn read_u64(b: &[u8]) -> u64 {
97 u64::from_be_bytes(b.try_into().unwrap())
98}
99
100fn exact_vec<T>(capacity: usize) -> Vec<T> {
101 let mut v = Vec::new();
102 v.reserve_exact(capacity);
103 v
104}
105
106#[inline]
107fn fan_is_monotonically_increasing(fan: &[u32]) -> bool {
108 !fan.windows(2).any(|window| window[0] > window[1])
109}