]> git.lizzy.rs Git - rust.git/commitdiff
str: move as_mut_buf into OwnedStr, and make it `self`
authorErick Tryzelaar <erick.tryzelaar@gmail.com>
Tue, 23 Jul 2013 19:30:34 +0000 (12:30 -0700)
committerErick Tryzelaar <erick.tryzelaar@gmail.com>
Tue, 23 Jul 2013 23:56:58 +0000 (16:56 -0700)
src/libstd/str.rs

index 42b651a8e3839e63e899ae9eb9d97c3146d2c2b8..95cbc5aa4097e12e2b051e8508fa66ec85c315d4 100644 (file)
@@ -1192,7 +1192,6 @@ fn split_options_iter<Sep: CharEq>(&self, sep: Sep, count: uint, allow_trailing_
     fn subslice_offset(&self, inner: &str) -> uint;
 
     fn as_imm_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T;
-    fn as_mut_buf<T>(&self, f: &fn(*mut u8, uint) -> T) -> T;
     fn as_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T;
 }
 
@@ -1965,23 +1964,6 @@ fn as_imm_buf<T>(&self, f: &fn(*u8, uint) -> T) -> T {
         }
     }
 
-    /**
-     * Work with the byte buffer and length of a slice.
-     *
-     * The given length is one byte longer than the 'official' indexable
-     * length of the string. This is to permit probing the byte past the
-     * indexable area for a null byte, as is the case in slices pointing
-     * to full strings, or suffixes of them.
-     */
-    #[inline]
-    fn as_mut_buf<T>(&self, f: &fn(*mut u8, uint) -> T) -> T {
-        unsafe {
-            let v: *(*mut u8, uint) = cast::transmute(self);
-            let (buf, len) = *v;
-            f(buf, len)
-        }
-    }
-
     /**
      * Work with the byte buffer of a string as a null-terminated C string.
      *
@@ -2056,6 +2038,18 @@ pub trait OwnedStr {
     fn reserve_at_least(&mut self, n: uint);
     fn capacity(&self) -> uint;
     fn to_bytes_with_null(self) -> ~[u8];
+
+    /**
+     * Work with the mutable byte buffer and length of a slice.
+     *
+     * The given length is one byte longer than the 'official' indexable
+     * length of the string. This is to permit probing the byte past the
+     * indexable area for a null byte, as is the case in slices pointing
+     * to full strings, or suffixes of them.
+     *
+     * Make sure any mutations to this buffer keep this string valid UTF8.
+     */
+    fn as_mut_buf<T>(&mut self, f: &fn(*mut u8, uint) -> T) -> T;
 }
 
 impl OwnedStr for ~str {
@@ -2246,6 +2240,12 @@ fn capacity(&self) -> uint {
     fn to_bytes_with_null(self) -> ~[u8] {
         unsafe { ::cast::transmute(self) }
     }
+
+    #[inline]
+    fn as_mut_buf<T>(&mut self, f: &fn(*mut u8, uint) -> T) -> T {
+        let v: &mut ~[u8] = unsafe { cast::transmute(self) };
+        v.as_mut_buf(f)
+    }
 }
 
 impl Clone for ~str {