Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use internal buffer's Buf impl for codec buffers #1695

Merged
merged 2 commits into from
May 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion tonic/src/codec/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bytes::buf::UninitSlice;
use bytes::{Buf, BufMut, BytesMut};
use bytes::{Buf, BufMut, Bytes, BytesMut};

/// A specialized buffer to decode gRPC messages from.
#[derive(Debug)]
Expand Down Expand Up @@ -43,6 +43,13 @@ impl Buf for DecodeBuf<'_> {
self.buf.advance(cnt);
self.len -= cnt;
}

#[inline]
fn copy_to_bytes(&mut self, len: usize) -> Bytes {
assert!(len <= self.len);
self.len -= len;
self.buf.copy_to_bytes(len)
}
}

impl<'a> EncodeBuf<'a> {
Expand Down Expand Up @@ -78,6 +85,24 @@ unsafe impl BufMut for EncodeBuf<'_> {
fn chunk_mut(&mut self) -> &mut UninitSlice {
self.buf.chunk_mut()
}

#[inline]
fn put<T: Buf>(&mut self, src: T)
where
Self: Sized,
{
self.buf.put(src)
}

#[inline]
fn put_slice(&mut self, src: &[u8]) {
self.buf.put_slice(src)
}

#[inline]
fn put_bytes(&mut self, val: u8, cnt: usize) {
self.buf.put_bytes(val, cnt);
}
}

#[cfg(test)]
Expand Down