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
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<T = MMap> {
111 data: T,
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<T> File<T>
122where
123 T: crate::FileData,
124{
125 /// The version of the pack index
126 pub fn version(&self) -> Version {
127 self.version
128 }
129 /// The path of the opened index file
130 pub fn path(&self) -> &std::path::Path {
131 &self.path
132 }
133 /// The amount of objects stored in the pack and index, as one past the highest entry index.
134 pub fn num_objects(&self) -> EntryIndex {
135 self.num_objects
136 }
137 /// The kind of hash we assume
138 pub fn object_hash(&self) -> gix_hash::Kind {
139 self.object_hash
140 }
141}
142
143const V2_SIGNATURE: &[u8] = b"\xfftOc";
144///
145pub mod init;
146
147pub(crate) mod access;
148pub use access::Entry;
149
150pub(crate) mod encode;
151///
152pub mod traverse;
153mod util;
154///
155pub mod verify;
156///
157#[cfg(feature = "streaming-input")]
158pub mod write;
159#[cfg(feature = "streaming-input")]
160pub use write::function::write_data_iter_to_stream;