Skip to content

Commit 8534db4

Browse files
authored
digest: add CoreProxy::compose/decompose methods (#1898)
1 parent 81757de commit 8534db4

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

digest/src/block_api.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,13 @@ pub enum TruncSide {
9797
Right,
9898
}
9999

100-
/// A proxy trait to a core type.
100+
/// A proxy trait to the core block-level type.
101101
pub trait CoreProxy {
102-
/// Wrapped block-level type.
103-
type Core;
102+
/// Core block-level type.
103+
type Core: BufferKindUser;
104+
105+
/// Create `Self` from core and buffer.
106+
fn compose(core: Self::Core, buffer: Buffer<Self::Core>) -> Self;
107+
/// Decompose `self` into core and buffer.
108+
fn decompose(self) -> (Self::Core, Buffer<Self::Core>);
104109
}

digest/src/buffer_macros/fixed.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,13 @@ macro_rules! buffer_fixed {
179179
) => {
180180
impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::block_api::CoreProxy for $name$(< $( $lt ),+ >)? {
181181
type Core = $core_ty;
182+
fn compose(core: Self::Core, buffer: $crate::block_api::Buffer<Self::Core>) -> Self {
183+
Self { core, buffer }
184+
}
185+
fn decompose(self) -> (Self::Core, $crate::block_api::Buffer<Self::Core>) {
186+
let Self { core, buffer } = self;
187+
(core, buffer)
188+
}
182189
}
183190

184191
$crate::buffer_fixed!(impl_inner: $name$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?($core_ty); $($trait_name)*;);

digest/src/buffer_macros/variable_ct.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ macro_rules! buffer_ct_variable {
112112
$out_size: $crate::array::ArraySize + $crate::typenum::IsLessOrEqual<$max_size, Output = $crate::typenum::True>,
113113
{
114114
type Core = $crate::block_api::CtOutWrapper<$core_ty, $out_size>;
115+
fn compose(core: Self::Core, buffer: $crate::block_api::Buffer<Self::Core>) -> Self {
116+
Self { core, buffer }
117+
}
118+
fn decompose(self) -> (Self::Core, $crate::block_api::Buffer<Self::Core>) {
119+
let Self { core, buffer } = self;
120+
(core, buffer)
121+
}
115122
}
116123

117124
impl<$out_size> $crate::Update for $name<$out_size>

digest/src/buffer_macros/xof.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ macro_rules! buffer_xof {
8787
) => {
8888
$crate::buffer_xof!(
8989
impl_inner: $name($core_ty);
90-
Debug Clone BlockSizeUser CoreProxy
90+
Debug Clone BlockSizeUser
9191
$($trait_name)*;);
9292
};
9393

@@ -194,6 +194,13 @@ macro_rules! buffer_xof {
194194
) => {
195195
impl $crate::block_api::CoreProxy for $name {
196196
type Core = $core_ty;
197+
fn compose(core: Self::Core, buffer: $crate::block_api::Buffer<Self::Core>) -> Self {
198+
Self { core, buffer }
199+
}
200+
fn decompose(self) -> (Self::Core, $crate::block_api::Buffer<Self::Core>) {
201+
let Self { core, buffer } = self;
202+
(core, buffer)
203+
}
197204
}
198205

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

0 commit comments

Comments
 (0)