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: add str encoding helper #978

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
14 changes: 10 additions & 4 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use core::cmp::min;
use core::convert::TryFrom;
use core::convert::{AsRef, TryFrom};
use core::mem;
use core::str;
use core::u32;
Expand Down Expand Up @@ -794,14 +794,20 @@ macro_rules! length_delimited {
pub mod string {
use super::*;

pub fn encode_ref(tag: u32, value: impl AsRef<str>, buf: &mut impl BufMut) {
let str = value.as_ref();
encode_key(tag, WireType::LengthDelimited, buf);
encode_varint(str.len() as u64, buf);
buf.put_slice(str.as_bytes());
}

pub fn encode<B>(tag: u32, value: &String, buf: &mut B)
where
B: BufMut,
{
encode_key(tag, WireType::LengthDelimited, buf);
encode_varint(value.len() as u64, buf);
buf.put_slice(value.as_bytes());
encode_ref(tag, value, buf)
}

pub fn merge<B>(
wire_type: WireType,
value: &mut String,
Expand Down
Loading