Bug 1312338 part 1 - Add as_str_unchecked() to nsACString. r=mystor draft
authorXidorn Quan <me@upsuper.org>
Mon, 24 Oct 2016 22:33:01 +1100
changeset 429029 d91104ea4dd66d39f1d9a00eff3a2027f6870176
parent 428619 e1c6c680a4718619e1a4cbecd84cccac4101d69b
child 429030 c0a7498ea402f1db4aca1c05508f577bacf21f0c
push id33477
push userxquan@mozilla.com
push dateTue, 25 Oct 2016 03:45:40 +0000
reviewersmystor
bugs1312338
milestone52.0a1
Bug 1312338 part 1 - Add as_str_unchecked() to nsACString. r=mystor MozReview-Commit-ID: 8FwxFGtnhjp
servo/components/style/gecko_bindings/nsstring_vendor/src/lib.rs
xpcom/rust/nsstring/src/lib.rs
--- a/servo/components/style/gecko_bindings/nsstring_vendor/src/lib.rs
+++ b/servo/components/style/gecko_bindings/nsstring_vendor/src/lib.rs
@@ -139,16 +139,17 @@
 
 use std::ops::{Deref, DerefMut};
 use std::marker::PhantomData;
 use std::slice;
 use std::ptr;
 use std::mem;
 use std::fmt;
 use std::cmp;
+use std::str;
 use std::u32;
 
 //////////////////////////////////
 // Internal Implemenation Flags //
 //////////////////////////////////
 
 const F_NONE: u32 = 0; // no flags
 
@@ -564,16 +565,20 @@ impl nsACString {
         }
     }
 
     pub fn append_utf16(&mut self, other: &nsAString) {
         unsafe {
             Gecko_AppendUTF16toCString(self as *mut _, other as *const _);
         }
     }
+
+    pub unsafe fn as_str_unchecked(&self) -> &str {
+        str::from_utf8_unchecked(self)
+    }
 }
 
 impl<'a> From<&'a str> for nsCString<'a> {
     fn from(s: &'a str) -> nsCString<'a> {
         s.as_bytes().into()
     }
 }
 
--- a/xpcom/rust/nsstring/src/lib.rs
+++ b/xpcom/rust/nsstring/src/lib.rs
@@ -135,16 +135,17 @@
 
 use std::ops::{Deref, DerefMut};
 use std::marker::PhantomData;
 use std::slice;
 use std::ptr;
 use std::mem;
 use std::fmt;
 use std::cmp;
+use std::str;
 use std::u32;
 
 //////////////////////////////////
 // Internal Implemenation Flags //
 //////////////////////////////////
 
 const F_NONE: u32 = 0; // no flags
 
@@ -558,16 +559,20 @@ impl nsACString {
         }
     }
 
     pub fn append_utf16(&mut self, other: &nsAString) {
         unsafe {
             Gecko_AppendUTF16toCString(self as *mut _, other as *const _);
         }
     }
+
+    pub unsafe fn as_str_unchecked(&self) -> &str {
+        str::from_utf8_unchecked(self)
+    }
 }
 
 impl<'a> From<&'a str> for nsCString<'a> {
     fn from(s: &'a str) -> nsCString<'a> {
         s.as_bytes().into()
     }
 }