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 crate::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
91/// A way to indicate if a lookup, despite successful, was ambiguous or yielded exactly
92/// one result in the particular index.
93pub type PrefixLookupResult = Result<EntryIndex, ()>;
94
95/// The type for referring to indices of an entry within the index file.
96pub type EntryIndex = u32;
97
98const FAN_LEN: usize = 256;
99
100/// A representation of a pack index file
101pub struct File<T = MMap> {
102 data: T,
103 path: std::path::PathBuf,
104 version: Version,
105 num_objects: u32,
106 fan: [u32; FAN_LEN],
107 hash_len: usize,
108 object_hash: gix_hash::Kind,
109}
110
111/// Basic file information
112impl<T> File<T>
113where
114 T: crate::FileData,
115{
116 /// The version of the pack index
117 pub fn version(&self) -> Version {
118 self.version
119 }
120 /// The path of the opened index file
121 pub fn path(&self) -> &std::path::Path {
122 &self.path
123 }
124 /// The amount of objects stored in the pack and index, as one past the highest entry index.
125 pub fn num_objects(&self) -> EntryIndex {
126 self.num_objects
127 }
128 /// The kind of hash we assume
129 pub fn object_hash(&self) -> gix_hash::Kind {
130 self.object_hash
131 }
132}
133
134const V2_SIGNATURE: &[u8] = b"\xfftOc";
135///
136pub mod init;
137
138pub(crate) mod access;
139pub use access::Entry;
140
141pub(crate) mod encode;
142///
143pub mod traverse;
144mod util;
145///
146pub mod verify;
147///
148#[cfg(feature = "streaming-input")]
149pub mod write;
150#[cfg(feature = "streaming-input")]
151pub use write::function::write_data_iter_to_stream;