Skip to content

Commit

Permalink
Merge pull request #18 from noislabs/append_repeated_message-AsRef
Browse files Browse the repository at this point in the history
Let append_repeated_message accept both &Anybuf and Anybuf as elements
  • Loading branch information
webmaster128 committed Jan 20, 2024
2 parents 6ef1626 + dc716bf commit b34968f
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/anybuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ impl Anybuf {
///
/// Use this instead of multiple [`Anybuf::append_message`] to ensure empty values are not lost.
///
/// The generic type `M` is the type of a single element in the input slice. This
/// is typically `&Anybuf` or `Anybuf`.
///
/// ## Example
///
/// ```
Expand All @@ -439,9 +442,13 @@ impl Anybuf {
/// ])
/// .into_vec();
/// ```
pub fn append_repeated_message(mut self, field_number: u32, messages: &[&Anybuf]) -> Self {
pub fn append_repeated_message<M: AsRef<Anybuf>>(
mut self,
field_number: u32,
messages: &[M],
) -> Self {
for message in messages {
let data = message.as_bytes();
let data = message.as_ref().as_bytes();
// tag
self.append_tag(field_number, WireType::Len);
// length
Expand Down Expand Up @@ -476,6 +483,13 @@ impl Anybuf {
}
}

impl AsRef<Anybuf> for Anybuf {
#[inline]
fn as_ref(&self) -> &Anybuf {
self
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -929,7 +943,17 @@ mod tests {
// echo "id: \"no messages\"; messages: []" | protoc --encode=Collection *.proto | xxd -p -c 9999
let data = Anybuf::new()
.append_string(1, "no messages")
.append_repeated_message(11, &[]);
.append_repeated_message::<&Anybuf>(11, &[]);
assert_eq!(data.into_vec(), hex!("0a0b6e6f206d65737361676573"));

// Works for owned Anybuf instances. We can't easily create a Vec<&Anybuf> because the references outlive the instances.
let owned: Vec<Anybuf> = vec![
Anybuf::new().append_uint32(1, 1),
Anybuf::new().append_uint32(1, 2),
Anybuf::new().append_uint32(1, 3),
];
// echo "messages: [{ number: 1}, { number: 2}, { number: 3}]" | protoc --encode=Collection *.proto | xxd -p -c 9999
let data = Anybuf::new().append_repeated_message(11, &owned);
assert_eq!(data.into_vec(), hex!("5a0208015a0208025a020803"));
}
}

0 comments on commit b34968f

Please sign in to comment.