Skip to main content

gix_utils/
lib.rs

1//! A crate with utilities that don't need feature toggles.
2//!
3//! If they would need feature toggles, they should be in `gix-features` instead.
4//!
5//! ## Examples
6//!
7//! ```
8//! use std::time::Duration;
9//!
10//! use gix_utils::{backoff::Quadratic};
11//!
12//! let waits: Vec<_> = Quadratic::default().take(3).collect();
13//! assert_eq!(waits, vec![
14//!     Duration::from_millis(1),
15//!     Duration::from_millis(4),
16//!     Duration::from_millis(9),
17//! ]);
18//! ```
19#![deny(missing_docs)]
20#![forbid(unsafe_code)]
21
22///
23pub mod backoff;
24
25///
26pub mod rng;
27
28///
29pub mod buffers;
30
31///
32pub mod str;
33
34///
35pub mod btoi;
36
37/// Byte-string conversion utilities.
38#[cfg(feature = "bstr")]
39mod bstr;
40#[cfg(feature = "bstr")]
41pub use bstr::{AsBStr, AsBStrOpt};
42
43/// Return whether `byte` is whitespace according to Git's locale-independent `sane_ctype` table.
44///
45/// This includes space, horizontal tab, newline and carriage return, but excludes vertical tab and form feed.
46#[inline]
47pub const fn git_is_space(byte: u8) -> bool {
48    matches!(byte, b' ' | b'\t' | b'\n' | b'\r')
49}
50
51/// A utility to do buffer-swapping with.
52///
53/// Use `src` to read from and `dest` to write to, and after actually changing data, call [Buffers::swap()].
54/// To be able to repeat the process, this time using what was `dest` as `src`, freeing up `dest` for writing once more.
55///
56/// Note that after each [`Buffers::swap()`], `src` is the most recent version of the data, just like before each swap.
57#[derive(Default, Clone)]
58pub struct Buffers {
59    /// The source data, as basis for processing.
60    pub src: Vec<u8>,
61    /// The data produced after processing `src`.
62    pub dest: Vec<u8>,
63}