gix_pack/index/mod.rs
1//! an index into the pack file
2
3/// From itertools
4/// Create an iterator running multiple iterators in lockstep.
5///
6/// The `izip!` iterator yields elements until any subiterator
7/// returns `None`.
8///
9/// This is a version of the standard ``.zip()`` that's supporting more than
10/// two iterators. The iterator element type is a tuple with one element
11/// from each of the input iterators. Just like ``.zip()``, the iteration stops
12/// when the shortest of the inputs reaches its end.
13///
14/// **Note:** The result of this macro is in the general case an iterator
15/// composed of repeated `.zip()` and a `.map()`; it has an anonymous type.
16/// The special cases of one and two arguments produce the equivalent of
17/// `$a.into_iter()` and `$a.into_iter().zip($b)` respectively.
18///
19/// Prefer this macro `izip!()` over [`multizip`] for the performance benefits
20/// of using the standard library `.zip()`.
21///
22/// [`multizip`]: fn.multizip.html
23///
24/// ```ignore
25/// # use itertools::izip;
26/// #
27/// # fn main() {
28///
29/// // iterate over three sequences side-by-side
30/// let mut results = [0, 0, 0, 0];
31/// let inputs = [3, 7, 9, 6];
32///
33/// for (r, index, input) in izip!(&mut results, 0..10, &inputs) {
34/// *r = index * 10 + input;
35/// }
36///
37/// assert_eq!(results, [0 + 3, 10 + 7, 29, 36]);
38/// # }
39/// ```
40///
41/// (The above is vendored from [itertools](https://github.com/rust-itertools/itertools),
42/// including the original doctest, though it has been marked `ignore` here.)
43macro_rules! izip {
44 // @closure creates a tuple-flattening closure for .map() call. usage:
45 // @closure partial_pattern => partial_tuple , rest , of , iterators
46 // eg. izip!( @closure ((a, b), c) => (a, b, c) , dd , ee )
47 ( @closure $p:pat => $tup:expr ) => {
48 |$p| $tup
49 };
50
51 // The "b" identifier is a different identifier on each recursion level thanks to hygiene.
52 ( @closure $p:pat => ( $($tup:tt)* ) , $_iter:expr $( , $tail:expr )* ) => {
53 izip!(@closure ($p, b) => ( $($tup)*, b ) $( , $tail )*)
54 };
55
56 // unary
57 ($first:expr $(,)*) => {
58 std::iter::IntoIterator::into_iter($first)
59 };
60
61 // binary
62 ($first:expr, $second:expr $(,)*) => {
63 izip!($first)
64 .zip($second)
65 };
66
67 // n-ary where n > 2
68 ( $first:expr $( , $rest:expr )* $(,)* ) => {
69 izip!($first)
70 $(
71 .zip($rest)
72 )*
73 .map(
74 izip!(@closure a => (a) $( , $rest )*)
75 )
76 };
77}
78
79use memmap2::Mmap;
80
81/// The version of an index file
82#[derive(Default, PartialEq, Eq, Ord, PartialOrd, Debug, Hash, Clone, Copy)]
83#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
84#[allow(missing_docs)]
85pub enum Version {
86 V1 = 1,
87 #[default]
88 V2 = 2,
89}
90
91impl Version {
92 /// The kind of hash to produce to be compatible to this kind of index
93 pub fn hash(&self) -> gix_hash::Kind {
94 match self {
95 Version::V1 | Version::V2 => gix_hash::Kind::Sha1,
96 }
97 }
98}
99
100/// A way to indicate if a lookup, despite successful, was ambiguous or yielded exactly
101/// one result in the particular index.
102pub type PrefixLookupResult = Result<EntryIndex, ()>;
103
104/// The type for referring to indices of an entry within the index file.
105pub type EntryIndex = u32;
106
107const FAN_LEN: usize = 256;
108
109/// A representation of a pack index file
110pub struct File {
111 data: Mmap,
112 path: std::path::PathBuf,
113 version: Version,
114 num_objects: u32,
115 fan: [u32; FAN_LEN],
116 hash_len: usize,
117 object_hash: gix_hash::Kind,
118}
119
120/// Basic file information
121impl File {
122 /// The version of the pack index
123 pub fn version(&self) -> Version {
124 self.version
125 }
126 /// The path of the opened index file
127 pub fn path(&self) -> &std::path::Path {
128 &self.path
129 }
130 /// The amount of objects stored in the pack and index, as one past the highest entry index.
131 pub fn num_objects(&self) -> EntryIndex {
132 self.num_objects
133 }
134 /// The kind of hash we assume
135 pub fn object_hash(&self) -> gix_hash::Kind {
136 self.object_hash
137 }
138}
139
140const V2_SIGNATURE: &[u8] = b"\xfftOc";
141///
142pub mod init;
143
144pub(crate) mod access;
145pub use access::Entry;
146
147pub(crate) mod encode;
148///
149pub mod traverse;
150mod util;
151///
152pub mod verify;
153///
154#[cfg(feature = "streaming-input")]
155pub mod write;