Skip to content

Commit e650399

Browse files
committed
auto merge of #7096 : huonw/rust/invalid-null-str, r=thestinger
A slice of a 'static str is still 'static, but doesn't necessarily have the null terminator.
2 parents 9355330 + abadece commit e650399

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/libstd/str.rs

-16
Original file line numberDiff line numberDiff line change
@@ -1953,18 +1953,6 @@ impl NullTerminatedStr for @str {
19531953
slice
19541954
}
19551955
}
1956-
// static strings are the only slices guaranteed to a nul-terminator
1957-
impl NullTerminatedStr for &'static str {
1958-
/**
1959-
* Work with the byte buffer of a string as a byte slice.
1960-
*
1961-
* The byte slice does include the null terminator.
1962-
*/
1963-
#[inline]
1964-
fn as_bytes_with_null(&self) -> &'static [u8] {
1965-
unsafe { ::cast::transmute(*self) }
1966-
}
1967-
}
19681956
19691957
#[allow(missing_doc)]
19701958
pub trait OwnedStr {
@@ -2925,10 +2913,6 @@ mod tests {
29252913
109, 0
29262914
];
29272915
2928-
assert_eq!("".as_bytes_with_null(), &[0]);
2929-
assert_eq!("abc".as_bytes_with_null(), &['a' as u8, 'b' as u8, 'c' as u8, 0]);
2930-
assert_eq!("ศไทย中华Việt Nam".as_bytes_with_null(), v);
2931-
29322916
let s1 = @"";
29332917
let s2 = @"abc";
29342918
let s3 = @"ศไทย中华Việt Nam";

src/test/bench/shootout-fasta-redux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl RepeatFasta {
9393
let stdout = self.stdout;
9494
let alu_len = self.alu.len();
9595
let mut buf = vec::from_elem(alu_len + LINE_LEN, 0u8);
96-
let alu: &[u8] = self.alu.as_bytes_with_null();
96+
let alu: &[u8] = self.alu.as_bytes();
9797

9898
copy_memory(buf, alu, alu_len);
9999
copy_memory(vec::mut_slice(buf, alu_len, buf.len()),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let _ = (~"foo").as_bytes_with_null();
13+
let _ = (@"foo").as_bytes_with_null();
14+
15+
// a plain static slice is null terminated, but such a slice can
16+
// be sliced shorter (i.e. become non-null terminated) and still
17+
// have the static lifetime
18+
let foo: &'static str = "foo";
19+
let _ = foo.as_bytes_with_null();
20+
//~^ ERROR does not implement any method in scope named `as_bytes_with_null`
21+
}

0 commit comments

Comments
 (0)