-
Notifications
You must be signed in to change notification settings - Fork 506
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
} | ||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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()); | ||
|
@@ -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 | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
|
@@ -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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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, | ||
{ | ||
|
@@ -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, | ||
{ | ||
|
@@ -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, | ||
{ | ||
|
@@ -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, | ||
{ | ||
|
@@ -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), | ||
|
@@ -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), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?