Skip to content

Commit

Permalink
Encode string and bytes slices
Browse files Browse the repository at this point in the history
  • Loading branch information
guilload committed Feb 16, 2024
1 parent 3cb37f2 commit d61c094
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions src/encoding.rs
Original file line number Diff line number Diff line change
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)
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 {
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]>,
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

0 comments on commit d61c094

Please sign in to comment.