]> git.lizzy.rs Git - rust.git/commitdiff
std::str: remove .as_mut_buf & rewrite/simplify `.push_char`.
authorHuon Wilson <dbau.pp+github@gmail.com>
Tue, 17 Dec 2013 15:46:26 +0000 (02:46 +1100)
committerHuon Wilson <dbau.pp+github@gmail.com>
Wed, 18 Dec 2013 23:18:02 +0000 (10:18 +1100)
`.as_mut_buf` was used exactly once, in `.push_char` which could be
written in a simpler way, using the `&mut ~[u8]` that it already
retrieved. In the rare situation when someone really needs
`.as_mut_buf`-like functionality (getting a `*mut u8`), they can go via
`str::raw::as_owned_vec`.

src/libstd/str.rs

index e90d3140077fb353dc83af33cc6da74e3ca0e604..78a09d459def21359f4a41f01db95f1ede21f5af 100644 (file)
@@ -2555,14 +2555,6 @@ pub trait OwnedStr {
     /// The buffer does not have a null terminator.
     fn into_bytes(self) -> ~[u8];
 
-    /// Work with the mutable byte buffer and length of a slice.
-    ///
-    /// The buffer does not have a null terminator.
-    ///
-    /// The caller must make sure any mutations to this buffer keep the string
-    /// valid UTF-8!
-    fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T;
-
     /// Sets the length of a string
     ///
     /// This will explicitly set the size of the string, without actually
@@ -2591,16 +2583,15 @@ fn push_char(&mut self, c: char) {
         let cur_len = self.len();
         // may use up to 4 bytes.
         unsafe {
-            raw::as_owned_vec(self).reserve_additional(4);
+            let v = raw::as_owned_vec(self);
+            v.reserve_additional(4);
 
             // Attempt to not use an intermediate buffer by just pushing bytes
             // directly onto this string.
-            let used = self.as_mut_buf(|buf, _| {
-                vec::raw::mut_buf_as_slice(buf.offset(cur_len as int), 4, |slc| {
-                    c.encode_utf8(slc)
-                })
-            });
-            self.set_len(cur_len + used);
+            let write_ptr = v.as_mut_ptr().offset(cur_len as int);
+            let used = vec::raw::mut_buf_as_slice(write_ptr, 4, |slc| c.encode_utf8(slc));
+
+            v.set_len(cur_len + used);
         }
     }
 
@@ -2668,14 +2659,6 @@ fn into_bytes(self) -> ~[u8] {
         unsafe { cast::transmute(self) }
     }
 
-    #[inline]
-    fn as_mut_buf<T>(&mut self, f: |*mut u8, uint| -> T) -> T {
-        unsafe {
-            let v = raw::as_owned_vec(self);
-            f(v.as_mut_ptr(), v.len())
-        }
-    }
-
     #[inline]
     unsafe fn set_len(&mut self, new_len: uint) {
         raw::as_owned_vec(self).set_len(new_len)