Skip to main content

brk_types/
type_index.rs

1use std::ops::Add;
2
3use byteview::ByteView;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use vecdb::{CheckedSub, Formattable, Pco};
7
8/// Index within its type (e.g., 0 for first P2WPKH address)
9#[derive(
10    Debug,
11    PartialEq,
12    Eq,
13    PartialOrd,
14    Ord,
15    Clone,
16    Copy,
17    Default,
18    Serialize,
19    Deserialize,
20    Pco,
21    JsonSchema,
22    Hash,
23)]
24pub struct TypeIndex(u32);
25
26impl TypeIndex {
27    pub const COINBASE: Self = Self(u32::MAX);
28
29    pub fn new(i: u32) -> Self {
30        Self(i)
31    }
32
33    pub fn increment(&mut self) {
34        self.0 += 1;
35    }
36
37    pub fn incremented(self) -> Self {
38        Self(self.0 + 1)
39    }
40
41    pub fn copy_then_increment(&mut self) -> Self {
42        let i = *self;
43        self.increment();
44        i
45    }
46
47    pub fn to_be_bytes(&self) -> [u8; 4] {
48        self.0.to_be_bytes()
49    }
50
51    pub fn to_ne_bytes(&self) -> [u8; 4] {
52        self.0.to_ne_bytes()
53    }
54}
55
56impl From<u32> for TypeIndex {
57    #[inline]
58    fn from(value: u32) -> Self {
59        Self(value)
60    }
61}
62impl From<TypeIndex> for u32 {
63    #[inline]
64    fn from(value: TypeIndex) -> Self {
65        value.0
66    }
67}
68
69impl From<u64> for TypeIndex {
70    #[inline]
71    fn from(value: u64) -> Self {
72        Self(value as u32)
73    }
74}
75impl From<TypeIndex> for u64 {
76    #[inline]
77    fn from(value: TypeIndex) -> Self {
78        value.0 as u64
79    }
80}
81
82impl From<usize> for TypeIndex {
83    #[inline]
84    fn from(value: usize) -> Self {
85        Self(value as u32)
86    }
87}
88impl From<TypeIndex> for usize {
89    #[inline]
90    fn from(value: TypeIndex) -> Self {
91        value.0 as usize
92    }
93}
94
95impl Add<u32> for TypeIndex {
96    type Output = Self;
97    fn add(self, rhs: u32) -> Self::Output {
98        Self(self.0 + rhs)
99    }
100}
101impl Add<usize> for TypeIndex {
102    type Output = Self;
103    fn add(self, rhs: usize) -> Self::Output {
104        Self(self.0 + rhs as u32)
105    }
106}
107
108impl Add<TypeIndex> for TypeIndex {
109    type Output = Self;
110    fn add(self, rhs: TypeIndex) -> Self::Output {
111        Self(self.0 + rhs.0)
112    }
113}
114
115impl From<ByteView> for TypeIndex {
116    #[inline]
117    fn from(value: ByteView) -> Self {
118        Self::from(u32::from_be_bytes((&*value).try_into().unwrap()))
119    }
120}
121impl From<TypeIndex> for ByteView {
122    #[inline(always)]
123    fn from(value: TypeIndex) -> Self {
124        ByteView::from(&value)
125    }
126}
127impl From<&TypeIndex> for ByteView {
128    #[inline(always)]
129    fn from(value: &TypeIndex) -> Self {
130        Self::new(&value.0.to_be_bytes())
131    }
132}
133
134impl CheckedSub<TypeIndex> for TypeIndex {
135    fn checked_sub(self, rhs: Self) -> Option<Self> {
136        self.0.checked_sub(rhs.0).map(Self)
137    }
138}
139
140impl std::fmt::Display for TypeIndex {
141    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
142        let mut buf = itoa::Buffer::new();
143        let str = buf.format(self.0);
144        f.write_str(str)
145    }
146}
147
148impl Formattable for TypeIndex {
149    #[inline(always)]
150    fn write_to(&self, buf: &mut Vec<u8>) {
151        let mut b = itoa::Buffer::new();
152        buf.extend_from_slice(b.format(self.0).as_bytes());
153    }
154}