-
Notifications
You must be signed in to change notification settings - Fork 469
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
feat: encode string and bytes slices #979
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -794,13 +794,15 @@ macro_rules! length_delimited { | |
pub mod string { | ||
use super::*; | ||
|
||
pub fn encode<B>(tag: u32, value: &String, buf: &mut B) | ||
pub fn encode<S, B>(tag: u32, value: &S, buf: &mut B) | ||
where | ||
S: AsRef<str>, | ||
B: BufMut, | ||
{ | ||
let slice = value.as_ref(); | ||
encode_key(tag, WireType::LengthDelimited, buf); | ||
encode_varint(value.len() as u64, buf); | ||
buf.put_slice(value.as_bytes()); | ||
encode_varint(slice.len() as u64, buf); | ||
buf.put_slice(slice.as_bytes()); | ||
} | ||
pub fn merge<B>( | ||
wire_type: WireType, | ||
|
@@ -876,21 +878,16 @@ pub mod string { | |
pub trait BytesAdapter: sealed::BytesAdapter {} | ||
|
||
mod sealed { | ||
use super::{Buf, BufMut}; | ||
use super::Buf; | ||
|
||
pub trait BytesAdapter: Default + Sized + 'static { | ||
pub trait BytesAdapter: Default + Sized + AsRef<[u8]> + 'static { | ||
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. BytesAdapter is not used anymore for |
||
fn len(&self) -> usize; | ||
|
||
/// Replace contents of this buffer with the contents of another buffer. | ||
fn replace_with<B>(&mut self, buf: B) | ||
where | ||
B: Buf; | ||
|
||
/// Appends this buffer to the (contents of) other buffer. | ||
fn append_to<B>(&self, buf: &mut B) | ||
where | ||
B: BufMut; | ||
|
||
fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
|
@@ -910,13 +907,6 @@ impl sealed::BytesAdapter for Bytes { | |
{ | ||
*self = buf.copy_to_bytes(buf.remaining()); | ||
} | ||
|
||
fn append_to<B>(&self, buf: &mut B) | ||
where | ||
B: BufMut, | ||
{ | ||
buf.put(self.clone()) | ||
} | ||
} | ||
|
||
impl BytesAdapter for Vec<u8> {} | ||
|
@@ -934,26 +924,20 @@ impl sealed::BytesAdapter for Vec<u8> { | |
self.reserve(buf.remaining()); | ||
self.put(buf); | ||
} | ||
|
||
fn append_to<B>(&self, buf: &mut B) | ||
where | ||
B: BufMut, | ||
{ | ||
buf.put(self.as_slice()) | ||
} | ||
} | ||
|
||
pub mod bytes { | ||
use super::*; | ||
|
||
pub fn encode<A, B>(tag: u32, value: &A, buf: &mut B) | ||
where | ||
A: BytesAdapter, | ||
A: AsRef<[u8]>, | ||
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. I would like to see tests to guarantee And you need to add tests for your new use case, to make sure it is not broken in the future. |
||
B: BufMut, | ||
{ | ||
let slice = value.as_ref(); | ||
encode_key(tag, WireType::LengthDelimited, buf); | ||
encode_varint(value.len() as u64, buf); | ||
value.append_to(buf); | ||
encode_varint(slice.len() as u64, buf); | ||
buf.put_slice(slice); | ||
} | ||
|
||
pub fn merge<A, B>( | ||
|
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.
I would like to see tests to guarantee
String
can still be used on this function.And you need to add tests for your new use case, to make sure it is not broken in the future.