Skip to content

Commit 4926ace

Browse files
committed
---
yaml --- r: 62911 b: refs/heads/snap-stage3 c: c23843c h: refs/heads/master i: 62909: cc56bf4 62907: efd8624 62903: d0cb54a 62895: 43b3b06 62879: 92cbadc 62847: c7c8adf v: v3
1 parent 2582141 commit 4926ace

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 2d28d645422c1617be58c8ca7ad9a457264ca850
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: c299230f3d7a43a4307ce317aa41b7e8f361a2e6
4+
refs/heads/snap-stage3: c23843c4471dcacb203d4439ef7c19bb2c3238b0
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libstd/prelude.rs

Lines changed: 1 addition & 1 deletion
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};

branches/snap-stage3/src/libstd/str.rs

Lines changed: 39 additions & 22 deletions
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)