macro_rules! decoder_newtype {
(
$(#[$($struct_attr:tt)*])*
$vis:vis struct $name:ident($decoder:ty);
fn end($result_name:ident: $result_ty:ty) -> Result<$output:ty, $err:ident> $end_impl:block
) => {
crate::_decoder_newtype_internal! {
$(#[$($struct_attr)*])*
$vis struct $name($decoder);
(err: <$decoder as encoding::Decoder>::Error) -> $err { $err(err) }
($result_name: $result_ty) -> Result<$output, $err> $end_impl
}
};
(
$(#[$($struct_attr:tt)*])*
$vis:vis struct $name:ident($decoder:ty);
$(#[$($new_attr:tt)*])*
$new_vis:vis $(const $($const:block)?)? fn new() -> Self $new_impl:block
fn end($result_name:ident: $result_ty:ty) -> Result<$output:ty, $err:ident> $end_impl:block
) => {
crate::_decoder_newtype_internal! {
$(#[$($struct_attr)*])*
$vis struct $name($decoder);
(err: <$decoder as encoding::Decoder>::Error) -> $err { $err(err) }
($result_name: $result_ty) -> Result<$output, $err> $end_impl
$(#[$($new_attr)*])*
$new_vis $(const $($const)?)? fn new() -> Self $new_impl
}
};
(
$(#[$($struct_attr:tt)*])*
$vis:vis struct $name:ident($decoder:ty);
fn map_push_bytes_err($err_var:ident: $inner_err:ty) -> $err_name:ident $on_err_impl:block
fn end($result_name:ident: $result_ty:ty) -> Result<$output:ty, $err:ident> $end_impl:block
) => {
crate::_decoder_newtype_internal! {
$(#[$($struct_attr)*])*
$vis struct $name($decoder);
($err_var: $inner_err) -> $err_name $on_err_impl
($result_name: $result_ty) -> Result<$output, $err> $end_impl
}
};
(
$(#[$($struct_attr:tt)*])*
$vis:vis struct $name:ident($decoder:ty);
$(#[$($new_attr:tt)*])*
$new_vis:vis $(const $($const:block)?)? fn new() -> Self $new_impl:block
fn map_push_bytes_err($err_var:ident: $inner_err:ty) -> $err_name:ident $on_err_impl:block
fn end($result_name:ident: $result_ty:ty) -> Result<$output:ty, $err:ident> $end_impl:block
) => {
crate::_decoder_newtype_internal! {
$(#[$($struct_attr)*])*
$vis struct $name($decoder);
($err_var: $inner_err) -> $err_name $on_err_impl
($result_name: $result_ty) -> Result<$output, $err> $end_impl
$(#[$($new_attr)*])*
$new_vis $(const $($const)?)? fn new() -> Self $new_impl
}
};
}
pub(crate) use decoder_newtype;
macro_rules! _decoder_newtype_internal {
(
$(#[$($struct_attr:tt)*])*
$vis:vis struct $name:ident($decoder:ty);
($err_var:ident: $inner_err:ty) -> $err_name:ident $on_err_impl:block
($result_name:ident: $result_ty:ty) -> Result<$output:ty, $err:ident> $end_impl:block
$(
$(#[$($new_attr:tt)*])*
$new_vis:vis $(const $($const:block)?)? fn new() -> Self $new_impl:block
)?
) => {
$(#[$($struct_attr)*])*
$vis struct $name($decoder);
$(
impl Default for $name {
#[inline]
fn default() -> Self { Self::new() }
}
impl $name {
$(#[$($new_attr)*])*
#[inline]
$new_vis $(const $($const)?)? fn new() -> Self $new_impl
}
)?
impl $name {
#[inline]
fn push_bytes_map_err($err_var: $inner_err) -> $err $on_err_impl
}
impl encoding::Decoder for $name {
type Output = $output;
type Error = $err;
#[inline]
fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<encoding::DecoderStatus, Self::Error> {
self.0.push_bytes(bytes).map_err(Self::push_bytes_map_err)
}
#[inline]
fn end(self) -> Result<Self::Output, Self::Error> {
let end = |$result_name: $result_ty| $end_impl;
end(self.0.end())
}
#[inline]
fn read_limit(&self) -> usize { self.0.read_limit() }
}
};
}
pub(crate) use _decoder_newtype_internal;