Skip to content

Commit 5f34069

Browse files
authored
Add IntoIterator to OwnedBinary (#702)
Co-authored-by: Jonathan Tanner <>
1 parent 730303e commit 5f34069

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

rustler/src/types/binary.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,32 @@ impl Drop for OwnedBinary {
236236
unsafe impl Send for OwnedBinary {}
237237
unsafe impl Sync for OwnedBinary {}
238238

239+
impl FromIterator<u8> for OwnedBinary {
240+
fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
241+
let mut iter = iter.into_iter();
242+
let (lower, upper) = iter.size_hint();
243+
let mut bin = OwnedBinary::new(upper.unwrap_or(lower)).expect("Allocation failed");
244+
let mut i = 0;
245+
loop {
246+
match iter.next() {
247+
None => {
248+
if bin.len() != i {
249+
bin.realloc_or_copy(i);
250+
}
251+
return bin;
252+
}
253+
Some(x) => {
254+
if bin.len() <= i {
255+
bin.realloc_or_copy(i + i / 2 + 1);
256+
}
257+
bin.as_mut_slice()[i] = x;
258+
i += 1;
259+
}
260+
}
261+
}
262+
}
263+
}
264+
239265
/// An immutable smart-pointer to an Erlang binary.
240266
///
241267
/// See [module-level doc](index.html) for more information.

0 commit comments

Comments
 (0)