Replace ByteString::as_slice() by a Deref implementation.

This commit is contained in:
Ms2ger 2015-05-01 21:19:11 +02:00
parent 0a5ffc4bb0
commit 84b1b52682
3 changed files with 14 additions and 13 deletions

View file

@ -400,9 +400,8 @@ impl FromJSValConvertible for USVString {
impl ToJSValConvertible for ByteString {
fn to_jsval(&self, cx: *mut JSContext) -> JSVal {
unsafe {
let slice = self.as_slice();
let jsstr = JS_NewStringCopyN(cx, slice.as_ptr() as *const libc::c_char,
slice.len() as libc::size_t);
let jsstr = JS_NewStringCopyN(cx, self.as_ptr() as *const libc::c_char,
self.len() as libc::size_t);
if jsstr.is_null() {
panic!("JS_NewStringCopyN failed");
}

View file

@ -6,6 +6,7 @@
use std::borrow::ToOwned;
use std::hash::{Hash, Hasher};
use std::ops;
use std::str;
use std::str::FromStr;
@ -27,12 +28,6 @@ impl ByteString {
str::from_utf8(&vec).ok()
}
/// Returns the underlying vector as a slice.
pub fn as_slice<'a>(&'a self) -> &'a [u8] {
let ByteString(ref vector) = *self;
vector
}
/// Returns the length.
pub fn len(&self) -> usize {
let ByteString(ref vector) = *self;
@ -158,6 +153,13 @@ impl FromStr for ByteString {
}
}
impl ops::Deref for ByteString {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.0
}
}
/// A string that is constructed from a UCS-2 buffer by replacing invalid code
/// points with the replacement character.
pub struct USVString(pub String);

View file

@ -399,14 +399,14 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
debug!("SetRequestHeader: old value = {:?}", raw[0]);
let mut buf = raw[0].clone();
buf.push_all(b", ");
buf.push_all(value.as_slice());
buf.push_all(&value);
debug!("SetRequestHeader: new value = {:?}", buf);
value = ByteString::new(buf);
},
None => {}
}
headers.set_raw(name_str.to_owned(), vec![value.as_slice().to_vec()]);
headers.set_raw(name_str.to_owned(), vec![value.to_vec()]);
Ok(())
}
@ -678,7 +678,7 @@ impl<'a> XMLHttpRequestMethods for JSRef<'a, XMLHttpRequest> {
},
_ if self.ready_state.get() != XMLHttpRequestState::Done => NullValue(),
Json => {
let decoded = UTF_8.decode(self.response.borrow().as_slice(), DecoderTrap::Replace).unwrap().to_owned();
let decoded = UTF_8.decode(&self.response.borrow(), DecoderTrap::Replace).unwrap().to_owned();
let decoded: Vec<u16> = decoded.utf16_units().collect();
let mut vp = UndefinedValue();
unsafe {
@ -1028,7 +1028,7 @@ impl<'a> PrivateXMLHttpRequestHelpers for JSRef<'a, XMLHttpRequest> {
let response = self.response.borrow();
// According to Simon, decode() should never return an error, so unwrap()ing
// the result should be fine. XXXManishearth have a closer look at this later
encoding.decode(response.as_slice(), DecoderTrap::Replace).unwrap().to_owned()
encoding.decode(&response, DecoderTrap::Replace).unwrap().to_owned()
}
fn filter_response_headers(self) -> Headers {
// https://fetch.spec.whatwg.org/#concept-response-header-list