Skip to content

Commit 2582141

Browse files
committed
---
yaml --- r: 62910 b: refs/heads/snap-stage3 c: c299230 h: refs/heads/master v: v3
1 parent cc56bf4 commit 2582141

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
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: b570536b38d3172deabb471f407993a1aa2d4a0d
4+
refs/heads/snap-stage3: c299230f3d7a43a4307ce317aa41b7e8f361a2e6
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, StrUtil};
48+
pub use str::{StrSlice, OwnedStr};
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: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,50 +2165,33 @@ pub fn as_bytes_slice<'a>(s: &'a str) -> &'a [u8] {
21652165
}
21662166

21672167
/**
2168-
* A dummy trait to hold all the utility methods that we implement on strings.
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+
* ~~~
21692181
*/
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-
}
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)
22002191
}
22012192
}
22022193
}
22032194

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-
22122195
/**
22132196
* Work with the byte buffer and length of a slice.
22142197
*

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> ~[T] {
149149
do as_mut_buf(v) |p, _len| {
150150
let mut i: uint = 0u;
151151
while i < n_elts {
152-
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)),
153-
op(i));
152+
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), op(i));
154153
i += 1u;
155154
}
156155
}
@@ -166,7 +165,20 @@ pub fn from_fn<T>(n_elts: uint, op: old_iter::InitOp<T>) -> ~[T] {
166165
* to the value `t`.
167166
*/
168167
pub fn from_elem<T:Copy>(n_elts: uint, t: T) -> ~[T] {
169-
from_fn(n_elts, |_i| copy t)
168+
// hack: manually inline from_fn for 2x plus speedup (sadly very important, from_elem is a
169+
// bottleneck in borrowck!)
170+
unsafe {
171+
let mut v = with_capacity(n_elts);
172+
do as_mut_buf(v) |p, _len| {
173+
let mut i = 0u;
174+
while i < n_elts {
175+
intrinsics::move_val_init(&mut(*ptr::mut_offset(p, i)), copy t);
176+
i += 1u;
177+
}
178+
}
179+
raw::set_len(&mut v, n_elts);
180+
v
181+
}
170182
}
171183

172184
/// Creates a new unique vector with the same contents as the slice

0 commit comments

Comments
 (0)