#[macro_export]
macro_rules! prost_wireformat {
($wrapper_vis:vis $wrapped_type:ty as $vis:vis $new_type:ident, derive($($derives:path),* $(,)?)) => {
#[derive($($derives),*)]
$wrapper_vis struct $new_type($vis $wrapped_type);
$crate::prost_wireformat!(@impl $wrapped_type, $vis, $new_type);
};
($wrapper_vis:vis $wrapped_type:ty as $vis:vis $new_type:ident) => {
$wrapper_vis struct $new_type($vis $wrapped_type);
$crate::prost_wireformat!(@impl $wrapped_type, $vis, $new_type);
};
(@impl $wrapped_type:ty, $vis:vis, $new_type:ident) => {
impl $new_type {
$vis fn new(inner: $wrapped_type) -> Self {
Self(inner)
}
$vis fn into_inner(self) -> $wrapped_type {
self.0
}
$vis fn inner(&self) -> &$wrapped_type {
&self.0
}
}
impl $crate::prelude::WireFormat for $new_type {
fn byte_size(&self) -> u32 {
std::mem::size_of::<usize>() as u32
+ self.0.encoded_len() as u32
}
fn encode<W: std::io::Write>(
&self,
writer: &mut W,
) -> std::io::Result<()> {
let len = self.0.encoded_len();
len.encode(writer)?;
let mut buf = Vec::with_capacity(len);
self.0
.encode(&mut buf)
.map_err(std::io::Error::other)?;
writer.write_all(&buf)
}
fn decode<R: std::io::Read>(
reader: &mut R,
) -> std::io::Result<Self> {
let len = usize::decode(reader)?;
let mut bytes = vec![0; len];
reader.read_exact(&mut bytes)?;
<$wrapped_type as prost::Message>::decode(bytes.as_slice())
.map($new_type)
.map_err(std::io::Error::other)
}
}
};
}