From 34540be54c0b7eae39a2df5f2c5419d2ca6b7461 Mon Sep 17 00:00:00 2001 From: Jef <jackefransham@gmail.com> Date: Thu, 17 Aug 2017 19:32:59 +0200 Subject: [PATCH] Add `FromIterator` impl (#148) --- src/bytes.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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<u8> for BytesMut { + fn from_iter<T: IntoIterator<Item = u8>>(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<u8> for Bytes { + fn from_iter<T: IntoIterator<Item = u8>>(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() -- GitLab