core: Make str::as_bytes handle failure. Closes #2156
This commit is contained in:
parent
59abf93b79
commit
10236f8cd4
2 changed files with 20 additions and 4 deletions
|
@ -1509,10 +1509,8 @@ let i = str::as_bytes(\"Hello World\") { |bytes| vec::len(bytes) };
|
|||
~~~
|
||||
"]
|
||||
fn as_bytes<T>(s: str, f: fn([u8]) -> T) -> T unsafe {
|
||||
let mut v: [u8] = ::unsafe::reinterpret_cast(s);
|
||||
let r = f(v);
|
||||
::unsafe::forget(v);
|
||||
r
|
||||
let v: *[u8] = ::unsafe::reinterpret_cast(ptr::addr_of(s));
|
||||
f(*v)
|
||||
}
|
||||
|
||||
#[doc = "
|
||||
|
@ -2449,6 +2447,14 @@ mod tests {
|
|||
assert (c == "AAA");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore(cfg(target_os = "win32"))]
|
||||
#[should_fail]
|
||||
fn test_as_bytes_fail() {
|
||||
// Don't double free
|
||||
as_bytes("") {|_bytes| fail }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_as_buf() unsafe {
|
||||
let a = "Abcdefg";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue