#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::fmt::Debug;
use bytes::{Buf, BufMut};
use crate::encoding::varint::{encode_varint, encoded_len_varint};
use crate::encoding::wire_type::WireType;
use crate::encoding::{decode_key, message, DecodeContext};
use crate::DecodeError;
use crate::EncodeError;
pub trait Message: Debug + Send + Sync {
#[doc(hidden)]
fn encode_raw(&self, buf: &mut impl BufMut)
where
Self: Sized;
#[doc(hidden)]
fn merge_field(
&mut self,
tag: u32,
wire_type: WireType,
buf: &mut impl Buf,
ctx: DecodeContext,
) -> Result<(), DecodeError>
where
Self: Sized;
fn encoded_len(&self) -> usize;
fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>
where
Self: Sized,
{
let required = self.encoded_len();
let remaining = buf.remaining_mut();
if required > remaining {
return Err(EncodeError::new(required, remaining));
}
self.encode_raw(buf);
Ok(())
}
fn encode_to_vec(&self) -> Vec<u8>
where
Self: Sized,
{
let mut buf = Vec::with_capacity(self.encoded_len());
self.encode_raw(&mut buf);
buf
}
fn encode_length_delimited(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>
where
Self: Sized,
{
let len = self.encoded_len();
let required = len + encoded_len_varint(len as u64);
let remaining = buf.remaining_mut();
if required > remaining {
return Err(EncodeError::new(required, remaining));
}
encode_varint(len as u64, buf);
self.encode_raw(buf);
Ok(())
}
fn encode_length_delimited_to_vec(&self) -> Vec<u8>
where
Self: Sized,
{
let len = self.encoded_len();
let mut buf = Vec::with_capacity(len + encoded_len_varint(len as u64));
encode_varint(len as u64, &mut buf);
self.encode_raw(&mut buf);
buf
}
fn decode(mut buf: impl Buf) -> Result<Self, DecodeError>
where
Self: Default,
{
let mut message = Self::default();
Self::merge(&mut message, &mut buf).map(|_| message)
}
fn decode_length_delimited(buf: impl Buf) -> Result<Self, DecodeError>
where
Self: Default,
{
let mut message = Self::default();
message.merge_length_delimited(buf)?;
Ok(message)
}
fn merge(&mut self, mut buf: impl Buf) -> Result<(), DecodeError>
where
Self: Sized,
{
let ctx = DecodeContext::default();
while buf.has_remaining() {
let (tag, wire_type) = decode_key(&mut buf)?;
self.merge_field(tag, wire_type, &mut buf, ctx.clone())?;
}
Ok(())
}
fn merge_length_delimited(&mut self, mut buf: impl Buf) -> Result<(), DecodeError>
where
Self: Sized,
{
message::merge(
WireType::LengthDelimited,
self,
&mut buf,
DecodeContext::default(),
)
}
fn clear(&mut self);
}
impl<M> Message for Box<M>
where
M: Message,
{
fn encode_raw(&self, buf: &mut impl BufMut) {
(**self).encode_raw(buf)
}
fn merge_field(
&mut self,
tag: u32,
wire_type: WireType,
buf: &mut impl Buf,
ctx: DecodeContext,
) -> Result<(), DecodeError> {
(**self).merge_field(tag, wire_type, buf, ctx)
}
fn encoded_len(&self) -> usize {
(**self).encoded_len()
}
fn clear(&mut self) {
(**self).clear()
}
}
#[cfg(test)]
mod tests {
use super::*;
const _MESSAGE_IS_OBJECT_SAFE: Option<&dyn Message> = None;
}