Skip to content

Commit e311a1e

Browse files
committed
Merge pull request #8733 from sfackler/str-to-bytes
Add OwnedStr::into_bytes
2 parents 59ca7a8 + e173a96 commit e311a1e

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/libstd/str.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2108,6 +2108,7 @@ pub trait OwnedStr {
21082108
fn reserve_at_least(&mut self, n: uint);
21092109
fn capacity(&self) -> uint;
21102110
fn truncate(&mut self, len: uint);
2111+
fn into_bytes(self) -> ~[u8];
21112112
21122113
/// Work with the mutable byte buffer and length of a slice.
21132114
///
@@ -2273,6 +2274,13 @@ impl OwnedStr for ~str {
22732274
unsafe { raw::set_len(self, len); }
22742275
}
22752276
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+
}
22762284
22772285
#[inline]
22782286
fn as_mut_buf<T>(&mut self, f: &fn(*mut u8, uint) -> T) -> T {
@@ -2356,7 +2364,7 @@ mod tests {
23562364
use ptr;
23572365
use str::*;
23582366
use vec;
2359-
use vec::{ImmutableVector, CopyableVector};
2367+
use vec::{Vector, ImmutableVector, CopyableVector};
23602368
use cmp::{TotalOrd, Less, Equal, Greater};
23612369
23622370
#[test]
@@ -2524,6 +2532,13 @@ mod tests {
25242532
assert_eq!("", data.as_slice());
25252533
}
25262534
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+
25272542
#[test]
25282543
fn test_find_str() {
25292544
// byte positions

0 commit comments

Comments
 (0)