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

feat: encode string and bytes slices #979

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 11 additions & 27 deletions src/encoding.rs
Expand Up @@ -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)
Copy link
Collaborator

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.

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,
Expand Down Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

BytesAdapter is not used anymore for encode, but it seems to still be used for encoded_len. Will this mismatch cause issues for other people?

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
}
Expand All @@ -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> {}
Expand All @@ -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]>,
Copy link
Collaborator

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 impl BytesAdapter, Bytes and Vec<u8> 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.

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>(
Expand Down