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

Allow encode BufMut arg to be unsized #1080

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions prost-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn try_message(input: TokenStream) -> Result<TokenStream, Error> {
let expanded = quote! {
impl #impl_generics ::prost::Message for #ident #ty_generics #where_clause {
#[allow(unused_variables)]
fn encode_raw(&self, buf: &mut impl ::prost::bytes::BufMut) {
fn encode_raw(&self, buf: &mut (impl ::prost::bytes::BufMut + ?Sized)) {
#(#encode)*
}

Expand Down Expand Up @@ -462,7 +462,7 @@ fn try_oneof(input: TokenStream) -> Result<TokenStream, Error> {
let expanded = quote! {
impl #impl_generics #ident #ty_generics #where_clause {
/// Encodes the message to a buffer.
pub fn encode(&self, buf: &mut impl ::prost::bytes::BufMut) {
pub fn encode(&self, buf: &mut (impl ::prost::bytes::BufMut + ?Sized)) {
match *self {
#(#encode,)*
}
Expand Down
40 changes: 20 additions & 20 deletions prost/src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::Message;
/// Encodes an integer value into LEB128 variable length format, and writes it to the buffer.
/// The buffer must have enough remaining space (maximum 10 bytes).
#[inline]
pub fn encode_varint(mut value: u64, buf: &mut impl BufMut) {
pub fn encode_varint(mut value: u64, buf: &mut (impl BufMut + ?Sized)) {
// Varints are never more than 10 bytes
for _ in 0..10 {
if value < 0x80 {
Expand Down Expand Up @@ -292,7 +292,7 @@ impl TryFrom<u64> for WireType {
/// Encodes a Protobuf field key, which consists of a wire type designator and
/// the field tag.
#[inline]
pub fn encode_key(tag: u32, wire_type: WireType, buf: &mut impl BufMut) {
pub fn encode_key(tag: u32, wire_type: WireType, buf: &mut (impl BufMut + ?Sized)) {
debug_assert!((MIN_TAG..=MAX_TAG).contains(&tag));
let key = (tag << 3) | wire_type as u32;
encode_varint(u64::from(key), buf);
Expand Down Expand Up @@ -403,7 +403,7 @@ pub fn skip_field(
/// Helper macro which emits an `encode_repeated` function for the type.
macro_rules! encode_repeated {
($ty:ty) => {
pub fn encode_repeated(tag: u32, values: &[$ty], buf: &mut impl BufMut) {
pub fn encode_repeated(tag: u32, values: &[$ty], buf: &mut (impl BufMut + ?Sized)) {
for value in values {
encode(tag, value, buf);
}
Expand Down Expand Up @@ -462,7 +462,7 @@ macro_rules! varint {
pub mod $proto_ty {
use crate::encoding::*;

pub fn encode(tag: u32, $to_uint64_value: &$ty, buf: &mut impl BufMut) {
pub fn encode(tag: u32, $to_uint64_value: &$ty, buf: &mut (impl BufMut + ?Sized)) {
encode_key(tag, WireType::Varint, buf);
encode_varint($to_uint64, buf);
}
Expand All @@ -476,7 +476,7 @@ macro_rules! varint {

encode_repeated!($ty);

pub fn encode_packed(tag: u32, values: &[$ty], buf: &mut impl BufMut) {
pub fn encode_packed(tag: u32, values: &[$ty], buf: &mut (impl BufMut + ?Sized)) {
if values.is_empty() { return; }

encode_key(tag, WireType::LengthDelimited, buf);
Expand Down Expand Up @@ -585,7 +585,7 @@ macro_rules! fixed_width {
pub mod $proto_ty {
use crate::encoding::*;

pub fn encode(tag: u32, value: &$ty, buf: &mut impl BufMut) {
pub fn encode(tag: u32, value: &$ty, buf: &mut (impl BufMut + ?Sized)) {
encode_key(tag, $wire_type, buf);
buf.$put(*value);
}
Expand All @@ -606,7 +606,7 @@ macro_rules! fixed_width {

encode_repeated!($ty);

pub fn encode_packed(tag: u32, values: &[$ty], buf: &mut impl BufMut) {
pub fn encode_packed(tag: u32, values: &[$ty], buf: &mut (impl BufMut + ?Sized)) {
if values.is_empty() {
return;
}
Expand Down Expand Up @@ -758,7 +758,7 @@ macro_rules! length_delimited {
pub mod string {
use super::*;

pub fn encode(tag: u32, value: &String, buf: &mut impl BufMut) {
pub fn encode(tag: u32, value: &String, buf: &mut (impl BufMut + ?Sized)) {
encode_key(tag, WireType::LengthDelimited, buf);
encode_varint(value.len() as u64, buf);
buf.put_slice(value.as_bytes());
Expand Down Expand Up @@ -844,7 +844,7 @@ mod sealed {
fn replace_with(&mut self, buf: impl Buf);

/// Appends this buffer to the (contents of) other buffer.
fn append_to(&self, buf: &mut impl BufMut);
fn append_to(&self, buf: &mut (impl BufMut + ?Sized));

fn is_empty(&self) -> bool {
self.len() == 0
Expand All @@ -863,8 +863,8 @@ impl sealed::BytesAdapter for Bytes {
*self = buf.copy_to_bytes(buf.remaining());
}

fn append_to(&self, buf: &mut impl BufMut) {
buf.put(self.clone())
fn append_to(&self, buf: &mut (impl BufMut + ?Sized)) {
buf.put_slice(self)
Copy link
Author

@dspyz-matician dspyz-matician Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? Why is it the way it is currently?

}
}

Expand All @@ -881,15 +881,15 @@ impl sealed::BytesAdapter for Vec<u8> {
self.put(buf);
}

fn append_to(&self, buf: &mut impl BufMut) {
buf.put(self.as_slice())
fn append_to(&self, buf: &mut (impl BufMut + ?Sized)) {
buf.put_slice(self.as_slice())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too

}
}

pub mod bytes {
use super::*;

pub fn encode(tag: u32, value: &impl BytesAdapter, buf: &mut impl BufMut) {
pub fn encode(tag: u32, value: &impl BytesAdapter, buf: &mut (impl BufMut + ?Sized)) {
encode_key(tag, WireType::LengthDelimited, buf);
encode_varint(value.len() as u64, buf);
value.append_to(buf);
Expand Down Expand Up @@ -986,7 +986,7 @@ pub mod bytes {
pub mod message {
use super::*;

pub fn encode<M>(tag: u32, msg: &M, buf: &mut impl BufMut)
pub fn encode<M>(tag: u32, msg: &M, buf: &mut (impl BufMut + ?Sized))
where
M: Message,
{
Expand Down Expand Up @@ -1018,7 +1018,7 @@ pub mod message {
)
}

pub fn encode_repeated<M>(tag: u32, messages: &[M], buf: &mut impl BufMut)
pub fn encode_repeated<M>(tag: u32, messages: &[M], buf: &mut (impl BufMut + ?Sized))
where
M: Message,
{
Expand Down Expand Up @@ -1069,7 +1069,7 @@ pub mod message {
pub mod group {
use super::*;

pub fn encode<M>(tag: u32, msg: &M, buf: &mut impl BufMut)
pub fn encode<M>(tag: u32, msg: &M, buf: &mut (impl BufMut + ?Sized))
where
M: Message,
{
Expand Down Expand Up @@ -1104,7 +1104,7 @@ pub mod group {
}
}

pub fn encode_repeated<M>(tag: u32, messages: &[M], buf: &mut impl BufMut)
pub fn encode_repeated<M>(tag: u32, messages: &[M], buf: &mut (impl BufMut + ?Sized))
where
M: Message,
{
Expand Down Expand Up @@ -1166,7 +1166,7 @@ macro_rules! map {
) where
K: Default + Eq + Hash + Ord,
V: Default + PartialEq,
B: BufMut,
B: BufMut + ?Sized,
KE: Fn(u32, &K, &mut B),
KL: Fn(u32, &K) -> usize,
VE: Fn(u32, &V, &mut B),
Expand Down Expand Up @@ -1234,7 +1234,7 @@ macro_rules! map {
) where
K: Default + Eq + Hash + Ord,
V: PartialEq,
B: BufMut,
B: BufMut + ?Sized,
KE: Fn(u32, &K, &mut B),
KL: Fn(u32, &K) -> usize,
VE: Fn(u32, &V, &mut B),
Expand Down
2 changes: 1 addition & 1 deletion prost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const RECURSION_LIMIT: u32 = 100;
///
/// An error will be returned if the buffer does not have sufficient capacity to encode the
/// delimiter.
pub fn encode_length_delimiter(length: usize, buf: &mut impl BufMut) -> Result<(), EncodeError> {
pub fn encode_length_delimiter(length: usize, buf: &mut (impl BufMut + ?Sized)) -> Result<(), EncodeError> {
let length = length as u64;
let required = encoded_len_varint(length);
let remaining = buf.remaining_mut();
Expand Down
8 changes: 4 additions & 4 deletions prost/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait Message: Debug + Send + Sync {
///
/// Meant to be used only by `Message` implementations.
#[doc(hidden)]
fn encode_raw(&self, buf: &mut impl BufMut)
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized))
where
Self: Sized;

Expand All @@ -45,7 +45,7 @@ pub trait Message: Debug + Send + Sync {
/// Encodes the message to a buffer.
///
/// An error will be returned if the buffer does not have sufficient capacity.
fn encode(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>
fn encode(&self, buf: &mut (impl BufMut + ?Sized)) -> Result<(), EncodeError>
where
Self: Sized,
{
Expand Down Expand Up @@ -73,7 +73,7 @@ pub trait Message: Debug + Send + Sync {
/// Encodes the message with a length-delimiter to a buffer.
///
/// An error will be returned if the buffer does not have sufficient capacity.
fn encode_length_delimited(&self, buf: &mut impl BufMut) -> Result<(), EncodeError>
fn encode_length_delimited(&self, buf: &mut (impl BufMut + ?Sized)) -> Result<(), EncodeError>
where
Self: Sized,
{
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<M> Message for Box<M>
where
M: Message,
{
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
(**self).encode_raw(buf)
}
fn merge_field(
Expand Down
22 changes: 11 additions & 11 deletions prost/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{

/// `google.protobuf.BoolValue`
impl Message for bool {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if *self {
bool::encode(1, self, buf)
}
Expand Down Expand Up @@ -54,7 +54,7 @@ impl Message for bool {

/// `google.protobuf.UInt32Value`
impl Message for u32 {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if *self != 0 {
uint32::encode(1, self, buf)
}
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Message for u32 {

/// `google.protobuf.UInt64Value`
impl Message for u64 {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if *self != 0 {
uint64::encode(1, self, buf)
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Message for u64 {

/// `google.protobuf.Int32Value`
impl Message for i32 {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if *self != 0 {
int32::encode(1, self, buf)
}
Expand Down Expand Up @@ -150,7 +150,7 @@ impl Message for i32 {

/// `google.protobuf.Int64Value`
impl Message for i64 {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if *self != 0 {
int64::encode(1, self, buf)
}
Expand Down Expand Up @@ -182,7 +182,7 @@ impl Message for i64 {

/// `google.protobuf.FloatValue`
impl Message for f32 {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if *self != 0.0 {
float::encode(1, self, buf)
}
Expand Down Expand Up @@ -214,7 +214,7 @@ impl Message for f32 {

/// `google.protobuf.DoubleValue`
impl Message for f64 {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if *self != 0.0 {
double::encode(1, self, buf)
}
Expand Down Expand Up @@ -246,7 +246,7 @@ impl Message for f64 {

/// `google.protobuf.StringValue`
impl Message for String {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if !self.is_empty() {
string::encode(1, self, buf)
}
Expand Down Expand Up @@ -278,7 +278,7 @@ impl Message for String {

/// `google.protobuf.BytesValue`
impl Message for Vec<u8> {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if !self.is_empty() {
bytes::encode(1, self, buf)
}
Expand Down Expand Up @@ -310,7 +310,7 @@ impl Message for Vec<u8> {

/// `google.protobuf.BytesValue`
impl Message for Bytes {
fn encode_raw(&self, buf: &mut impl BufMut) {
fn encode_raw(&self, buf: &mut (impl BufMut + ?Sized)) {
if !self.is_empty() {
bytes::encode(1, self, buf)
}
Expand Down Expand Up @@ -342,7 +342,7 @@ impl Message for Bytes {

/// `google.protobuf.Empty`
impl Message for () {
fn encode_raw(&self, _buf: &mut impl BufMut) {}
fn encode_raw(&self, _buf: &mut (impl BufMut + ?Sized)) {}
fn merge_field(
&mut self,
tag: u32,
Expand Down
Loading