1use crate::aliases;
2use core::{fmt, iter, ops, str};
3use derive_more::{Deref, DerefMut, From, Index, IndexMut, IntoIterator};
4use hex::FromHex;
5
6#[derive(
16 Clone,
17 Copy,
18 PartialEq,
19 Eq,
20 PartialOrd,
21 Ord,
22 Hash,
23 Deref,
24 DerefMut,
25 From,
26 Index,
27 IndexMut,
28 IntoIterator,
29)]
30#[cfg_attr(feature = "arbitrary", derive(derive_arbitrary::Arbitrary, proptest_derive::Arbitrary))]
31#[cfg_attr(feature = "allocative", derive(allocative::Allocative))]
32#[repr(transparent)]
33pub struct FixedBytes<const N: usize>(#[into_iterator(owned, ref, ref_mut)] pub [u8; N]);
34
35crate::impl_fb_traits!(FixedBytes<N>, N, const);
36
37impl<const N: usize> Default for FixedBytes<N> {
38 #[inline]
39 fn default() -> Self {
40 Self::ZERO
41 }
42}
43
44impl<'a, const N: usize> Default for &'a FixedBytes<N> {
45 #[inline]
46 fn default() -> Self {
47 &FixedBytes::ZERO
48 }
49}
50
51impl<const N: usize> From<&[u8; N]> for FixedBytes<N> {
52 #[inline]
53 fn from(bytes: &[u8; N]) -> Self {
54 Self(*bytes)
55 }
56}
57
58impl<const N: usize> From<&mut [u8; N]> for FixedBytes<N> {
59 #[inline]
60 fn from(bytes: &mut [u8; N]) -> Self {
61 Self(*bytes)
62 }
63}
64
65impl<const N: usize> TryFrom<&[u8]> for FixedBytes<N> {
68 type Error = core::array::TryFromSliceError;
69
70 #[inline]
71 fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
72 <&Self>::try_from(slice).copied()
73 }
74}
75
76impl<const N: usize> TryFrom<&mut [u8]> for FixedBytes<N> {
79 type Error = core::array::TryFromSliceError;
80
81 #[inline]
82 fn try_from(slice: &mut [u8]) -> Result<Self, Self::Error> {
83 Self::try_from(&*slice)
84 }
85}
86
87impl<'a, const N: usize> TryFrom<&'a [u8]> for &'a FixedBytes<N> {
90 type Error = core::array::TryFromSliceError;
91
92 #[inline]
93 fn try_from(slice: &'a [u8]) -> Result<&'a FixedBytes<N>, Self::Error> {
94 <&[u8; N]>::try_from(slice).map(|array_ref| unsafe { core::mem::transmute(array_ref) })
96 }
97}
98
99impl<'a, const N: usize> TryFrom<&'a mut [u8]> for &'a mut FixedBytes<N> {
102 type Error = core::array::TryFromSliceError;
103
104 #[inline]
105 fn try_from(slice: &'a mut [u8]) -> Result<&'a mut FixedBytes<N>, Self::Error> {
106 <&mut [u8; N]>::try_from(slice).map(|array_ref| unsafe { core::mem::transmute(array_ref) })
108 }
109}
110
111macro_rules! fixed_bytes_uint_conversions {
115 ($($int:ty => $fb:ty),* $(,)?) => {$(
116 impl From<$int> for $fb {
117 #[inline]
120 fn from(value: $int) -> Self {
121 Self(value.to_be_bytes())
122 }
123 }
124
125 impl From<$fb> for $int {
126 #[inline]
129 fn from(value: $fb) -> Self {
130 Self::from_be_bytes(value.0)
131 }
132 }
133
134 const _: () = assert!(<$int>::BITS as usize == <$fb>::len_bytes() * 8);
135 )*};
136}
137
138fixed_bytes_uint_conversions! {
139 u8 => aliases::B8,
140 aliases::U8 => aliases::B8,
141 i8 => aliases::B8,
142 aliases::I8 => aliases::B8,
143
144 u16 => aliases::B16,
145 aliases::U16 => aliases::B16,
146 i16 => aliases::B16,
147 aliases::I16 => aliases::B16,
148
149 u32 => aliases::B32,
150 aliases::U32 => aliases::B32,
151 i32 => aliases::B32,
152 aliases::I32 => aliases::B32,
153
154 u64 => aliases::B64,
155 aliases::U64 => aliases::B64,
156 i64 => aliases::B64,
157 aliases::I64 => aliases::B64,
158
159 u128 => aliases::B128,
160 aliases::U128 => aliases::B128,
161 i128 => aliases::B128,
162 aliases::I128 => aliases::B128,
163
164 aliases::U160 => aliases::B160,
165 aliases::I160 => aliases::B160,
166
167 aliases::U256 => aliases::B256,
168 aliases::I256 => aliases::B256,
169
170 aliases::U512 => aliases::B512,
171 aliases::I512 => aliases::B512,
172
173}
174
175impl<const N: usize> From<FixedBytes<N>> for [u8; N] {
176 #[inline]
177 fn from(s: FixedBytes<N>) -> Self {
178 s.0
179 }
180}
181
182impl<const N: usize> AsRef<[u8; N]> for FixedBytes<N> {
183 #[inline]
184 fn as_ref(&self) -> &[u8; N] {
185 &self.0
186 }
187}
188
189impl<const N: usize> AsMut<[u8; N]> for FixedBytes<N> {
190 #[inline]
191 fn as_mut(&mut self) -> &mut [u8; N] {
192 &mut self.0
193 }
194}
195
196impl<const N: usize> AsRef<[u8]> for FixedBytes<N> {
197 #[inline]
198 fn as_ref(&self) -> &[u8] {
199 &self.0
200 }
201}
202
203impl<const N: usize> AsMut<[u8]> for FixedBytes<N> {
204 #[inline]
205 fn as_mut(&mut self) -> &mut [u8] {
206 &mut self.0
207 }
208}
209
210impl<const N: usize> fmt::Debug for FixedBytes<N> {
211 #[inline]
212 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213 self.fmt_hex::<false>(f, true)
214 }
215}
216
217impl<const N: usize> fmt::Display for FixedBytes<N> {
218 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 if N <= 4 || !f.alternate() {
221 return self.fmt_hex::<false>(f, true);
222 }
223
224 const SEP_LEN: usize = '…'.len_utf8();
226 let mut buf = [0; 2 + 4 + SEP_LEN + 4];
227 buf[0] = b'0';
228 buf[1] = b'x';
229 hex::encode_to_slice(&self.0[0..2], &mut buf[2..6]).unwrap();
230 '…'.encode_utf8(&mut buf[6..]);
231 hex::encode_to_slice(&self.0[N - 2..N], &mut buf[6 + SEP_LEN..]).unwrap();
232
233 f.write_str(unsafe { str::from_utf8_unchecked(&buf) })
235 }
236}
237
238impl<const N: usize> fmt::LowerHex for FixedBytes<N> {
239 #[inline]
240 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241 self.fmt_hex::<false>(f, f.alternate())
242 }
243}
244
245impl<const N: usize> fmt::UpperHex for FixedBytes<N> {
246 #[inline]
247 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
248 self.fmt_hex::<true>(f, f.alternate())
249 }
250}
251
252impl<const N: usize> ops::BitAnd for FixedBytes<N> {
253 type Output = Self;
254
255 #[inline]
256 fn bitand(mut self, rhs: Self) -> Self::Output {
257 self &= rhs;
258 self
259 }
260}
261
262impl<const N: usize> ops::BitAndAssign for FixedBytes<N> {
263 #[inline]
264 fn bitand_assign(&mut self, rhs: Self) {
265 iter::zip(self, &rhs).for_each(|(a, b)| *a &= *b);
267 }
268}
269
270impl<const N: usize> ops::BitOr for FixedBytes<N> {
271 type Output = Self;
272
273 #[inline]
274 fn bitor(mut self, rhs: Self) -> Self::Output {
275 self |= rhs;
276 self
277 }
278}
279
280impl<const N: usize> ops::BitOrAssign for FixedBytes<N> {
281 #[inline]
282 fn bitor_assign(&mut self, rhs: Self) {
283 iter::zip(self, &rhs).for_each(|(a, b)| *a |= *b);
285 }
286}
287
288impl<const N: usize> ops::BitXor for FixedBytes<N> {
289 type Output = Self;
290
291 #[inline]
292 fn bitxor(mut self, rhs: Self) -> Self::Output {
293 self ^= rhs;
294 self
295 }
296}
297
298impl<const N: usize> ops::BitXorAssign for FixedBytes<N> {
299 #[inline]
300 fn bitxor_assign(&mut self, rhs: Self) {
301 iter::zip(self, &rhs).for_each(|(a, b)| *a ^= *b);
303 }
304}
305
306impl<const N: usize> ops::Not for FixedBytes<N> {
307 type Output = Self;
308
309 #[inline]
310 fn not(mut self) -> Self::Output {
311 self.iter_mut().for_each(|byte| *byte = !*byte);
312 self
313 }
314}
315
316impl<const N: usize> str::FromStr for FixedBytes<N> {
317 type Err = hex::FromHexError;
318
319 #[inline]
320 fn from_str(s: &str) -> Result<Self, Self::Err> {
321 Self::from_hex(s)
322 }
323}
324
325#[cfg(feature = "rand")]
326impl<const N: usize> rand::distributions::Distribution<FixedBytes<N>>
327 for rand::distributions::Standard
328{
329 #[inline]
330 fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> FixedBytes<N> {
331 FixedBytes::random_with(rng)
332 }
333}
334
335impl<const N: usize> FixedBytes<N> {
336 pub const ZERO: Self = Self([0u8; N]);
338
339 #[inline]
341 pub const fn new(bytes: [u8; N]) -> Self {
342 Self(bytes)
343 }
344
345 #[inline]
347 pub const fn with_last_byte(x: u8) -> Self {
348 let mut bytes = [0u8; N];
349 if N > 0 {
350 bytes[N - 1] = x;
351 }
352 Self(bytes)
353 }
354
355 #[inline]
357 pub const fn repeat_byte(byte: u8) -> Self {
358 Self([byte; N])
359 }
360
361 #[inline(always)]
363 pub const fn len_bytes() -> usize {
364 N
365 }
366
367 #[cfg(feature = "getrandom")]
374 #[inline]
375 #[track_caller]
376 pub fn random() -> Self {
377 Self::try_random().unwrap()
378 }
379
380 #[cfg(feature = "getrandom")]
388 #[inline]
389 pub fn try_random() -> Result<Self, getrandom::Error> {
390 let mut bytes = Self::ZERO;
391 bytes.try_randomize()?;
392 Ok(bytes)
393 }
394
395 #[cfg(feature = "rand")]
397 #[inline]
398 #[doc(alias = "random_using")]
399 pub fn random_with<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
400 let mut bytes = Self::ZERO;
401 bytes.randomize_with(rng);
402 bytes
403 }
404
405 #[cfg(feature = "getrandom")]
412 #[inline]
413 #[track_caller]
414 pub fn randomize(&mut self) {
415 self.try_randomize().unwrap()
416 }
417
418 #[inline]
425 #[cfg(feature = "getrandom")]
426 pub fn try_randomize(&mut self) -> Result<(), getrandom::Error> {
427 getrandom::getrandom(&mut self.0)
428 }
429
430 #[cfg(feature = "rand")]
432 #[doc(alias = "randomize_using")]
433 pub fn randomize_with<R: rand::Rng + ?Sized>(&mut self, rng: &mut R) {
434 rng.fill_bytes(&mut self.0);
435 }
436
437 pub const fn concat_const<const M: usize, const Z: usize>(
446 self,
447 other: FixedBytes<M>,
448 ) -> FixedBytes<Z> {
449 assert!(N + M == Z, "Output size `Z` must equal the sum of the input sizes `N` and `M`");
450
451 let mut result = [0u8; Z];
452 let mut i = 0;
453 while i < Z {
454 result[i] = if i >= N { other.0[i - N] } else { self.0[i] };
455 i += 1;
456 }
457 FixedBytes(result)
458 }
459
460 #[inline]
472 #[track_caller]
473 pub fn from_slice(src: &[u8]) -> Self {
474 match Self::try_from(src) {
475 Ok(x) => x,
476 Err(_) => panic!("cannot convert a slice of length {} to FixedBytes<{N}>", src.len()),
477 }
478 }
479
480 #[inline]
491 #[track_caller]
492 pub fn left_padding_from(value: &[u8]) -> Self {
493 let len = value.len();
494 assert!(len <= N, "slice is too large. Expected <={N} bytes, got {len}");
495 let mut bytes = Self::ZERO;
496 bytes[N - len..].copy_from_slice(value);
497 bytes
498 }
499
500 #[inline]
511 #[track_caller]
512 pub fn right_padding_from(value: &[u8]) -> Self {
513 let len = value.len();
514 assert!(len <= N, "slice is too large. Expected <={N} bytes, got {len}");
515 let mut bytes = Self::ZERO;
516 bytes[..len].copy_from_slice(value);
517 bytes
518 }
519
520 #[inline]
522 pub const fn as_slice(&self) -> &[u8] {
523 &self.0
524 }
525
526 #[inline]
529 pub fn as_mut_slice(&mut self) -> &mut [u8] {
530 &mut self.0
531 }
532
533 #[inline]
535 pub fn covers(&self, other: &Self) -> bool {
536 (*self & *other) == *other
537 }
538
539 pub const fn const_covers(self, other: Self) -> bool {
541 other.const_eq(&self.bit_and(other))
543 }
544
545 pub const fn const_eq(&self, other: &Self) -> bool {
547 let mut i = 0;
548 while i < N {
549 if self.0[i] != other.0[i] {
550 return false;
551 }
552 i += 1;
553 }
554 true
555 }
556
557 #[inline]
559 pub fn is_zero(&self) -> bool {
560 *self == Self::ZERO
561 }
562
563 #[inline]
565 pub const fn const_is_zero(&self) -> bool {
566 self.const_eq(&Self::ZERO)
567 }
568
569 pub const fn bit_and(self, rhs: Self) -> Self {
571 let mut ret = Self::ZERO;
572 let mut i = 0;
573 while i < N {
574 ret.0[i] = self.0[i] & rhs.0[i];
575 i += 1;
576 }
577 ret
578 }
579
580 pub const fn bit_or(self, rhs: Self) -> Self {
582 let mut ret = Self::ZERO;
583 let mut i = 0;
584 while i < N {
585 ret.0[i] = self.0[i] | rhs.0[i];
586 i += 1;
587 }
588 ret
589 }
590
591 pub const fn bit_xor(self, rhs: Self) -> Self {
593 let mut ret = Self::ZERO;
594 let mut i = 0;
595 while i < N {
596 ret.0[i] = self.0[i] ^ rhs.0[i];
597 i += 1;
598 }
599 ret
600 }
601
602 fn fmt_hex<const UPPER: bool>(&self, f: &mut fmt::Formatter<'_>, prefix: bool) -> fmt::Result {
603 let mut buf = hex::Buffer::<N, true>::new();
604 let s = if UPPER { buf.format_upper(self) } else { buf.format(self) };
605 f.write_str(unsafe { s.get_unchecked((!prefix as usize) * 2..) })
607 }
608}
609
610#[cfg(test)]
611mod tests {
612 use super::*;
613
614 macro_rules! test_fmt {
615 ($($fmt:literal, $hex:literal => $expected:literal;)+) => {$(
616 assert_eq!(
617 format!($fmt, fixed_bytes!($hex)),
618 $expected
619 );
620 )+};
621 }
622
623 #[test]
624 fn concat_const() {
625 const A: FixedBytes<2> = fixed_bytes!("0123");
626 const B: FixedBytes<2> = fixed_bytes!("4567");
627 const EXPECTED: FixedBytes<4> = fixed_bytes!("01234567");
628 const ACTUAL: FixedBytes<4> = A.concat_const(B);
629
630 assert_eq!(ACTUAL, EXPECTED);
631 }
632
633 #[test]
634 fn display() {
635 test_fmt! {
636 "{}", "0123456789abcdef" => "0x0123456789abcdef";
637 "{:#}", "0123" => "0x0123";
638 "{:#}", "01234567" => "0x01234567";
639 "{:#}", "0123456789" => "0x0123…6789";
640 }
641 }
642
643 #[test]
644 fn debug() {
645 test_fmt! {
646 "{:?}", "0123456789abcdef" => "0x0123456789abcdef";
647 "{:#?}", "0123456789abcdef" => "0x0123456789abcdef";
648 }
649 }
650
651 #[test]
652 fn lower_hex() {
653 test_fmt! {
654 "{:x}", "0123456789abcdef" => "0123456789abcdef";
655 "{:#x}", "0123456789abcdef" => "0x0123456789abcdef";
656 }
657 }
658
659 #[test]
660 fn upper_hex() {
661 test_fmt! {
662 "{:X}", "0123456789abcdef" => "0123456789ABCDEF";
663 "{:#X}", "0123456789abcdef" => "0x0123456789ABCDEF";
664 }
665 }
666
667 #[test]
668 fn left_padding_from() {
669 assert_eq!(FixedBytes::<4>::left_padding_from(&[0x01, 0x23]), fixed_bytes!("00000123"));
670
671 assert_eq!(
672 FixedBytes::<4>::left_padding_from(&[0x01, 0x23, 0x45, 0x67]),
673 fixed_bytes!("01234567")
674 );
675 }
676
677 #[test]
678 #[should_panic(expected = "slice is too large. Expected <=4 bytes, got 5")]
679 fn left_padding_from_too_large() {
680 FixedBytes::<4>::left_padding_from(&[0x01, 0x23, 0x45, 0x67, 0x89]);
681 }
682
683 #[test]
684 fn right_padding_from() {
685 assert_eq!(FixedBytes::<4>::right_padding_from(&[0x01, 0x23]), fixed_bytes!("01230000"));
686
687 assert_eq!(
688 FixedBytes::<4>::right_padding_from(&[0x01, 0x23, 0x45, 0x67]),
689 fixed_bytes!("01234567")
690 );
691 }
692
693 #[test]
694 #[should_panic(expected = "slice is too large. Expected <=4 bytes, got 5")]
695 fn right_padding_from_too_large() {
696 FixedBytes::<4>::right_padding_from(&[0x01, 0x23, 0x45, 0x67, 0x89]);
697 }
698}