rust/bt-common: Add Decodable::decode_multiple Decodes multiple decodable items that are concurrently included in a buffer. Change-Id: I29cb6f1ef7ba885e5733d5c231061c66103ea047 Reviewed-on: https://bluetooth-review.git.corp.google.com/c/bluetooth/+/1721 Reviewed-by: Dayeong Lee <dayeonglee@google.com> Reviewed-by: Ani Ramakrishnan <aniramakri@google.com>
diff --git a/rust/bt-common/src/packet_encoding.rs b/rust/bt-common/src/packet_encoding.rs index 2516dcb..9ff05d4 100644 --- a/rust/bt-common/src/packet_encoding.rs +++ b/rust/bt-common/src/packet_encoding.rs
@@ -12,6 +12,29 @@ /// Decodes into a new object with the number of bytes that were decoded, or /// returns an error. fn decode(buf: &[u8]) -> ::core::result::Result<(Self, usize), Self::Error>; + + /// Tries to decode a collection of a decodable item from a buffer. + /// If any item fails to decode, fails with an Error and the previously + /// decoded items. + /// Will only decode up to max items. If None, will decode the entire + /// buffer. + fn decode_multiple( + buf: &[u8], + max: Option<usize>, + ) -> ::core::result::Result<(Vec<Self>, usize), (Vec<Self>, usize, Self::Error)> { + let mut idx = 0; + let mut result = Vec::new(); + while idx < buf.len() && Some(result.len()) != max { + match Self::decode(&buf[idx..]) { + Err(e) => return Err((result, idx, e)), + Ok((item, consumed)) => { + result.push(item); + idx += consumed; + } + } + } + Ok((result, idx)) + } } /// An encodable type can write itself into a byte buffer.