Skip to main content

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