use bytes::{Buf, BufMut};
use crate::error::DecodeError;
use crate::message_field::DefaultInstance;
#[cfg(any(feature = "json", feature = "text"))]
pub(crate) static REGISTRY_TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct SizedMsg {
pub(crate) reported_size: u32,
}
impl DefaultInstance for SizedMsg {
fn default_instance() -> &'static Self {
static INST: crate::__private::OnceBox<SizedMsg> = crate::__private::OnceBox::new();
INST.get_or_init(|| alloc::boxed::Box::new(SizedMsg::default()))
}
}
impl crate::Message for SizedMsg {
fn compute_size(&self, _cache: &mut crate::SizeCache) -> u32 {
self.reported_size
}
fn write_to(&self, _cache: &mut crate::SizeCache, _buf: &mut impl crate::EncodeSink) {}
fn merge_field(
&mut self,
tag: crate::encoding::Tag,
buf: &mut impl Buf,
_ctx: crate::DecodeContext<'_>,
) -> Result<(), DecodeError> {
crate::encoding::skip_field(tag, buf)?;
Ok(())
}
fn clear(&mut self) {
*self = Self::default();
}
}
pub(crate) struct NullSink;
impl crate::EncodeSink for NullSink {
fn put_u8(&mut self, _: u8) {}
fn put_slice(&mut self, _: &[u8]) {}
fn put_u32_le(&mut self, _: u32) {}
fn put_u64_le(&mut self, _: u64) {}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub(crate) struct Mismatched {
pub(crate) declared: u32,
pub(crate) written: usize,
}
impl DefaultInstance for Mismatched {
fn default_instance() -> &'static Self {
static INST: crate::__private::OnceBox<Mismatched> = crate::__private::OnceBox::new();
INST.get_or_init(|| alloc::boxed::Box::new(Mismatched::default()))
}
}
impl crate::Message for Mismatched {
fn compute_size(&self, _cache: &mut crate::SizeCache) -> u32 {
self.declared
}
fn write_to(&self, _cache: &mut crate::SizeCache, buf: &mut impl crate::EncodeSink) {
for _ in 0..self.written {
buf.put_u8(0x55);
}
}
fn merge_field(
&mut self,
tag: crate::encoding::Tag,
buf: &mut impl Buf,
_ctx: crate::DecodeContext<'_>,
) -> Result<(), DecodeError> {
crate::encoding::skip_field(tag, buf)?;
Ok(())
}
fn clear(&mut self) {
*self = Self::default();
}
}
#[derive(Default)]
pub(crate) struct Probe {
pub(crate) inner: Vec<u8>,
pub(crate) chunk_mut_calls: usize,
pub(crate) advance_mut_calls: usize,
pub(crate) put_slice_calls: usize,
pub(crate) chunk_limit: Option<usize>,
}
unsafe impl BufMut for Probe {
fn remaining_mut(&self) -> usize {
self.inner.remaining_mut()
}
unsafe fn advance_mut(&mut self, cnt: usize) {
self.advance_mut_calls += 1;
unsafe { self.inner.advance_mut(cnt) }
}
fn chunk_mut(&mut self) -> &mut bytes::buf::UninitSlice {
self.chunk_mut_calls += 1;
let limit = self.chunk_limit;
let chunk = self.inner.chunk_mut();
match limit {
Some(limit) if limit < chunk.len() => &mut chunk[..limit],
_ => chunk,
}
}
fn put_slice(&mut self, src: &[u8]) {
self.put_slice_calls += 1;
self.inner.extend_from_slice(src);
}
}