A better Rust ATProto crate
1use std::convert::TryInto;
2
3#[cfg(feature = "sync")]
4pub(crate) mod concurrent;
5
6pub(crate) mod builder_utils;
7pub(crate) mod deque;
8pub(crate) mod frequency_sketch;
9pub mod time;
10
11// Note: `CacheRegion` cannot have more than four enum variants. This is because
12// `crate::{sync,unsync}::DeqNodes` uses a `tagptr::TagNonNull<DeqNode<T>, 2>`
13// pointer, where the 2-bit tag is `CacheRegion`.
14#[derive(Clone, Copy, Debug, Eq)]
15pub(crate) enum CacheRegion {
16 Window = 0,
17 MainProbation = 1,
18 MainProtected = 2,
19 Other = 3,
20}
21
22impl From<usize> for CacheRegion {
23 fn from(n: usize) -> Self {
24 match n {
25 0 => Self::Window,
26 1 => Self::MainProbation,
27 2 => Self::MainProtected,
28 3 => Self::Other,
29 _ => panic!("No such CacheRegion variant for {}", n),
30 }
31 }
32}
33
34impl PartialEq<Self> for CacheRegion {
35 fn eq(&self, other: &Self) -> bool {
36 core::mem::discriminant(self) == core::mem::discriminant(other)
37 }
38}
39
40impl PartialEq<usize> for CacheRegion {
41 fn eq(&self, other: &usize) -> bool {
42 *self as usize == *other
43 }
44}
45
46// Ensures the value fits in a range of `128u32..=u32::MAX`.
47pub(crate) fn sketch_capacity(max_capacity: u64) -> u32 {
48 max_capacity.try_into().unwrap_or(u32::MAX).max(128)
49}