git_chunk/
lib.rs

1//! Low-level access to reading and writing chunk file based formats.
2//!
3//! See the [git documentation](https://github.com/git/git/blob/seen/Documentation/technical/chunk-format.txt) for details.
4#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
5
6/// An identifier to describe the kind of chunk, unique within a chunk file, typically in ASCII
7pub type Id = [u8; 4];
8
9/// A special value denoting the end of the chunk file table of contents.
10pub const SENTINEL: Id = [0u8; 4];
11
12///
13pub mod range {
14    use std::{convert::TryInto, ops::Range};
15
16    use crate::file;
17
18    /// Turn a u64 Range into a usize range safely, to make chunk ranges useful in memory mapped files.
19    pub fn into_usize(Range { start, end }: Range<file::Offset>) -> Option<Range<usize>> {
20        let start = start.try_into().ok()?;
21        let end = end.try_into().ok()?;
22        Some(Range { start, end })
23    }
24
25    /// Similar to [`into_usize()`], but panics assuming that the memory map couldn't be created if offsets
26    /// stored are too high.
27    ///
28    /// This is only true for correctly formed files, as it's entirely possible to provide out of bounds offsets
29    /// which are checked for separately - we wouldn't be here if that was the case.
30    pub fn into_usize_or_panic(range: Range<file::Offset>) -> Range<usize> {
31        into_usize(range).expect("memory maps can't be created if files are too large")
32    }
33}
34
35///
36pub mod file;