Skip to content

Commit 763a05f

Browse files
authored
fix: use self.len() to prevent from consuming the slices iterator (#1022)
1 parent c853eba commit 763a05f

File tree

1 file changed

+4
-11
lines changed
  • commons/zenoh-buffers/src

1 file changed

+4
-11
lines changed

commons/zenoh-buffers/src/lib.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -106,17 +106,10 @@ pub mod buffer {
106106
// the iterator has 1 element.
107107
Cow::Borrowed(unsafe { slices.next().unwrap_unchecked() })
108108
}
109-
_ => {
110-
let mut l = 0;
111-
for s in slices.by_ref() {
112-
l += s.len();
113-
}
114-
let mut vec = Vec::with_capacity(l);
115-
for slice in slices {
116-
vec.extend_from_slice(slice);
117-
}
118-
Cow::Owned(vec)
119-
}
109+
_ => Cow::Owned(slices.fold(Vec::with_capacity(self.len()), |mut acc, it| {
110+
acc.extend_from_slice(it);
111+
acc
112+
})),
120113
}
121114
}
122115
}

0 commit comments

Comments
 (0)