Bug 1316964 - Make the append and assign methods on the rust nsstring types more generic, r=froydnj
authorMichael Layzell <michael@thelayzells.com>
Fri, 11 Nov 2016 16:16:08 -0500
changeset 367760 e5c8e3d23cf2750d7be106d8dd837d368db326b3
parent 367759 b99edf918b964e1ad5d096c26cc439818d925891
child 367761 a958562f3d67a8bcb8c0dd98f2e4d54c1365b675
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1316964
milestone53.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1316964 - Make the append and assign methods on the rust nsstring types more generic, r=froydnj MozReview-Commit-ID: 5y30BnUDKsT
xpcom/rust/nsstring/src/lib.rs
--- a/xpcom/rust/nsstring/src/lib.rs
+++ b/xpcom/rust/nsstring/src/lib.rs
@@ -253,16 +253,22 @@ macro_rules! define_string_types {
                         slice::from_raw_parts(0x1 as *const $char_t, 0)
                     } else {
                         slice::from_raw_parts(this.data, this.length as usize)
                     }
                 }
             }
         }
 
+        impl AsRef<[$char_t]> for $AString {
+            fn as_ref(&self) -> &[$char_t] {
+                self
+            }
+        }
+
         impl cmp::PartialEq for $AString {
             fn eq(&self, other: &$AString) -> bool {
                 &self[..] == &other[..]
             }
         }
 
         impl cmp::PartialEq<[$char_t]> for $AString {
             fn eq(&self, other: &[$char_t]) -> bool {
@@ -308,16 +314,22 @@ macro_rules! define_string_types {
         }
 
         impl<'a> DerefMut for $String<'a> {
             fn deref_mut(&mut self) -> &mut $AString {
                 &mut self.hdr
             }
         }
 
+        impl<'a> AsRef<[$char_t]> for $String<'a> {
+            fn as_ref(&self) -> &[$char_t] {
+                &self
+            }
+        }
+
         impl<'a> From<&'a String> for $String<'a> {
             fn from(s: &'a String) -> $String<'a> {
                 $String::from(&s[..])
             }
         }
 
         impl<'a> From<&'a Vec<$char_t>> for $String<'a> {
             fn from(s: &'a Vec<$char_t>) -> $String<'a> {
@@ -467,16 +479,22 @@ macro_rules! define_string_types {
         }
 
         impl<'a> DerefMut for $FixedString<'a> {
             fn deref_mut(&mut self) -> &mut $AString {
                 &mut self.hdr.base
             }
         }
 
+        impl<'a> AsRef<[$char_t]> for $FixedString<'a> {
+            fn as_ref(&self) -> &[$char_t] {
+                &self
+            }
+        }
+
         impl<'a> fmt::Write for $FixedString<'a> {
             fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
                 $AString::write_str(self, s)
             }
         }
 
         impl<'a> fmt::Display for $FixedString<'a> {
             fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
@@ -549,36 +567,39 @@ define_string_types! {
 impl nsACString {
     /// Leaves the nsACString in an unstable state with a dangling data pointer.
     /// Should only be used in drop implementations of rust types which wrap
     /// this type.
     unsafe fn finalize(&mut self) {
         Gecko_FinalizeCString(self);
     }
 
-    pub fn assign(&mut self, other: &nsACString) {
+    pub fn assign<T: AsRef<[u8]>>(&mut self, other: &T) {
+        let s = nsCString::from(other.as_ref());
         unsafe {
-            Gecko_AssignCString(self as *mut _, other as *const _);
+            Gecko_AssignCString(self, &*s);
         }
     }
 
-    pub fn assign_utf16(&mut self, other: &nsAString) {
-        self.assign(&nsCString::new());
+    pub fn assign_utf16<T: AsRef<[u16]>>(&mut self, other: &T) {
+        self.assign(&[]);
         self.append_utf16(other);
     }
 
-    pub fn append(&mut self, other: &nsACString) {
+    pub fn append<T: AsRef<[u8]>>(&mut self, other: &T) {
+        let s = nsCString::from(other.as_ref());
         unsafe {
-            Gecko_AppendCString(self as *mut _, other as *const _);
+            Gecko_AppendCString(self, &*s);
         }
     }
 
-    pub fn append_utf16(&mut self, other: &nsAString) {
+    pub fn append_utf16<T: AsRef<[u16]>>(&mut self, other: &T) {
+        let s = nsString::from(other.as_ref());
         unsafe {
-            Gecko_AppendUTF16toCString(self as *mut _, other as *const _);
+            Gecko_AppendUTF16toCString(self, &*s);
         }
     }
 
     pub unsafe fn as_str_unchecked(&self) -> &str {
         str::from_utf8_unchecked(self)
     }
 }
 
@@ -653,36 +674,39 @@ define_string_types! {
 impl nsAString {
     /// Leaves the nsAString in an unstable state with a dangling data pointer.
     /// Should only be used in drop implementations of rust types which wrap
     /// this type.
     unsafe fn finalize(&mut self) {
         Gecko_FinalizeString(self);
     }
 
-    pub fn assign(&mut self, other: &nsAString) {
+    pub fn assign<T: AsRef<[u16]>>(&mut self, other: &T) {
+        let s = nsString::from(other.as_ref());
         unsafe {
-            Gecko_AssignString(self as *mut _, other as *const _);
+            Gecko_AssignString(self, &*s);
         }
     }
 
-    pub fn assign_utf8(&mut self, other: &nsACString) {
-        self.assign(&nsString::new());
+    pub fn assign_utf8<T: AsRef<[u8]>>(&mut self, other: &T) {
+        self.assign(&[]);
         self.append_utf8(other);
     }
 
-    pub fn append(&mut self, other: &nsAString) {
+    pub fn append<T: AsRef<[u16]>>(&mut self, other: &T) {
+        let s = nsString::from(other.as_ref());
         unsafe {
-            Gecko_AppendString(self as *mut _, other as *const _);
+            Gecko_AppendString(self, &*s);
         }
     }
 
-    pub fn append_utf8(&mut self, other: &nsACString) {
+    pub fn append_utf8<T: AsRef<[u8]>>(&mut self, other: &T) {
+        let s = nsCString::from(other.as_ref());
         unsafe {
-            Gecko_AppendUTF8toString(self as *mut _, other as *const _);
+            Gecko_AppendUTF8toString(self, &*s);
         }
     }
 }
 
 // NOTE: The From impl for a string slice for nsString produces a <'static>
 // lifetime, as it allocates.
 impl<'a> From<&'a str> for nsString<'static> {
     fn from(s: &'a str) -> nsString<'static> {