From ec7d7f27fe04ca220caafd6691b06690cca7b698 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Fri, 14 Oct 2016 20:07:57 -0700 Subject: [PATCH] Add Bytes::from_vec --- src/imp/bytes/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/imp/bytes/mod.rs b/src/imp/bytes/mod.rs index daf4bd4..e5edf8b 100644 --- a/src/imp/bytes/mod.rs +++ b/src/imp/bytes/mod.rs @@ -44,6 +44,18 @@ impl Bytes { .unwrap_or_else(|| Seq::from_slice(slice.as_ref())) } + pub fn from_vec(mem: Vec) -> Bytes { + let pos = 0; + let len = mem.len(); + + Small::from_slice(&mem[..]) + .map(|b| Bytes { kind: Kind::Small(b) }) + .unwrap_or_else(|| { + let seq = Seq::new(Arc::new(mem.into_boxed_slice()), pos, len); + Bytes { kind: Kind::Seq(seq) } + }) + } + /// Creates a new `Bytes` from an `Arc>`, an offset, and a length. #[inline] pub fn from_boxed(mem: Arc>, pos: usize, len: usize) -> Bytes {