alloy_primitives/
sealed.rsuse crate::B256;
use derive_more::Deref;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deref)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
pub struct Sealed<T> {
#[deref]
#[cfg_attr(feature = "serde", serde(flatten))]
inner: T,
#[cfg_attr(feature = "serde", serde(flatten, alias = "hash"))]
seal: B256,
}
impl<T> Sealed<T> {
pub fn new(inner: T) -> Self
where
T: Sealable,
{
let seal = inner.hash_slow();
Self { inner, seal }
}
pub fn new_ref(inner: &T) -> Sealed<&T>
where
T: Sealable,
{
let seal = inner.hash_slow();
Sealed { inner, seal }
}
pub fn new_with<F>(inner: T, f: F) -> Self
where
T: Sized,
F: FnOnce(&T) -> B256,
{
let seal = f(&inner);
Self::new_unchecked(inner, seal)
}
pub fn new_ref_with<F>(inner: &T, f: F) -> Sealed<&T>
where
T: Sized,
F: FnOnce(&T) -> B256,
{
let seal = f(inner);
Sealed::new_unchecked(inner, seal)
}
pub const fn new_unchecked(inner: T, seal: B256) -> Self {
Self { inner, seal }
}
#[allow(clippy::missing_const_for_fn)] pub fn into_parts(self) -> (T, B256) {
(self.inner, self.seal)
}
#[allow(clippy::missing_const_for_fn)] pub fn split(self) -> (T, B256) {
self.into_parts()
}
#[inline(always)]
pub const fn inner(&self) -> &T {
&self.inner
}
#[inline(always)]
pub const fn seal(&self) -> B256 {
self.seal
}
#[inline(always)]
#[allow(clippy::missing_const_for_fn)] pub fn into_inner(self) -> T {
self.inner
}
#[inline(always)]
#[allow(clippy::missing_const_for_fn)] pub fn unseal(self) -> T {
self.into_inner()
}
}
impl<T> Default for Sealed<T>
where
T: Sealable + Default,
{
fn default() -> Self {
T::default().seal_slow()
}
}
#[cfg(feature = "arbitrary")]
impl<'a, T> arbitrary::Arbitrary<'a> for Sealed<T>
where
T: for<'b> arbitrary::Arbitrary<'b> + Sealable,
{
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(T::arbitrary(u)?.seal_slow())
}
}
pub trait Sealable: Sized {
fn hash_slow(&self) -> B256;
fn seal_slow(self) -> Sealed<Self> {
Sealed::new(self)
}
fn seal_ref_slow(&self) -> Sealed<&Self> {
Sealed::new_ref(self)
}
fn seal_unchecked(self, seal: B256) -> Sealed<Self> {
Sealed::new_unchecked(self, seal)
}
fn seal_ref_unchecked(&self, seal: B256) -> Sealed<&Self> {
Sealed::new_unchecked(self, seal)
}
}