This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

core / knot2 / crates / knot-pack / src / ids.rs
2.4 kB 108 lines
1knot_types::scalar_newtype! { 2 pub(crate) struct Crc32(u32); 3 pub struct MaxWireBytes(usize); 4} 5 6#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] 7pub(crate) struct PackOffset(u64); 8 9impl PackOffset { 10 pub(crate) fn new(value: u64) -> Self { 11 Self(value) 12 } 13 14 pub(crate) fn get(self) -> u64 { 15 self.0 16 } 17 18 // Hostile packs will ask to go back past the start of a file, 19 // so just making sure. 20 pub(crate) fn checked_sub_distance(self, distance: u64) -> Option<Self> { 21 self.0.checked_sub(distance).map(Self) 22 } 23} 24 25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 26pub struct DeltaDepth(usize); 27 28impl DeltaDepth { 29 pub(crate) const ZERO: Self = Self(0); 30 31 pub const fn new(depth: usize) -> Self { 32 Self(depth) 33 } 34 35 pub(crate) const fn get(self) -> usize { 36 self.0 37 } 38 39 pub(crate) fn deeper(self) -> Self { 40 Self(self.0 + 1) 41 } 42 43 pub(crate) fn exceeds(self, max: DeltaDepth) -> bool { 44 self.0 > max.0 45 } 46} 47 48#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] 49pub struct MaxObjectBytes(u64); 50 51impl MaxObjectBytes { 52 pub const fn new(bytes: u64) -> Self { 53 Self(bytes) 54 } 55 56 pub const fn get(self) -> u64 { 57 self.0 58 } 59 60 pub(crate) const fn exceeded_by(self, size: u64) -> bool { 61 size > self.0 62 } 63} 64 65#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] 66pub struct MaxTotalBytes(u64); 67 68impl MaxTotalBytes { 69 pub const fn new(bytes: u64) -> Self { 70 Self(bytes) 71 } 72 73 pub const fn get(self) -> u64 { 74 self.0 75 } 76 77 pub(crate) const fn exceeded_by(self, size: u64) -> bool { 78 size > self.0 79 } 80} 81 82#[derive(Debug, Clone, Copy, PartialEq, Eq)] 83pub(crate) struct Rounds(usize); 84 85impl Rounds { 86 pub(crate) fn new(rounds: usize) -> Self { 87 Self(rounds) 88 } 89 90 // Ensuring chains deeper than the limit return `None` instead of 91 // wrapping and doing like 18 quintillion more passes. 92 pub(crate) fn next(self) -> Option<Self> { 93 self.0.checked_sub(1).map(Self) 94 } 95} 96 97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 98pub(crate) struct RefsDigest([u8; 32]); 99 100impl RefsDigest { 101 pub(crate) fn new(bytes: [u8; 32]) -> Self { 102 Self(bytes) 103 } 104 105 pub(crate) fn as_bytes(&self) -> &[u8] { 106 &self.0 107 } 108}