Add methods to move ByteStrings underlying bytes

Add methods to move the underlying Vec<u8> for ByteString.
This commit is contained in:
Daniel Robertson 2016-02-17 19:45:35 +00:00
parent 184c7dbd47
commit d23774d3d7
4 changed files with 32 additions and 1 deletions

View file

@ -7,6 +7,7 @@
use std::ascii::AsciiExt;
use std::borrow::ToOwned;
use std::hash::{Hash, Hasher};
use std::mem;
use std::ops;
use std::str;
use std::str::FromStr;
@ -28,6 +29,12 @@ impl ByteString {
str::from_utf8(&vec).ok()
}
/// Returns ownership of the underlying Vec<u8> and copies an empty
/// vec in its place
pub fn bytes(&mut self) -> Vec<u8> {
mem::replace(&mut self.0, Vec::new())
}
/// Returns the length.
pub fn len(&self) -> usize {
let ByteString(ref vector) = *self;
@ -117,6 +124,12 @@ impl ByteString {
}
}
impl Into<Vec<u8>> for ByteString {
fn into(self) -> Vec<u8> {
self.0
}
}
impl Hash for ByteString {
fn hash<H: Hasher>(&self, state: &mut H) {
let ByteString(ref vec) = *self;