diff --git a/src/bytes.rs b/src/bytes.rs index c4cfcaa..fa62d19 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -7,6 +7,7 @@ use std::borrow::Borrow; use std::io::Cursor; use std::sync::atomic::{self, AtomicUsize, AtomicPtr}; use std::sync::atomic::Ordering::{Relaxed, Acquire, Release, AcqRel}; +use std::iter::{FromIterator, Iterator}; /// A reference counted contiguous slice of memory. /// @@ -838,6 +839,27 @@ impl<'a> From<&'a str> for Bytes { } } +impl FromIterator for BytesMut { + fn from_iter>(into_iter: T) -> Self { + let iter = into_iter.into_iter(); + let (min, maybe_max) = iter.size_hint(); + + let mut out = BytesMut::with_capacity(maybe_max.unwrap_or(min)); + + for i in iter { + out.put(i); + } + + out + } +} + +impl FromIterator for Bytes { + fn from_iter>(into_iter: T) -> Self { + BytesMut::from_iter(into_iter).freeze() + } +} + impl PartialEq for Bytes { fn eq(&self, other: &Bytes) -> bool { self.inner.as_ref() == other.inner.as_ref()