@@ -2108,6 +2108,7 @@ pub trait OwnedStr {
2108
2108
fn reserve_at_least(&mut self, n: uint);
2109
2109
fn capacity(&self) -> uint;
2110
2110
fn truncate(&mut self, len: uint);
2111
+ fn into_bytes(self) -> ~[u8];
2111
2112
2112
2113
/// Work with the mutable byte buffer and length of a slice.
2113
2114
///
@@ -2273,6 +2274,13 @@ impl OwnedStr for ~str {
2273
2274
unsafe { raw::set_len(self, len); }
2274
2275
}
2275
2276
2277
+ /// Consumes the string, returning the underlying byte buffer.
2278
+ ///
2279
+ /// The buffer does not have a null terminator.
2280
+ #[inline]
2281
+ fn into_bytes(self) -> ~[u8] {
2282
+ unsafe { cast::transmute(self) }
2283
+ }
2276
2284
2277
2285
#[inline]
2278
2286
fn as_mut_buf<T>(&mut self, f: &fn(*mut u8, uint) -> T) -> T {
@@ -2356,7 +2364,7 @@ mod tests {
2356
2364
use ptr;
2357
2365
use str::*;
2358
2366
use vec;
2359
- use vec::{ImmutableVector, CopyableVector};
2367
+ use vec::{Vector, ImmutableVector, CopyableVector};
2360
2368
use cmp::{TotalOrd, Less, Equal, Greater};
2361
2369
2362
2370
#[test]
@@ -2524,6 +2532,13 @@ mod tests {
2524
2532
assert_eq!(" 华", data.as_slice());
2525
2533
}
2526
2534
2535
+ #[test]
2536
+ fn test_into_bytes() {
2537
+ let data = ~" asdf";
2538
+ let buf = data.into_bytes();
2539
+ assert_eq!(bytes!(" asdf"), buf.as_slice());
2540
+ }
2541
+
2527
2542
#[test]
2528
2543
fn test_find_str() {
2529
2544
// byte positions
0 commit comments