Skip to content

digest: add CoreProxy::compose/decompose methods #1898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions digest/src/block_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@ pub enum TruncSide {
Right,
}

/// A proxy trait to a core type.
/// A proxy trait to the core block-level type.
pub trait CoreProxy {
/// Wrapped block-level type.
type Core;
/// Core block-level type.
type Core: BufferKindUser;

/// Create `Self` from core and buffer.
fn compose(core: Self::Core, buffer: Buffer<Self::Core>) -> Self;
/// Decompose `self` into core and buffer.
fn decompose(self) -> (Self::Core, Buffer<Self::Core>);
}
7 changes: 7 additions & 0 deletions digest/src/buffer_macros/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ macro_rules! buffer_fixed {
) => {
impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::block_api::CoreProxy for $name$(< $( $lt ),+ >)? {
type Core = $core_ty;
fn compose(core: Self::Core, buffer: $crate::block_api::Buffer<Self::Core>) -> Self {
Self { core, buffer }
}
fn decompose(self) -> (Self::Core, $crate::block_api::Buffer<Self::Core>) {
let Self { core, buffer } = self;
(core, buffer)
}
}

$crate::buffer_fixed!(impl_inner: $name$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?($core_ty); $($trait_name)*;);
Expand Down
7 changes: 7 additions & 0 deletions digest/src/buffer_macros/variable_ct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ macro_rules! buffer_ct_variable {
$out_size: $crate::array::ArraySize + $crate::typenum::IsLessOrEqual<$max_size, Output = $crate::typenum::True>,
{
type Core = $crate::block_api::CtOutWrapper<$core_ty, $out_size>;
fn compose(core: Self::Core, buffer: $crate::block_api::Buffer<Self::Core>) -> Self {
Self { core, buffer }
}
fn decompose(self) -> (Self::Core, $crate::block_api::Buffer<Self::Core>) {
let Self { core, buffer } = self;
(core, buffer)
}
}

impl<$out_size> $crate::Update for $name<$out_size>
Expand Down
9 changes: 8 additions & 1 deletion digest/src/buffer_macros/xof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ macro_rules! buffer_xof {
) => {
$crate::buffer_xof!(
impl_inner: $name($core_ty);
Debug Clone BlockSizeUser CoreProxy
Debug Clone BlockSizeUser
Copy link
Member Author

@newpavlov newpavlov Jun 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that XOF readers no longer implement CoreProxy since CoreProxy::Core is bounded by BufferKindUser. We could make the buffer another associated type, but I don't think it's worth the trouble.

If necessary, in future we may add inherent methods to the XOF readers.

$($trait_name)*;);
};

Expand Down Expand Up @@ -194,6 +194,13 @@ macro_rules! buffer_xof {
) => {
impl $crate::block_api::CoreProxy for $name {
type Core = $core_ty;
fn compose(core: Self::Core, buffer: $crate::block_api::Buffer<Self::Core>) -> Self {
Self { core, buffer }
}
fn decompose(self) -> (Self::Core, $crate::block_api::Buffer<Self::Core>) {
let Self { core, buffer } = self;
(core, buffer)
}
}

$crate::buffer_xof!(impl_inner: $name($core_ty); $($trait_name)*;);
Expand Down