diff --git a/components/script/dom/bindings/str.rs b/components/script/dom/bindings/str.rs index 3f3c14bf854..850277f596e 100644 --- a/components/script/dom/bindings/str.rs +++ b/components/script/dom/bindings/str.rs @@ -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; @@ -24,14 +25,18 @@ impl ByteString { /// Returns `self` as a string, if it encodes valid UTF-8, and `None` /// otherwise. pub fn as_str(&self) -> Option<&str> { - let ByteString(ref vec) = *self; - str::from_utf8(&vec).ok() + str::from_utf8(&self.0).ok() + } + + /// Returns ownership of the underlying Vec and copies an empty + /// vec in its place + pub fn bytes(&mut self) -> Vec { + mem::replace(&mut self.0, Vec::new()) } /// Returns the length. pub fn len(&self) -> usize { - let ByteString(ref vector) = *self; - vector.len() + self.0.len() } /// Compare `self` to `other`, matching A–Z and a–z as equal. @@ -47,8 +52,7 @@ impl ByteString { /// Returns whether `self` is a `token`, as defined by /// [RFC 2616](http://tools.ietf.org/html/rfc2616#page-17). pub fn is_token(&self) -> bool { - let ByteString(ref vec) = *self; - is_token(vec) + is_token(&self.0) } /// Returns whether `self` is a `field-value`, as defined by @@ -62,9 +66,8 @@ impl ByteString { LF, SPHT, // SP or HT } - let ByteString(ref vec) = *self; let mut prev = PreviousCharacter::Other; // The previous character - vec.iter().all(|&x| { + self.0.iter().all(|&x| { // http://tools.ietf.org/html/rfc2616#section-2.2 match x { 13 => { // CR @@ -117,6 +120,12 @@ impl ByteString { } } +impl Into> for ByteString { + fn into(self) -> Vec { + self.0 + } +} + impl Hash for ByteString { fn hash(&self, state: &mut H) { let ByteString(ref vec) = *self; diff --git a/components/script/dom/xmlhttprequest.rs b/components/script/dom/xmlhttprequest.rs index 355cd989e93..72ad07d89a6 100644 --- a/components/script/dom/xmlhttprequest.rs +++ b/components/script/dom/xmlhttprequest.rs @@ -456,7 +456,7 @@ impl XMLHttpRequestMethods for XMLHttpRequest { None => {} } - headers.set_raw(name_str.to_owned(), vec![value.to_vec()]); + headers.set_raw(name_str.to_owned(), vec![value.into()]); Ok(()) } diff --git a/tests/unit/script/dom/bindings.rs b/tests/unit/script/dom/bindings.rs new file mode 100644 index 00000000000..e6742107136 --- /dev/null +++ b/tests/unit/script/dom/bindings.rs @@ -0,0 +1,17 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use script::dom::bindings::str::ByteString; + +#[test] +fn test_byte_string_move() { + let mut byte_str = ByteString::new(vec![0x73, 0x65, 0x72, 0x76, 0x6f]); + let mut byte_vec = byte_str.bytes(); + + assert_eq!(byte_vec, "servo".as_bytes()); + assert_eq!(*byte_str, []); + + byte_vec = byte_str.into(); + assert_eq!(byte_vec, Vec::new()); +} diff --git a/tests/unit/script/lib.rs b/tests/unit/script/lib.rs index 7996dae56cb..fd5bea0f718 100644 --- a/tests/unit/script/lib.rs +++ b/tests/unit/script/lib.rs @@ -9,5 +9,6 @@ extern crate util; #[cfg(all(test, target_pointer_width = "64"))] mod size_of; #[cfg(test)] mod textinput; #[cfg(test)] mod dom { + mod bindings; mod blob; }