From 763a05f0d61a894cf3cbeb07223e2ea726777325 Mon Sep 17 00:00:00 2001 From: Yuyuan Yuan Date: Tue, 14 May 2024 21:03:41 +0800 Subject: [PATCH] fix: use `self.len()` to prevent from consuming the slices iterator (#1022) --- commons/zenoh-buffers/src/lib.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/commons/zenoh-buffers/src/lib.rs b/commons/zenoh-buffers/src/lib.rs index abd85d024c..8641ff4afc 100644 --- a/commons/zenoh-buffers/src/lib.rs +++ b/commons/zenoh-buffers/src/lib.rs @@ -106,17 +106,10 @@ pub mod buffer { // the iterator has 1 element. Cow::Borrowed(unsafe { slices.next().unwrap_unchecked() }) } - _ => { - let mut l = 0; - for s in slices.by_ref() { - l += s.len(); - } - let mut vec = Vec::with_capacity(l); - for slice in slices { - vec.extend_from_slice(slice); - } - Cow::Owned(vec) - } + _ => Cow::Owned(slices.fold(Vec::with_capacity(self.len()), |mut acc, it| { + acc.extend_from_slice(it); + acc + })), } } }