proto_rs 0.11.24

Rust-first gRPC macros collection for .proto/protobufs managment and more
use alloc::collections::VecDeque;

use bytes::Buf;
use bytes::BufMut;
use bytes::Bytes;

pub trait BytesAdapterEncode {
    fn len(&self) -> usize;
    fn append_to(&self, buf: &mut impl BufMut);
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

pub trait BytesAdapterDecode: BytesAdapterEncode {
    fn replace_with(&mut self, buf: impl Buf);
}

impl BytesAdapterEncode for Bytes {
    fn len(&self) -> usize {
        Buf::remaining(self)
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        buf.put_slice(self.as_ref());
    }
}

impl BytesAdapterDecode for Bytes {
    fn replace_with(&mut self, mut buf: impl Buf) {
        *self = buf.copy_to_bytes(buf.remaining());
    }
}

impl BytesAdapterEncode for &mut [u8] {
    #[inline]
    fn len(&self) -> usize {
        (**self).len()
    }

    #[inline]
    fn append_to(&self, buf: &mut impl BufMut) {
        buf.put_slice(self);
    }
}

impl BytesAdapterDecode for &mut [u8] {
    #[inline]
    fn replace_with(&mut self, mut buf: impl Buf) {
        let n = buf.remaining();
        assert!(n <= self.len(), "replace_with overflow: src={} dst={}", n, self.len());
        // Copy in as chunks to avoid needing `Buf::copy_to_slice` on older `bytes` versions.
        let mut written = 0usize;
        while buf.has_remaining() {
            let chunk = buf.chunk();
            let m = chunk.len();
            self[written..written + m].copy_from_slice(chunk);
            written += m;
            buf.advance(m);
        }
    }
}
impl BytesAdapterEncode for Vec<u8> {
    fn len(&self) -> usize {
        Vec::len(self)
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        buf.put_slice(self.as_ref());
    }
}

impl BytesAdapterDecode for Vec<u8> {
    fn replace_with(&mut self, buf: impl Buf) {
        self.clear();
        self.reserve(buf.remaining());
        self.put(buf);
    }
}

impl BytesAdapterEncode for VecDeque<u8> {
    fn len(&self) -> usize {
        VecDeque::len(self)
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        let (left, right) = self.as_slices();
        buf.put_slice(left);
        buf.put_slice(right);
    }
}

impl BytesAdapterDecode for VecDeque<u8> {
    fn replace_with(&mut self, mut buf: impl Buf) {
        self.clear();
        self.reserve(buf.remaining());

        while buf.has_remaining() {
            let chunk = buf.chunk();
            self.extend(chunk);
            let len = chunk.len();
            buf.advance(len);
        }
    }
}

impl BytesAdapterEncode for &VecDeque<u8> {
    fn len(&self) -> usize {
        VecDeque::len(self)
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        let (left, right) = self.as_slices();
        buf.put_slice(left);
        buf.put_slice(right);
    }
}

impl BytesAdapterEncode for &Vec<u8> {
    fn len(&self) -> usize {
        Vec::len(self)
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        buf.put_slice(self);
    }
}

impl BytesAdapterEncode for &Bytes {
    fn len(&self) -> usize {
        Buf::remaining(*self)
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        buf.put_slice(self.as_ref());
    }
}

impl BytesAdapterEncode for &[u8] {
    fn len(&self) -> usize {
        (*self).len()
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        buf.put_slice(self);
    }
}

impl<const N: usize> BytesAdapterEncode for &[u8; N] {
    fn len(&self) -> usize {
        N
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        buf.put_slice(*self);
    }
}

impl<const N: usize> BytesAdapterEncode for [u8; N] {
    fn len(&self) -> usize {
        N
    }

    fn append_to(&self, buf: &mut impl BufMut) {
        buf.put_slice(&self[..]);
    }
}