bufkit/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![cfg_attr(docsrs, allow(unused_attributes))]
5#![deny(missing_docs)]
6
7#[cfg(all(not(feature = "std"), feature = "alloc"))]
8extern crate alloc as std;
9
10#[cfg(feature = "std")]
11extern crate std;
12
13#[cfg(feature = "varint")]
14#[cfg_attr(docsrs, doc(cfg(feature = "varint")))]
15pub use varing::Varint;
16
17pub use chunk::*;
18pub use chunk_mut::*;
19
20/// Errors buffer I/O
21pub mod error;
22
23macro_rules! non_zero {
24  ($($size:literal),+$(,)?) => {
25    paste::paste! {
26      $(
27        const [<NON_ZERO_ $size>]: ::core::num::NonZeroUsize = core::num::NonZeroUsize::new($size).expect(concat!($size, "is non-zero"));
28      )*
29    }
30  };
31}
32
33non_zero!(1);
34
35mod chunk;
36mod chunk_mut;
37
38/// Panic with a nice error message.
39#[cold]
40fn panic_advance(error_info: &error::TryAdvanceError) -> ! {
41  panic!(
42    "advance out of bounds: the len is {} but advancing by {}",
43    error_info.available(),
44    error_info.requested()
45  );
46}
47
48/// # Panics
49/// This function panics if `size` is zero. Use only when you have already checked that `size` is non-zero.
50#[inline]
51const fn must_non_zero(size: usize) -> core::num::NonZeroUsize {
52  match core::num::NonZeroUsize::new(size) {
53    Some(nz) => nz,
54    None => panic!("Already checked value is non-zero"),
55  }
56}
57
58#[test]
59#[should_panic]
60fn test_must_non_zero_panic() {
61  must_non_zero(0);
62}