Skip to content

Commit c77d58f

Browse files
committed
Add as_c_str method on strings
1 parent 91a7073 commit c77d58f

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

src/libstd/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub use path::PosixPath;
4545
pub use path::WindowsPath;
4646
pub use ptr::Ptr;
4747
pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr};
48-
pub use str::{StrSlice, OwnedStr};
48+
pub use str::{StrSlice, OwnedStr, StrUtil};
4949
pub use from_str::{FromStr};
5050
pub use to_bytes::IterBytes;
5151
pub use to_str::{ToStr, ToStrConsume};

src/libstd/str.rs

+39-22
Original file line numberDiff line numberDiff line change
@@ -2165,33 +2165,50 @@ pub fn as_bytes_slice<'a>(s: &'a str) -> &'a [u8] {
21652165
}
21662166

21672167
/**
2168-
* Work with the byte buffer of a string as a null-terminated C string.
2169-
*
2170-
* Allows for unsafe manipulation of strings, which is useful for foreign
2171-
* interop. This is similar to `str::as_buf`, but guarantees null-termination.
2172-
* If the given slice is not already null-terminated, this function will
2173-
* allocate a temporary, copy the slice, null terminate it, and pass
2174-
* that instead.
2175-
*
2176-
* # Example
2177-
*
2178-
* ~~~ {.rust}
2179-
* let s = str::as_c_str("PATH", { |path| libc::getenv(path) });
2180-
* ~~~
2168+
* A dummy trait to hold all the utility methods that we implement on strings.
21812169
*/
2182-
#[inline]
2183-
pub fn as_c_str<T>(s: &str, f: &fn(*libc::c_char) -> T) -> T {
2184-
do as_buf(s) |buf, len| {
2185-
// NB: len includes the trailing null.
2186-
assert!(len > 0);
2187-
if unsafe { *(ptr::offset(buf,len-1)) != 0 } {
2188-
as_c_str(to_owned(s), f)
2189-
} else {
2190-
f(buf as *libc::c_char)
2170+
pub trait StrUtil {
2171+
/**
2172+
* Work with the byte buffer of a string as a null-terminated C string.
2173+
*
2174+
* Allows for unsafe manipulation of strings, which is useful for foreign
2175+
* interop. This is similar to `str::as_buf`, but guarantees null-termination.
2176+
* If the given slice is not already null-terminated, this function will
2177+
* allocate a temporary, copy the slice, null terminate it, and pass
2178+
* that instead.
2179+
*
2180+
* # Example
2181+
*
2182+
* ~~~ {.rust}
2183+
* let s = "PATH".as_c_str(|path| libc::getenv(path));
2184+
* ~~~
2185+
*/
2186+
fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T;
2187+
}
2188+
2189+
impl<'self> StrUtil for &'self str {
2190+
#[inline]
2191+
fn as_c_str<T>(self, f: &fn(*libc::c_char) -> T) -> T {
2192+
do as_buf(self) |buf, len| {
2193+
// NB: len includes the trailing null.
2194+
assert!(len > 0);
2195+
if unsafe { *(ptr::offset(buf,len-1)) != 0 } {
2196+
to_owned(self).as_c_str(f)
2197+
} else {
2198+
f(buf as *libc::c_char)
2199+
}
21912200
}
21922201
}
21932202
}
21942203

2204+
/**
2205+
* Deprecated. Use the `as_c_str` method on strings instead.
2206+
*/
2207+
#[inline(always)]
2208+
pub fn as_c_str<T>(s: &str, f: &fn(*libc::c_char) -> T) -> T {
2209+
s.as_c_str(f)
2210+
}
2211+
21952212
/**
21962213
* Work with the byte buffer and length of a slice.
21972214
*

0 commit comments

Comments
 (0)