Skip to content

Commit 7212674

Browse files
committed
---
yaml --- r: 73635 b: refs/heads/dist-snap c: c23843c h: refs/heads/master i: 73633: 381cf72 73631: bba81cc v: v3
1 parent aadd5e3 commit 7212674

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
@@ -7,7 +7,7 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c
99
refs/heads/incoming: b50030718cf28f2a5a81857a26b57442734fe854
10-
refs/heads/dist-snap: c299230f3d7a43a4307ce317aa41b7e8f361a2e6
10+
refs/heads/dist-snap: c23843c4471dcacb203d4439ef7c19bb2c3238b0
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1313
refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0

branches/dist-snap/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/dist-snap/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)