brk_structs/structs/
stored_u16.rs1use std::ops::{Add, AddAssign, Div};
2
3use derive_deref::Deref;
4use serde::Serialize;
5use vecdb::{CheckedSub, Printable, StoredCompressed};
6use zerocopy_derive::{FromBytes, Immutable, IntoBytes, KnownLayout};
7
8use super::{
9 EmptyOutputIndex, OpReturnIndex, P2AAddressIndex, P2MSOutputIndex, P2PK33AddressIndex,
10 P2PK65AddressIndex, P2PKHAddressIndex, P2SHAddressIndex, P2TRAddressIndex, P2WPKHAddressIndex,
11 P2WSHAddressIndex, UnknownOutputIndex,
12};
13
14#[derive(
15 Debug,
16 Deref,
17 Clone,
18 Default,
19 Copy,
20 PartialEq,
21 Eq,
22 PartialOrd,
23 Ord,
24 FromBytes,
25 Immutable,
26 IntoBytes,
27 KnownLayout,
28 Serialize,
29 StoredCompressed,
30)]
31pub struct StoredU16(u16);
32
33impl StoredU16 {
34 pub const ZERO: Self = Self(0);
35
36 pub fn new(v: u16) -> Self {
37 Self(v)
38 }
39}
40
41impl From<u16> for StoredU16 {
42 fn from(value: u16) -> Self {
43 Self(value)
44 }
45}
46
47impl From<usize> for StoredU16 {
48 fn from(value: usize) -> Self {
49 if value > u16::MAX as usize {
50 panic!("usize too big (value = {value})")
51 }
52 Self(value as u16)
53 }
54}
55
56impl CheckedSub<StoredU16> for StoredU16 {
57 fn checked_sub(self, rhs: Self) -> Option<Self> {
58 self.0.checked_sub(rhs.0).map(Self)
59 }
60}
61
62impl Div<usize> for StoredU16 {
63 type Output = Self;
64 fn div(self, rhs: usize) -> Self::Output {
65 Self(self.0 / rhs as u16)
66 }
67}
68
69impl Add for StoredU16 {
70 type Output = Self;
71 fn add(self, rhs: Self) -> Self::Output {
72 Self(self.0 + rhs.0)
73 }
74}
75
76impl AddAssign for StoredU16 {
77 fn add_assign(&mut self, rhs: Self) {
78 *self = *self + rhs
79 }
80}
81
82impl From<f64> for StoredU16 {
83 fn from(value: f64) -> Self {
84 if value < 0.0 || value > u16::MAX as f64 {
85 panic!()
86 }
87 Self(value as u16)
88 }
89}
90
91impl From<StoredU16> for f64 {
92 fn from(value: StoredU16) -> Self {
93 value.0 as f64
94 }
95}
96
97impl From<StoredU16> for usize {
98 fn from(value: StoredU16) -> Self {
99 value.0 as usize
100 }
101}
102
103impl From<P2PK65AddressIndex> for StoredU16 {
104 fn from(value: P2PK65AddressIndex) -> Self {
105 Self::from(usize::from(value))
106 }
107}
108
109impl From<P2PK33AddressIndex> for StoredU16 {
110 fn from(value: P2PK33AddressIndex) -> Self {
111 Self::from(usize::from(value))
112 }
113}
114
115impl From<P2PKHAddressIndex> for StoredU16 {
116 fn from(value: P2PKHAddressIndex) -> Self {
117 Self::from(usize::from(value))
118 }
119}
120
121impl From<OpReturnIndex> for StoredU16 {
122 fn from(value: OpReturnIndex) -> Self {
123 Self::from(usize::from(value))
124 }
125}
126
127impl From<P2MSOutputIndex> for StoredU16 {
128 fn from(value: P2MSOutputIndex) -> Self {
129 Self::from(usize::from(value))
130 }
131}
132
133impl From<P2SHAddressIndex> for StoredU16 {
134 fn from(value: P2SHAddressIndex) -> Self {
135 Self::from(usize::from(value))
136 }
137}
138
139impl From<P2WSHAddressIndex> for StoredU16 {
140 fn from(value: P2WSHAddressIndex) -> Self {
141 Self::from(usize::from(value))
142 }
143}
144
145impl From<P2WPKHAddressIndex> for StoredU16 {
146 fn from(value: P2WPKHAddressIndex) -> Self {
147 Self::from(usize::from(value))
148 }
149}
150
151impl From<P2TRAddressIndex> for StoredU16 {
152 fn from(value: P2TRAddressIndex) -> Self {
153 Self::from(usize::from(value))
154 }
155}
156
157impl From<P2AAddressIndex> for StoredU16 {
158 fn from(value: P2AAddressIndex) -> Self {
159 Self::from(usize::from(value))
160 }
161}
162
163impl From<UnknownOutputIndex> for StoredU16 {
164 fn from(value: UnknownOutputIndex) -> Self {
165 Self::from(usize::from(value))
166 }
167}
168
169impl From<EmptyOutputIndex> for StoredU16 {
170 fn from(value: EmptyOutputIndex) -> Self {
171 Self::from(usize::from(value))
172 }
173}
174
175impl Printable for StoredU16 {
176 fn to_string() -> &'static str {
177 "u16"
178 }
179
180 fn to_possible_strings() -> &'static [&'static str] {
181 &["u16"]
182 }
183}