Skip to content

Commit

Permalink
Return Vec from util functions instead of iterator.
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-graj committed Jul 27, 2024
1 parent 0e4410b commit 076b90b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bin-proto/src/types/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ where
{
fn read(read: &mut dyn BitRead, byte_order: ByteOrder, ctx: &mut Ctx) -> Result<Self> {
let elements = util::read_items(N, read, byte_order, ctx)?;
Ok(elements.into_iter().collect::<Vec<T>>().try_into().unwrap())
Ok(elements.try_into().unwrap())
}
}

Expand Down
2 changes: 1 addition & 1 deletion bin-proto/src/types/collections/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macro_rules! impl_list_type {
where T: $crate::ProtocolRead<Ctx> $( + $ty_pred )*
{
fn read(read: &mut dyn $crate::BitRead, byte_order: $crate::ByteOrder, ctx: &mut Ctx) -> $crate::Result<Self> {
Ok($crate::util::read_items_to_eof(read, byte_order, ctx)?.collect())
Ok($crate::util::read_items_to_eof(read, byte_order, ctx)?.into_iter().collect())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion bin-proto/src/types/collections/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ macro_rules! impl_map_type {
byte_order: $crate::ByteOrder,
ctx: &mut Ctx,
) -> $crate::Result<Self> {
Ok($crate::util::read_items_to_eof(read, byte_order, ctx)?.collect())
Ok($crate::util::read_items_to_eof(read, byte_order, ctx)?.into_iter().collect())
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions bin-proto/src/types/string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
util, BitRead, BitWrite, ByteOrder, Error, TaggedRead, UntaggedWrite,
FlexibleArrayMemberRead, Result,
util, BitRead, BitWrite, ByteOrder, Error, FlexibleArrayMemberRead, Result, TaggedRead,
UntaggedWrite,
};

impl<Tag, Ctx> TaggedRead<Tag, Ctx> for String
Expand All @@ -13,13 +13,12 @@ where
ctx: &mut Ctx,
tag: Tag,
) -> Result<Self> {
let bytes: Vec<u8> = util::read_items(
let bytes = util::read_items(
tag.try_into().map_err(|_| Error::TagConvert)?,
read,
byte_order,
ctx,
)?
.collect();
)?;

Ok(String::from_utf8(bytes)?)
}
Expand All @@ -34,7 +33,7 @@ impl<Ctx> UntaggedWrite<Ctx> for String {

impl<Ctx> FlexibleArrayMemberRead<Ctx> for String {
fn read(read: &mut dyn BitRead, byte_order: ByteOrder, ctx: &mut Ctx) -> Result<Self> {
let bytes: Vec<u8> = util::read_items_to_eof(read, byte_order, ctx)?.collect();
let bytes = util::read_items_to_eof(read, byte_order, ctx)?;
Ok(String::from_utf8(bytes)?)
}
}
Expand Down
9 changes: 4 additions & 5 deletions bin-proto/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ pub fn read_items<Ctx, T>(
read: &mut dyn BitRead,
byte_order: ByteOrder,
ctx: &mut Ctx,
) -> Result<impl Iterator<Item = T>>
) -> Result<Vec<T>>
where
T: ProtocolRead<Ctx>,
{
let mut elements = Vec::with_capacity(item_count);

for _ in 0..item_count {
let element = T::read(read, byte_order, ctx)?;
elements.push(element);
}
Ok(elements.into_iter())
Ok(elements)
}

/// BitWrites an iterator of parcels to the stream.
Expand All @@ -45,7 +44,7 @@ pub fn read_items_to_eof<Ctx, T>(
read: &mut dyn BitRead,
byte_order: ByteOrder,
ctx: &mut Ctx,
) -> Result<impl Iterator<Item = T>>
) -> Result<Vec<T>>
where
T: ProtocolRead<Ctx>,
{
Expand All @@ -55,7 +54,7 @@ where
Ok(item) => item,
Err(Error::IO(e)) => {
return if e.kind() == io::ErrorKind::UnexpectedEof {
Ok(items.into_iter())
Ok(items)
} else {
Err(e.into())
}
Expand Down

0 comments on commit 076b90b

Please sign in to comment.