alloy_primitives/bits/
macros.rs

1/// Wrap a fixed-size byte array in a newtype, delegating all methods to the
2/// underlying [`crate::FixedBytes`].
3///
4/// This functionally creates a new named `FixedBytes` that cannot be
5/// type-confused for another named `FixedBytes`.
6///
7/// # Examples
8///
9/// ```
10/// use alloy_primitives::wrap_fixed_bytes;
11///
12/// // These hashes are the same length, and have the same functionality, but
13/// // are distinct types
14/// wrap_fixed_bytes!(pub struct KeccakOutput<32>;);
15/// wrap_fixed_bytes!(pub struct MerkleTreeItem<32>;);
16/// ```
17#[macro_export]
18macro_rules! wrap_fixed_bytes {
19    (
20        $(#[$attrs:meta])*
21        $vis:vis struct $name:ident<$n:literal>;
22    ) => {
23        $crate::wrap_fixed_bytes!(
24            extra_derives: [$crate::private::derive_more::Display],
25            $(#[$attrs])*
26            $vis struct $name<$n>;
27        );
28    };
29
30    (
31        extra_derives: [$($extra_derives:path),* $(,)?],
32        $(#[$attrs:meta])*
33        $vis:vis struct $name:ident<$n:literal>;
34    ) => {
35        $(#[$attrs])*
36        #[derive(
37            Clone,
38            Copy,
39            Default,
40            PartialEq,
41            Eq,
42            PartialOrd,
43            Ord,
44            Hash,
45            $crate::private::derive_more::AsMut,
46            $crate::private::derive_more::AsRef,
47            $crate::private::derive_more::BitAnd,
48            $crate::private::derive_more::BitAndAssign,
49            $crate::private::derive_more::BitOr,
50            $crate::private::derive_more::BitOrAssign,
51            $crate::private::derive_more::BitXor,
52            $crate::private::derive_more::BitXorAssign,
53            $crate::private::derive_more::Not,
54            $crate::private::derive_more::Deref,
55            $crate::private::derive_more::DerefMut,
56            $crate::private::derive_more::From,
57            $crate::private::derive_more::FromStr,
58            $crate::private::derive_more::Index,
59            $crate::private::derive_more::IndexMut,
60            $crate::private::derive_more::Into,
61            $crate::private::derive_more::IntoIterator,
62            $crate::private::derive_more::LowerHex,
63            $crate::private::derive_more::UpperHex,
64            $(
65                $extra_derives,
66            )*
67        )]
68        #[repr(transparent)]
69        $vis struct $name(#[into_iterator(owned, ref, ref_mut)] pub $crate::FixedBytes<$n>);
70
71        impl $crate::private::From<[u8; $n]> for $name {
72            #[inline]
73            fn from(value: [u8; $n]) -> Self {
74                Self($crate::FixedBytes(value))
75            }
76        }
77
78        impl $crate::private::From<$name> for [u8; $n] {
79            #[inline]
80            fn from(value: $name) -> Self {
81                value.0 .0
82            }
83        }
84
85        impl<'a> $crate::private::From<&'a [u8; $n]> for $name {
86            #[inline]
87            fn from(value: &'a [u8; $n]) -> Self {
88                Self($crate::FixedBytes(*value))
89            }
90        }
91
92        impl<'a> $crate::private::From<&'a mut [u8; $n]> for $name {
93            #[inline]
94            fn from(value: &'a mut [u8; $n]) -> Self {
95                Self($crate::FixedBytes(*value))
96            }
97        }
98
99        impl $crate::private::TryFrom<&[u8]> for $name {
100            type Error = $crate::private::core::array::TryFromSliceError;
101
102            #[inline]
103            fn try_from(slice: &[u8]) -> Result<Self, Self::Error> {
104                <&Self as $crate::private::TryFrom<&[u8]>>::try_from(slice).copied()
105            }
106        }
107
108        impl $crate::private::TryFrom<&mut [u8]> for $name {
109            type Error = $crate::private::core::array::TryFromSliceError;
110
111            #[inline]
112            fn try_from(slice: &mut [u8]) -> Result<Self, Self::Error> {
113                <Self as $crate::private::TryFrom<&[u8]>>::try_from(&*slice)
114            }
115        }
116
117        impl<'a> $crate::private::TryFrom<&'a [u8]> for &'a $name {
118            type Error = $crate::private::core::array::TryFromSliceError;
119
120            #[inline]
121            #[allow(unsafe_code)]
122            fn try_from(slice: &'a [u8]) -> Result<&'a $name, Self::Error> {
123                // SAFETY: `$name` is `repr(transparent)` for `FixedBytes<$n>`
124                // and consequently `[u8; $n]`
125                <&[u8; $n] as $crate::private::TryFrom<&[u8]>>::try_from(slice)
126                    .map(|array_ref| unsafe { $crate::private::core::mem::transmute(array_ref) })
127            }
128        }
129
130        impl<'a> $crate::private::TryFrom<&'a mut [u8]> for &'a mut $name {
131            type Error = $crate::private::core::array::TryFromSliceError;
132
133            #[inline]
134            #[allow(unsafe_code)]
135            fn try_from(slice: &'a mut [u8]) -> Result<&'a mut $name, Self::Error> {
136                // SAFETY: `$name` is `repr(transparent)` for `FixedBytes<$n>`
137                // and consequently `[u8; $n]`
138                <&mut [u8; $n] as $crate::private::TryFrom<&mut [u8]>>::try_from(slice)
139                    .map(|array_ref| unsafe { $crate::private::core::mem::transmute(array_ref) })
140            }
141        }
142
143        impl $crate::private::AsRef<[u8; $n]> for $name {
144            #[inline]
145            fn as_ref(&self) -> &[u8; $n] {
146                &self.0 .0
147            }
148        }
149
150        impl $crate::private::AsMut<[u8; $n]> for $name {
151            #[inline]
152            fn as_mut(&mut self) -> &mut [u8; $n] {
153                &mut self.0 .0
154            }
155        }
156
157        impl $crate::private::AsRef<[u8]> for $name {
158            #[inline]
159            fn as_ref(&self) -> &[u8] {
160                &self.0 .0
161            }
162        }
163
164        impl $crate::private::AsMut<[u8]> for $name {
165            #[inline]
166            fn as_mut(&mut self) -> &mut [u8] {
167                &mut self.0 .0
168            }
169        }
170
171        impl $crate::private::core::fmt::Debug for $name {
172            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
173                $crate::private::core::fmt::Debug::fmt(&self.0, f)
174            }
175        }
176
177        $crate::impl_fb_traits!($name, $n);
178        $crate::impl_rlp!($name, $n);
179        $crate::impl_serde!($name);
180        $crate::impl_allocative!($name);
181        $crate::impl_arbitrary!($name, $n);
182        $crate::impl_rand!($name);
183
184        impl $name {
185            /// Array of Zero bytes.
186            pub const ZERO: Self = Self($crate::FixedBytes::ZERO);
187
188            /// Wraps the given byte array in this type.
189            #[inline]
190            pub const fn new(bytes: [u8; $n]) -> Self {
191                Self($crate::FixedBytes(bytes))
192            }
193
194            /// Creates a new byte array with the last byte set to `x`.
195            #[inline]
196            pub const fn with_last_byte(x: u8) -> Self {
197                Self($crate::FixedBytes::with_last_byte(x))
198            }
199
200            /// Creates a new byte array where all bytes are set to `byte`.
201            #[inline]
202            pub const fn repeat_byte(byte: u8) -> Self {
203                Self($crate::FixedBytes::repeat_byte(byte))
204            }
205
206            /// Returns the size of this array in bytes.
207            #[inline]
208            pub const fn len_bytes() -> usize {
209                $n
210            }
211
212            $crate::impl_getrandom!();
213            $crate::impl_rand!();
214
215            /// Create a new byte array from the given slice `src`.
216            ///
217            /// For a fallible version, use the `TryFrom<&[u8]>` implementation.
218            ///
219            /// # Note
220            ///
221            /// The given bytes are interpreted in big endian order.
222            ///
223            /// # Panics
224            ///
225            /// If the length of `src` and the number of bytes in `Self` do not match.
226            #[inline]
227            #[track_caller]
228            pub fn from_slice(src: &[u8]) -> Self {
229                match Self::try_from(src) {
230                    Ok(x) => x,
231                    Err(_) => panic!("cannot convert a slice of length {} to {}", src.len(), stringify!($name)),
232                }
233            }
234
235            /// Create a new byte array from the given slice `src`, left-padding it
236            /// with zeroes if necessary.
237            ///
238            /// # Note
239            ///
240            /// The given bytes are interpreted in big endian order.
241            ///
242            /// # Panics
243            ///
244            /// Panics if `src.len() > N`.
245            #[inline]
246            #[track_caller]
247            pub fn left_padding_from(value: &[u8]) -> Self {
248                Self($crate::FixedBytes::left_padding_from(value))
249            }
250
251            /// Create a new byte array from the given slice `src`, right-padding it
252            /// with zeroes if necessary.
253            ///
254            /// # Note
255            ///
256            /// The given bytes are interpreted in big endian order.
257            ///
258            /// # Panics
259            ///
260            /// Panics if `src.len() > N`.
261            #[inline]
262            #[track_caller]
263            pub fn right_padding_from(value: &[u8]) -> Self {
264                Self($crate::FixedBytes::right_padding_from(value))
265            }
266
267            /// Returns the inner bytes array.
268            #[inline]
269            pub const fn into_array(self) -> [u8; $n] {
270                self.0 .0
271            }
272
273            /// Returns `true` if all bits set in `b` are also set in `self`.
274            #[inline]
275            pub fn covers(&self, b: &Self) -> bool {
276                &(*b & *self) == b
277            }
278
279            /// Compile-time equality. NOT constant-time equality.
280            pub const fn const_eq(&self, other: &Self) -> bool {
281                self.0.const_eq(&other.0)
282            }
283
284            /// Computes the bitwise AND of two `FixedBytes`.
285            pub const fn bit_and(self, rhs: Self) -> Self {
286                Self(self.0.bit_and(rhs.0))
287            }
288
289            /// Computes the bitwise OR of two `FixedBytes`.
290            pub const fn bit_or(self, rhs: Self) -> Self {
291                Self(self.0.bit_or(rhs.0))
292            }
293
294            /// Computes the bitwise XOR of two `FixedBytes`.
295            pub const fn bit_xor(self, rhs: Self) -> Self {
296                Self(self.0.bit_xor(rhs.0))
297            }
298        }
299    };
300}
301
302// Extra traits that cannot be derived automatically
303#[doc(hidden)]
304#[macro_export]
305macro_rules! impl_fb_traits {
306    (impl<$($const:ident)?> Borrow<$t:ty> for $b:ty) => {
307        impl<$($const N: usize)?> $crate::private::Borrow<$t> for $b {
308            #[inline]
309            fn borrow(&self) -> &$t {
310                $crate::private::Borrow::borrow(&self.0)
311            }
312        }
313    };
314
315    (impl<$($const:ident)?> BorrowMut<$t:ty> for $b:ty) => {
316        impl<$($const N: usize)?> $crate::private::BorrowMut<$t> for $b {
317            #[inline]
318            fn borrow_mut(&mut self) -> &mut $t {
319                $crate::private::BorrowMut::borrow_mut(&mut self.0)
320            }
321        }
322    };
323
324    (unsafe impl<$lt:lifetime, $($const:ident)?> From<$a:ty> for $b:ty) => {
325        impl<$lt, $($const N: usize)?> $crate::private::From<$a> for $b {
326            #[inline]
327            #[allow(unsafe_code)]
328            fn from(value: $a) -> $b {
329                // SAFETY: guaranteed by caller
330                unsafe { $crate::private::core::mem::transmute::<$a, $b>(value) }
331            }
332        }
333    };
334
335    (impl<$($const:ident)?> cmp::$tr:ident<$a:ty> for $b:ty where fn $fn:ident -> $ret:ty $(, [$e:expr])?) => {
336        impl<$($const N: usize)?> $crate::private::$tr<$a> for $b {
337            #[inline]
338            fn $fn(&self, other: &$a) -> $ret {
339                $crate::private::$tr::$fn(&self.0 $([$e])?, other)
340            }
341        }
342
343        impl<$($const N: usize)?> $crate::private::$tr<$b> for $a {
344            #[inline]
345            fn $fn(&self, other: &$b) -> $ret {
346                $crate::private::$tr::$fn(self, &other.0 $([$e])?)
347            }
348        }
349
350        impl<$($const N: usize)?> $crate::private::$tr<&$a> for $b {
351            #[inline]
352            fn $fn(&self, other: &&$a) -> $ret {
353                $crate::private::$tr::$fn(&self.0 $([$e])?, *other)
354            }
355        }
356
357        impl<$($const N: usize)?> $crate::private::$tr<$b> for &$a {
358            #[inline]
359            fn $fn(&self, other: &$b) -> $ret {
360                $crate::private::$tr::$fn(*self, &other.0 $([$e])?)
361            }
362        }
363
364        impl<$($const N: usize)?> $crate::private::$tr<$a> for &$b {
365            #[inline]
366            fn $fn(&self, other: &$a) -> $ret {
367                $crate::private::$tr::$fn(&self.0 $([$e])?, other)
368            }
369        }
370
371        impl<$($const N: usize)?> $crate::private::$tr<&$b> for $a {
372            #[inline]
373            fn $fn(&self, other: &&$b) -> $ret {
374                $crate::private::$tr::$fn(self, &other.0 $([$e])?)
375            }
376        }
377    };
378
379    ($t:ty, $n:tt $(, $const:ident)?) => {
380        // Borrow is not automatically implemented for references
381        $crate::impl_fb_traits!(impl<$($const)?> Borrow<[u8]>        for $t);
382        $crate::impl_fb_traits!(impl<$($const)?> Borrow<[u8]>        for &$t);
383        $crate::impl_fb_traits!(impl<$($const)?> Borrow<[u8]>        for &mut $t);
384        $crate::impl_fb_traits!(impl<$($const)?> Borrow<[u8; $n]>    for $t);
385        $crate::impl_fb_traits!(impl<$($const)?> Borrow<[u8; $n]>    for &$t);
386        $crate::impl_fb_traits!(impl<$($const)?> Borrow<[u8; $n]>    for &mut $t);
387
388        $crate::impl_fb_traits!(impl<$($const)?> BorrowMut<[u8]>     for $t);
389        $crate::impl_fb_traits!(impl<$($const)?> BorrowMut<[u8]>     for &mut $t);
390        $crate::impl_fb_traits!(impl<$($const)?> BorrowMut<[u8; $n]> for $t);
391        $crate::impl_fb_traits!(impl<$($const)?> BorrowMut<[u8; $n]> for &mut $t);
392
393        // Implement conversion traits for references with `mem::transmute`
394        // SAFETY: `repr(transparent)`
395        $crate::impl_fb_traits!(unsafe impl<'a, $($const)?> From<&'a [u8; $n]>     for &'a $t);
396        $crate::impl_fb_traits!(unsafe impl<'a, $($const)?> From<&'a mut [u8; $n]> for &'a $t);
397        $crate::impl_fb_traits!(unsafe impl<'a, $($const)?> From<&'a mut [u8; $n]> for &'a mut $t);
398
399        $crate::impl_fb_traits!(unsafe impl<'a, $($const)?> From<&'a $t>           for &'a [u8; $n]);
400        $crate::impl_fb_traits!(unsafe impl<'a, $($const)?> From<&'a mut $t>       for &'a [u8; $n]);
401        $crate::impl_fb_traits!(unsafe impl<'a, $($const)?> From<&'a mut $t>       for &'a mut [u8; $n]);
402
403        // Implement PartialEq, PartialOrd, with slice and array
404        $crate::impl_fb_traits!(impl<$($const)?> cmp::PartialEq<[u8]> for $t where fn eq -> bool);
405        $crate::impl_fb_traits!(impl<$($const)?> cmp::PartialEq<[u8; $n]> for $t where fn eq -> bool);
406        $crate::impl_fb_traits!(
407            impl<$($const)?> cmp::PartialOrd<[u8]> for $t
408            where
409                fn partial_cmp -> $crate::private::Option<$crate::private::Ordering>,
410                [..] // slices $t
411        );
412
413        impl<$($const N: usize)?> $crate::hex::FromHex for $t {
414            type Error = $crate::hex::FromHexError;
415
416            #[inline]
417            fn from_hex<T: $crate::private::AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
418                $crate::hex::decode_to_array(hex).map(Self::new)
419            }
420        }
421    };
422}
423
424#[doc(hidden)]
425#[macro_export]
426#[cfg(feature = "getrandom")]
427macro_rules! impl_getrandom {
428    () => {
429        /// Creates a new fixed byte array with the default cryptographic random number
430        /// generator.
431        ///
432        /// This is `rand::thread_rng` if the "rand" and "std" features are enabled, otherwise
433        /// it uses `getrandom::getrandom`. Both are cryptographically secure.
434        #[inline]
435        #[track_caller]
436        #[cfg_attr(docsrs, doc(cfg(feature = "getrandom")))]
437        pub fn random() -> Self {
438            Self($crate::FixedBytes::random())
439        }
440
441        /// Tries to create a new fixed byte array with the default cryptographic random number
442        /// generator.
443        ///
444        /// See [`random`](Self::random) for more details.
445        #[inline]
446        #[cfg_attr(docsrs, doc(cfg(feature = "getrandom")))]
447        pub fn try_random() -> $crate::private::Result<Self, $crate::private::getrandom::Error> {
448            $crate::FixedBytes::try_random().map(Self)
449        }
450
451        /// Fills this fixed byte array with the default cryptographic random number generator.
452        ///
453        /// See [`random`](Self::random) for more details.
454        #[inline]
455        #[track_caller]
456        #[cfg_attr(docsrs, doc(cfg(feature = "getrandom")))]
457        pub fn randomize(&mut self) {
458            self.0.randomize();
459        }
460
461        /// Tries to fill this fixed byte array with the default cryptographic random number
462        /// generator.
463        ///
464        /// See [`random`](Self::random) for more details.
465        #[inline]
466        #[cfg_attr(docsrs, doc(cfg(feature = "getrandom")))]
467        pub fn try_randomize(
468            &mut self,
469        ) -> $crate::private::Result<(), $crate::private::getrandom::Error> {
470            self.0.try_randomize()
471        }
472    };
473}
474
475#[doc(hidden)]
476#[macro_export]
477#[cfg(not(feature = "getrandom"))]
478macro_rules! impl_getrandom {
479    () => {};
480}
481
482#[doc(hidden)]
483#[macro_export]
484#[cfg(feature = "rand")]
485macro_rules! impl_rand {
486    () => {
487        /// Creates a new fixed byte array with the given random number generator.
488        #[inline]
489        #[doc(alias = "random_using")]
490        #[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
491        pub fn random_with<R: $crate::private::rand::RngCore + ?Sized>(rng: &mut R) -> Self {
492            Self($crate::FixedBytes::random_with(rng))
493        }
494
495        /// Fills this fixed byte array with the given random number generator.
496        #[inline]
497        #[doc(alias = "randomize_using")]
498        #[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
499        pub fn randomize_with<R: $crate::private::rand::RngCore + ?Sized>(&mut self, rng: &mut R) {
500            self.0.randomize_with(rng);
501        }
502    };
503
504    ($t:ty) => {
505        #[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
506        impl $crate::private::rand::distributions::Distribution<$t>
507            for $crate::private::rand::distributions::Standard
508        {
509            #[inline]
510            fn sample<R: $crate::private::rand::Rng + ?Sized>(&self, rng: &mut R) -> $t {
511                <$t>::random_with(rng)
512            }
513        }
514    };
515}
516
517#[doc(hidden)]
518#[macro_export]
519#[cfg(not(feature = "rand"))]
520macro_rules! impl_rand {
521    ($($t:tt)*) => {};
522}
523
524#[doc(hidden)]
525#[macro_export]
526#[cfg(feature = "rlp")]
527macro_rules! impl_rlp {
528    ($t:ty, $n:literal) => {
529        #[cfg_attr(docsrs, doc(cfg(feature = "rlp")))]
530        impl $crate::private::alloy_rlp::Decodable for $t {
531            #[inline]
532            fn decode(buf: &mut &[u8]) -> $crate::private::alloy_rlp::Result<Self> {
533                $crate::private::alloy_rlp::Decodable::decode(buf).map(Self)
534            }
535        }
536
537        #[cfg_attr(docsrs, doc(cfg(feature = "rlp")))]
538        impl $crate::private::alloy_rlp::Encodable for $t {
539            #[inline]
540            fn length(&self) -> usize {
541                $crate::private::alloy_rlp::Encodable::length(&self.0)
542            }
543
544            #[inline]
545            fn encode(&self, out: &mut dyn bytes::BufMut) {
546                $crate::private::alloy_rlp::Encodable::encode(&self.0, out)
547            }
548        }
549
550        $crate::private::alloy_rlp::impl_max_encoded_len!($t, {
551            $n + $crate::private::alloy_rlp::length_of_length($n)
552        });
553    };
554}
555
556#[doc(hidden)]
557#[macro_export]
558#[cfg(not(feature = "rlp"))]
559macro_rules! impl_rlp {
560    ($t:ty, $n:literal) => {};
561}
562
563#[doc(hidden)]
564#[macro_export]
565#[cfg(feature = "allocative")]
566macro_rules! impl_allocative {
567    ($t:ty) => {
568        #[cfg_attr(docsrs, doc(cfg(feature = "allocative")))]
569        impl $crate::private::allocative::Allocative for $t {
570            #[inline]
571            fn visit<'a, 'b: 'a>(&self, visitor: &'a mut $crate::private::allocative::Visitor<'b>) {
572                $crate::private::allocative::Allocative::visit(&self.0, visitor)
573            }
574        }
575    };
576}
577
578#[doc(hidden)]
579#[macro_export]
580#[cfg(not(feature = "allocative"))]
581macro_rules! impl_allocative {
582    ($t:ty) => {};
583}
584
585#[doc(hidden)]
586#[macro_export]
587#[cfg(feature = "serde")]
588macro_rules! impl_serde {
589    ($t:ty) => {
590        #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
591        impl $crate::private::serde::Serialize for $t {
592            #[inline]
593            fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
594                $crate::private::serde::Serialize::serialize(&self.0, serializer)
595            }
596        }
597
598        #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
599        impl<'de> $crate::private::serde::Deserialize<'de> for $t {
600            #[inline]
601            fn deserialize<D: $crate::private::serde::Deserializer<'de>>(
602                deserializer: D,
603            ) -> Result<Self, D::Error> {
604                $crate::private::serde::Deserialize::deserialize(deserializer).map(Self)
605            }
606        }
607    };
608}
609
610#[doc(hidden)]
611#[macro_export]
612#[cfg(not(feature = "serde"))]
613macro_rules! impl_serde {
614    ($t:ty) => {};
615}
616
617#[doc(hidden)]
618#[macro_export]
619#[cfg(feature = "arbitrary")]
620macro_rules! impl_arbitrary {
621    ($t:ty, $n:literal) => {
622        #[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
623        impl<'a> $crate::private::arbitrary::Arbitrary<'a> for $t {
624            #[inline]
625            fn arbitrary(u: &mut $crate::private::arbitrary::Unstructured<'a>) -> $crate::private::arbitrary::Result<Self> {
626                <$crate::FixedBytes<$n> as $crate::private::arbitrary::Arbitrary>::arbitrary(u).map(Self)
627            }
628
629            #[inline]
630            fn arbitrary_take_rest(u: $crate::private::arbitrary::Unstructured<'a>) -> $crate::private::arbitrary::Result<Self> {
631                <$crate::FixedBytes<$n> as $crate::private::arbitrary::Arbitrary>::arbitrary_take_rest(u).map(Self)
632            }
633
634            #[inline]
635            fn size_hint(depth: usize) -> (usize, Option<usize>) {
636                <$crate::FixedBytes<$n> as $crate::private::arbitrary::Arbitrary>::size_hint(depth)
637            }
638        }
639
640        #[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
641        impl $crate::private::proptest::arbitrary::Arbitrary for $t {
642            type Parameters = <$crate::FixedBytes<$n> as $crate::private::proptest::arbitrary::Arbitrary>::Parameters;
643            type Strategy = $crate::private::proptest::strategy::Map<
644                <$crate::FixedBytes<$n> as $crate::private::proptest::arbitrary::Arbitrary>::Strategy,
645                fn($crate::FixedBytes<$n>) -> Self,
646            >;
647
648            #[inline]
649            fn arbitrary() -> Self::Strategy {
650                use $crate::private::proptest::strategy::Strategy;
651                <$crate::FixedBytes<$n> as $crate::private::proptest::arbitrary::Arbitrary>::arbitrary()
652                    .prop_map(Self)
653            }
654
655            #[inline]
656            fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
657                use $crate::private::proptest::strategy::Strategy;
658                <$crate::FixedBytes<$n> as $crate::private::proptest::arbitrary::Arbitrary>::arbitrary_with(args)
659                    .prop_map(Self)
660            }
661        }
662    };
663}
664
665#[doc(hidden)]
666#[macro_export]
667#[cfg(not(feature = "arbitrary"))]
668macro_rules! impl_arbitrary {
669    ($t:ty, $n:literal) => {};
670}
671
672macro_rules! fixed_bytes_macros {
673    ($d:tt $($(#[$attr:meta])* macro $name:ident($ty:ident $($rest:tt)*);)*) => {$(
674        /// Converts a sequence of string literals containing hex-encoded data
675        #[doc = concat!(
676            "into a new [`", stringify!($ty), "`][crate::", stringify!($ty), "] at compile time.\n",
677        )]
678        ///
679        /// If the input is empty, a zero-initialized array is returned.
680        ///
681        /// See [`hex!`](crate::hex!) for more information.
682        ///
683        /// # Examples
684        ///
685        /// ```
686        #[doc = concat!("use alloy_primitives::{", stringify!($name), ", ", stringify!($ty), "};")]
687        ///
688        #[doc = concat!("const ZERO: ", stringify!($ty $($rest)*), " = ", stringify!($name), "!();")]
689        #[doc = concat!("assert_eq!(ZERO, ", stringify!($ty), "::ZERO);")]
690        ///
691        /// # stringify!(
692        #[doc = concat!("let byte_array: ", stringify!($ty), " = ", stringify!($name), "!(\"0x0123abcd…\");")]
693        /// # );
694        /// ```
695        $(#[$attr])*
696        #[macro_export]
697        macro_rules! $name {
698            () => {
699                $crate::$ty::ZERO
700            };
701
702            ($d ($d t:tt)+) => {
703                $crate::$ty::new($crate::hex!($d ($d t)+))
704            };
705        }
706    )*};
707}
708
709fixed_bytes_macros! { $
710    macro address(Address);
711
712    macro b64(B64);
713
714    macro b128(B128);
715
716    macro b256(B256);
717
718    macro b512(B512);
719
720    macro bloom(Bloom);
721
722    macro fixed_bytes(FixedBytes<0>); // <0> is just for the doctest
723}
724
725/// Converts a sequence of string literals containing hex-encoded data into a
726/// new [`Bytes`][crate::Bytes] at compile time.
727///
728/// If the input is empty, an empty instance is returned.
729///
730/// See [`hex!`](crate::hex!) for more information.
731///
732/// # Examples
733///
734/// ```
735/// use alloy_primitives::{bytes, Bytes};
736///
737/// static MY_BYTES: Bytes = bytes!("0x0123" "0xabcd");
738/// assert_eq!(MY_BYTES, Bytes::from(&[0x01, 0x23, 0xab, 0xcd]));
739/// ```
740#[macro_export]
741macro_rules! bytes {
742    () => {
743        $crate::Bytes::new()
744    };
745
746    ($($s:literal)+) => {const {
747        $crate::Bytes::from_static(&$crate::hex!($($s)+))
748    }};
749
750    [$($inner:expr),+ $(,)?] => {const {
751        $crate::Bytes::from_static(&[$($inner),+])
752    }};
753
754    [$inner:expr; $size:literal] => {const {
755        $crate::Bytes::from_static(&[$inner; $size])
756    }};
757}
758
759#[cfg(test)]
760mod tests {
761    use crate::{hex, Address, Bytes, FixedBytes};
762
763    #[test]
764    fn bytes_macros() {
765        static B1: Bytes = bytes!("010203040506070809");
766        static B2: Bytes = bytes![1, 2, 3, 4, 5, 6, 7, 8, 9];
767        static B3: Bytes = bytes![1, 2, 3, 4, 5, 6, 7, 8, 9,];
768
769        assert_eq!(B1, B2);
770        assert_eq!(B1, B3);
771
772        static B4: Bytes = bytes!("0000");
773        static B5: Bytes = bytes![0; 2];
774        static B6: Bytes = bytes![0, 0];
775        assert_eq!(B4, B5);
776        assert_eq!(B4, B6);
777    }
778
779    #[test]
780    fn fixed_byte_macros() {
781        const A0: Address = address!();
782        assert_eq!(A0, Address::ZERO);
783
784        const A1: Address = address!("0x0102030405060708090a0b0c0d0e0f1011121314");
785        const A2: Address = Address(fixed_bytes!("0x0102030405060708090a0b0c0d0e0f1011121314"));
786        const A3: Address = Address(FixedBytes(hex!("0x0102030405060708090a0b0c0d0e0f1011121314")));
787        assert_eq!(A1, A2);
788        assert_eq!(A1, A3);
789        assert_eq!(A1, hex!("0x0102030405060708090a0b0c0d0e0f1011121314"));
790
791        static B: Bytes = bytes!("0x112233");
792        assert_eq!(B[..], [0x11, 0x22, 0x33]);
793
794        static EMPTY_BYTES1: Bytes = bytes!();
795        static EMPTY_BYTES2: Bytes = bytes!("");
796        assert!(EMPTY_BYTES1.is_empty());
797        assert_eq!(EMPTY_BYTES1, Bytes::new());
798        assert_eq!(EMPTY_BYTES1, EMPTY_BYTES2);
799    }
800}