1
Fork 0

Auto merge of #22838 - petrochenkov:bytelit, r=alexcrichton

This patch changes the type of byte string literals from `&[u8]` to `&[u8; N]`.
It also implements some necessary traits (`IntoBytes`, `Seek`, `Read`, `BufRead`) for fixed-size arrays (also related to #21725) and adds test for #17233, which seems to be resolved.

Fixes #18465
[breaking-change]
This commit is contained in:
bors 2015-03-18 08:27:22 +00:00
commit 46f649c479
21 changed files with 276 additions and 191 deletions

View file

@ -23,10 +23,34 @@ use marker::{Copy, Sized};
use option::Option;
use slice::{Iter, IterMut, SliceExt};
/// Utility trait implemented only on arrays of fixed size
///
/// This trait can be used to implement other traits on fixed-size arrays
/// without causing much metadata bloat.
#[unstable(feature = "core")]
pub trait FixedSizeArray<T> {
/// Converts the array to immutable slice
fn as_slice(&self) -> &[T];
/// Converts the array to mutable slice
fn as_mut_slice(&mut self) -> &mut [T];
}
// macro for implementing n-ary tuple functions and operations
macro_rules! array_impls {
($($N:expr)+) => {
$(
#[unstable(feature = "core")]
impl<T> FixedSizeArray<T> for [T; $N] {
#[inline]
fn as_slice(&self) -> &[T] {
&self[..]
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] {
&mut self[..]
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T:Copy> Clone for [T; $N] {
fn clone(&self) -> [T; $N] {

View file

@ -129,6 +129,7 @@ pub mod default;
/* Core types and methods on primitives */
pub mod any;
pub mod array;
pub mod atomic;
pub mod cell;
pub mod char;
@ -151,7 +152,6 @@ mod bool {
// note: does not need to be public
mod tuple;
mod array;
#[doc(hidden)]
mod core {

View file

@ -448,7 +448,8 @@ impl<T, E> Result<T, E> {
/// ```
/// use std::old_io::IoResult;
///
/// let mut buffer = &mut b"1\n2\n3\n4\n";
/// let mut buffer: &[u8] = b"1\n2\n3\n4\n";
/// let mut buffer = &mut buffer;
///
/// let mut sum = 0;
///

View file

@ -200,7 +200,7 @@ use middle::mem_categorization as mc;
use middle::pat_util::*;
use trans::adt;
use trans::base::*;
use trans::build::{AddCase, And, BitCast, Br, CondBr, GEPi, InBoundsGEP, Load};
use trans::build::{AddCase, And, Br, CondBr, GEPi, InBoundsGEP, Load, PointerCast};
use trans::build::{Not, Store, Sub, add_comment};
use trans::build;
use trans::callee;
@ -853,14 +853,31 @@ fn compare_values<'blk, 'tcx>(cx: Block<'blk, 'tcx>,
ty::ty_str => compare_str(cx, lhs, rhs, rhs_t, debug_loc),
ty::ty_vec(ty, _) => match ty.sty {
ty::ty_uint(ast::TyU8) => {
// NOTE: cast &[u8] to &str and abuse the str_eq lang item,
// NOTE: cast &[u8] and &[u8; N] to &str and abuse the str_eq lang item,
// which calls memcmp().
let t = ty::mk_str_slice(cx.tcx(),
cx.tcx().mk_region(ty::ReStatic),
ast::MutImmutable);
let lhs = BitCast(cx, lhs, type_of::type_of(cx.ccx(), t).ptr_to());
let rhs = BitCast(cx, rhs, type_of::type_of(cx.ccx(), t).ptr_to());
compare_str(cx, lhs, rhs, rhs_t, debug_loc)
let pat_len = val_ty(rhs).element_type().array_length();
let ty_str_slice = ty::mk_str_slice(cx.tcx(),
cx.tcx().mk_region(ty::ReStatic),
ast::MutImmutable);
let rhs_str = alloc_ty(cx, ty_str_slice, "rhs_str");
Store(cx, GEPi(cx, rhs, &[0, 0]), expr::get_dataptr(cx, rhs_str));
Store(cx, C_uint(cx.ccx(), pat_len), expr::get_len(cx, rhs_str));
let lhs_str;
if val_ty(lhs) == val_ty(rhs) {
// Both the discriminant and the pattern are thin pointers
lhs_str = alloc_ty(cx, ty_str_slice, "lhs_str");
Store(cx, GEPi(cx, lhs, &[0, 0]), expr::get_dataptr(cx, lhs_str));
Store(cx, C_uint(cx.ccx(), pat_len), expr::get_len(cx, lhs_str));
}
else {
// The discriminant is a fat pointer
let llty_str_slice = type_of::type_of(cx.ccx(), ty_str_slice).ptr_to();
lhs_str = PointerCast(cx, lhs, llty_str_slice);
}
compare_str(cx, lhs_str, rhs_str, rhs_t, debug_loc)
},
_ => cx.sess().bug("only byte strings supported in compare_values"),
},

View file

@ -75,14 +75,7 @@ pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: &ast::Lit)
ast::LitBool(b) => C_bool(cx, b),
ast::LitStr(ref s, _) => C_str_slice(cx, (*s).clone()),
ast::LitBinary(ref data) => {
let g = addr_of(cx, C_bytes(cx, &data[..]), "binary", e.id);
let base = ptrcast(g, Type::i8p(cx));
let prev_const = cx.const_unsized().borrow_mut()
.insert(base, g);
assert!(prev_const.is_none() || prev_const == Some(g));
assert_eq!(abi::FAT_PTR_ADDR, 0);
assert_eq!(abi::FAT_PTR_EXTRA, 1);
C_struct(cx, &[base, C_uint(cx, data.len())], false)
addr_of(cx, C_bytes(cx, &data[..]), "binary", e.id)
}
}
}

View file

@ -4054,7 +4054,7 @@ fn namespace_for_item(cx: &CrateContext, def_id: ast::DefId) -> Rc<NamespaceTree
/// .debug_gdb_scripts global is referenced, so it isn't removed by the linker.
pub fn insert_reference_to_gdb_debug_scripts_section_global(ccx: &CrateContext) {
if needs_gdb_debug_scripts_section(ccx) {
let empty = CString::new(b"").unwrap();
let empty = CString::new("").unwrap();
let gdb_debug_scripts_section_global =
get_or_insert_gdb_debug_scripts_section_global(ccx);
unsafe {

View file

@ -48,7 +48,23 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
ast::PatLit(ref lt) => {
check_expr(fcx, &**lt);
let expr_ty = fcx.expr_ty(&**lt);
fcx.write_ty(pat.id, expr_ty);
// Byte string patterns behave the same way as array patterns
// They can denote both statically and dynamically sized byte arrays
let mut pat_ty = expr_ty;
if let ast::ExprLit(ref lt) = lt.node {
if let ast::LitBinary(_) = lt.node {
let expected_ty = structurally_resolved_type(fcx, pat.span, expected);
if let ty::ty_rptr(_, mt) = expected_ty.sty {
if let ty::ty_vec(_, None) = mt.ty.sty {
pat_ty = ty::mk_slice(tcx, tcx.mk_region(ty::ReStatic),
ty::mt{ ty: tcx.types.u8, mutbl: ast::MutImmutable })
}
}
}
}
fcx.write_ty(pat.id, pat_ty);
// somewhat surprising: in this case, the subtyping
// relation goes the opposite way as the other
@ -62,7 +78,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
// &'static str <: expected
//
// that's equivalent to there existing a LUB.
demand::suptype(fcx, pat.span, expected, expr_ty);
demand::suptype(fcx, pat.span, expected, pat_ty);
}
ast::PatRange(ref begin, ref end) => {
check_expr(fcx, &**begin);

View file

@ -2505,10 +2505,11 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
match lit.node {
ast::LitStr(..) => ty::mk_str_slice(tcx, tcx.mk_region(ty::ReStatic), ast::MutImmutable),
ast::LitBinary(..) => {
ty::mk_slice(tcx,
tcx.mk_region(ty::ReStatic),
ty::mt{ ty: tcx.types.u8, mutbl: ast::MutImmutable })
ast::LitBinary(ref v) => {
ty::mk_rptr(tcx, tcx.mk_region(ty::ReStatic), ty::mt {
ty: ty::mk_vec(tcx, tcx.types.u8, Some(v.len())),
mutbl: ast::MutImmutable,
})
}
ast::LitByte(_) => tcx.types.u8,
ast::LitChar(_) => tcx.types.char,

View file

@ -58,7 +58,7 @@ use vec::Vec;
/// fn my_printer(s: *const libc::c_char);
/// }
///
/// let to_print = b"Hello, world!";
/// let to_print = &b"Hello, world!"[..];
/// let c_to_print = CString::new(to_print).unwrap();
/// unsafe {
/// my_printer(c_to_print.as_ptr());
@ -469,14 +469,14 @@ mod tests {
#[test]
fn simple() {
let s = CString::new(b"1234").unwrap();
let s = CString::new("1234").unwrap();
assert_eq!(s.as_bytes(), b"1234");
assert_eq!(s.as_bytes_with_nul(), b"1234\0");
}
#[test]
fn build_with_zero1() {
assert!(CString::new(b"\0").is_err());
assert!(CString::new(&b"\0"[..]).is_err());
}
#[test]
fn build_with_zero2() {
@ -493,7 +493,7 @@ mod tests {
#[test]
fn formatted() {
let s = CString::new(b"12").unwrap();
let s = CString::new(&b"12"[..]).unwrap();
assert_eq!(format!("{:?}", s), "\"12\"");
}

View file

@ -622,7 +622,7 @@ mod tests {
#[test]
fn test_read_line() {
let in_buf = b"a\nb\nc";
let in_buf: &[u8] = b"a\nb\nc";
let mut reader = BufReader::with_capacity(2, in_buf);
let mut s = String::new();
reader.read_line(&mut s).unwrap();
@ -640,7 +640,7 @@ mod tests {
#[test]
fn test_lines() {
let in_buf = b"a\nb\nc";
let in_buf: &[u8] = b"a\nb\nc";
let reader = BufReader::with_capacity(2, in_buf);
let mut it = reader.lines();
assert_eq!(it.next(), Some(Ok("a".to_string())));

View file

@ -328,7 +328,7 @@ mod tests {
#[test]
fn test_read_char() {
let b = b"Vi\xE1\xBB\x87t";
let b = &b"Vi\xE1\xBB\x87t"[..];
let mut c = Cursor::new(b).chars();
assert_eq!(c.next(), Some(Ok('V')));
assert_eq!(c.next(), Some(Ok('i')));
@ -339,7 +339,7 @@ mod tests {
#[test]
fn test_read_bad_char() {
let b = b"\x80";
let b = &b"\x80"[..];
let mut c = Cursor::new(b).chars();
assert!(c.next().unwrap().is_err());
}

View file

@ -933,12 +933,12 @@ mod tests {
#[test]
fn read_until() {
let mut buf = Cursor::new(b"12");
let mut buf = Cursor::new(&b"12"[..]);
let mut v = Vec::new();
assert_eq!(buf.read_until(b'3', &mut v), Ok(2));
assert_eq!(v, b"12");
let mut buf = Cursor::new(b"1233");
let mut buf = Cursor::new(&b"1233"[..]);
let mut v = Vec::new();
assert_eq!(buf.read_until(b'3', &mut v), Ok(3));
assert_eq!(v, b"123");
@ -952,12 +952,12 @@ mod tests {
#[test]
fn split() {
let buf = Cursor::new(b"12");
let buf = Cursor::new(&b"12"[..]);
let mut s = buf.split(b'3');
assert_eq!(s.next(), Some(Ok(vec![b'1', b'2'])));
assert_eq!(s.next(), None);
let buf = Cursor::new(b"1233");
let buf = Cursor::new(&b"1233"[..]);
let mut s = buf.split(b'3');
assert_eq!(s.next(), Some(Ok(vec![b'1', b'2'])));
assert_eq!(s.next(), Some(Ok(vec![])));
@ -966,12 +966,12 @@ mod tests {
#[test]
fn read_line() {
let mut buf = Cursor::new(b"12");
let mut buf = Cursor::new(&b"12"[..]);
let mut v = String::new();
assert_eq!(buf.read_line(&mut v), Ok(2));
assert_eq!(v, "12");
let mut buf = Cursor::new(b"12\n\n");
let mut buf = Cursor::new(&b"12\n\n"[..]);
let mut v = String::new();
assert_eq!(buf.read_line(&mut v), Ok(3));
assert_eq!(v, "12\n");
@ -985,12 +985,12 @@ mod tests {
#[test]
fn lines() {
let buf = Cursor::new(b"12");
let buf = Cursor::new(&b"12"[..]);
let mut s = buf.lines();
assert_eq!(s.next(), Some(Ok("12".to_string())));
assert_eq!(s.next(), None);
let buf = Cursor::new(b"12\n\n");
let buf = Cursor::new(&b"12\n\n"[..]);
let mut s = buf.lines();
assert_eq!(s.next(), Some(Ok("12".to_string())));
assert_eq!(s.next(), Some(Ok(String::new())));
@ -999,12 +999,12 @@ mod tests {
#[test]
fn read_to_end() {
let mut c = Cursor::new(b"");
let mut c = Cursor::new(&b""[..]);
let mut v = Vec::new();
assert_eq!(c.read_to_end(&mut v), Ok(0));
assert_eq!(v, []);
let mut c = Cursor::new(b"1");
let mut c = Cursor::new(&b"1"[..]);
let mut v = Vec::new();
assert_eq!(c.read_to_end(&mut v), Ok(1));
assert_eq!(v, b"1");
@ -1012,17 +1012,17 @@ mod tests {
#[test]
fn read_to_string() {
let mut c = Cursor::new(b"");
let mut c = Cursor::new(&b""[..]);
let mut v = String::new();
assert_eq!(c.read_to_string(&mut v), Ok(0));
assert_eq!(v, "");
let mut c = Cursor::new(b"1");
let mut c = Cursor::new(&b"1"[..]);
let mut v = String::new();
assert_eq!(c.read_to_string(&mut v), Ok(1));
assert_eq!(v, "1");
let mut c = Cursor::new(b"\xff");
let mut c = Cursor::new(&b"\xff"[..]);
let mut v = String::new();
assert!(c.read_to_string(&mut v).is_err());
}

View file

@ -1231,7 +1231,7 @@ mod tests {
cmd.env("path", "foo");
cmd.env("Path", "bar");
let env = &cmd.env.unwrap();
let val = env.get(&EnvKey(CString::new(b"PATH").unwrap()));
assert!(val.unwrap() == &CString::new(b"bar").unwrap());
let val = env.get(&EnvKey(CString::new("PATH").unwrap()));
assert!(val.unwrap() == &CString::new("bar").unwrap());
}
}

View file

@ -445,7 +445,8 @@ mod test {
#[test]
fn limit_reader_buffer() {
let r = &mut b"0123456789\n0123456789\n";
let mut r: &[u8] = b"0123456789\n0123456789\n";
let r = &mut r;
{
let mut r = LimitReader::new(r.by_ref(), 3);
assert_eq!(r.read_line(), Ok("012".to_string()));

View file

@ -311,7 +311,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
/// # #[cfg(windows)] fn foo() {}
/// # #[cfg(unix)] fn foo() {
/// let p = Path::new("abc/def/ghi");
/// assert_eq!(p.filename(), Some(b"ghi"));
/// assert_eq!(p.filename(), Some(&b"ghi"[..]));
/// # }
/// ```
fn filename<'a>(&'a self) -> Option<&'a [u8]>;
@ -345,7 +345,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
/// # #[cfg(windows)] fn foo() {}
/// # #[cfg(unix)] fn foo() {
/// let p = Path::new("/abc/def.txt");
/// assert_eq!(p.filestem(), Some(b"def"));
/// assert_eq!(p.filestem(), Some(&b"def"[..]));
/// # }
/// ```
fn filestem<'a>(&'a self) -> Option<&'a [u8]> {
@ -392,7 +392,7 @@ pub trait GenericPath: Clone + GenericPathUnsafe {
/// # #[cfg(windows)] fn foo() {}
/// # #[cfg(unix)] fn foo() {
/// let p = Path::new("abc/def.txt");
/// assert_eq!(p.extension(), Some(b"txt"));
/// assert_eq!(p.extension(), Some(&b"txt"[..]));
/// # }
/// ```
fn extension<'a>(&'a self) -> Option<&'a [u8]> {

View file

@ -477,11 +477,11 @@ mod tests {
fn test_paths() {
let empty: &[u8] = &[];
t!(v: Path::new(empty), b".");
t!(v: Path::new(b"/"), b"/");
t!(v: Path::new(b"a/b/c"), b"a/b/c");
t!(v: Path::new(b"a/b/c\xFF"), b"a/b/c\xFF");
t!(v: Path::new(b"\xFF/../foo\x80"), b"foo\x80");
let p = Path::new(b"a/b/c\xFF");
t!(v: Path::new(&b"/"[..]), b"/");
t!(v: Path::new(&b"a/b/c"[..]), b"a/b/c");
t!(v: Path::new(&b"a/b/c\xFF"[..]), b"a/b/c\xFF");
t!(v: Path::new(&b"\xFF/../foo\x80"[..]), b"foo\x80");
let p = Path::new(&b"a/b/c\xFF"[..]);
assert!(p.as_str().is_none());
t!(s: Path::new(""), ".");
@ -507,18 +507,18 @@ mod tests {
t!(s: Path::new("foo/../../.."), "../..");
t!(s: Path::new("foo/../../bar"), "../bar");
assert_eq!(Path::new(b"foo/bar").into_vec(), b"foo/bar");
assert_eq!(Path::new(b"/foo/../../bar").into_vec(),
assert_eq!(Path::new(&b"foo/bar"[..]).into_vec(), b"foo/bar");
assert_eq!(Path::new(&b"/foo/../../bar"[..]).into_vec(),
b"/bar");
let p = Path::new(b"foo/bar\x80");
let p = Path::new(&b"foo/bar\x80"[..]);
assert!(p.as_str().is_none());
}
#[test]
fn test_opt_paths() {
assert!(Path::new_opt(b"foo/bar\0").is_none());
t!(v: Path::new_opt(b"foo/bar").unwrap(), b"foo/bar");
assert!(Path::new_opt(&b"foo/bar\0"[..]).is_none());
t!(v: Path::new_opt(&b"foo/bar"[..]).unwrap(), b"foo/bar");
assert!(Path::new_opt("foo/bar\0").is_none());
t!(s: Path::new_opt("foo/bar").unwrap(), "foo/bar");
}
@ -527,17 +527,17 @@ mod tests {
fn test_null_byte() {
use thread;
let result = thread::spawn(move|| {
Path::new(b"foo/bar\0");
Path::new(&b"foo/bar\0"[..]);
}).join();
assert!(result.is_err());
let result = thread::spawn(move|| {
Path::new("test").set_filename(b"f\0o")
Path::new("test").set_filename(&b"f\0o"[..])
}).join();
assert!(result.is_err());
let result = thread::spawn(move|| {
Path::new("test").push(b"f\0o");
Path::new("test").push(&b"f\0o"[..]);
}).join();
assert!(result.is_err());
}
@ -553,11 +553,11 @@ mod tests {
)
}
t!("foo", display, "foo");
t!(b"foo\x80", display, "foo\u{FFFD}");
t!(b"foo\xFFbar", display, "foo\u{FFFD}bar");
t!(b"foo\xFF/bar", filename_display, "bar");
t!(b"foo/\xFFbar", filename_display, "\u{FFFD}bar");
t!(b"/", filename_display, "");
t!(&b"foo\x80"[..], display, "foo\u{FFFD}");
t!(&b"foo\xFFbar"[..], display, "foo\u{FFFD}bar");
t!(&b"foo\xFF/bar"[..], filename_display, "bar");
t!(&b"foo/\xFFbar"[..], filename_display, "\u{FFFD}bar");
t!(&b"/"[..], filename_display, "");
macro_rules! t {
($path:expr, $exp:expr) => (
@ -577,11 +577,11 @@ mod tests {
}
t!("foo", "foo");
t!(b"foo\x80", "foo\u{FFFD}");
t!(b"foo\xFFbar", "foo\u{FFFD}bar");
t!(b"foo\xFF/bar", "bar", filename);
t!(b"foo/\xFFbar", "\u{FFFD}bar", filename);
t!(b"/", "", filename);
t!(&b"foo\x80"[..], "foo\u{FFFD}");
t!(&b"foo\xFFbar"[..], "foo\u{FFFD}bar");
t!(&b"foo\xFF/bar"[..], "bar", filename);
t!(&b"foo/\xFFbar"[..], "\u{FFFD}bar", filename);
t!(&b"/"[..], "", filename);
}
#[test]
@ -598,13 +598,13 @@ mod tests {
)
}
t!(b"foo", "foo", "foo");
t!(b"foo/bar", "foo/bar", "bar");
t!(b"/", "/", "");
t!(b"foo\xFF", "foo\u{FFFD}", "foo\u{FFFD}");
t!(b"foo\xFF/bar", "foo\u{FFFD}/bar", "bar");
t!(b"foo/\xFFbar", "foo/\u{FFFD}bar", "\u{FFFD}bar");
t!(b"\xFFfoo/bar\xFF", "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}");
t!(&b"foo"[..], "foo", "foo");
t!(&b"foo/bar"[..], "foo/bar", "bar");
t!(&b"/"[..], "/", "");
t!(&b"foo\xFF"[..], "foo\u{FFFD}", "foo\u{FFFD}");
t!(&b"foo\xFF/bar"[..], "foo\u{FFFD}/bar", "bar");
t!(&b"foo/\xFFbar"[..], "foo/\u{FFFD}bar", "\u{FFFD}bar");
t!(&b"\xFFfoo/bar\xFF"[..], "\u{FFFD}foo/bar\u{FFFD}", "bar\u{FFFD}");
}
#[test]
@ -632,9 +632,9 @@ mod tests {
);
}
t!(v: b"a/b/c", filename, Some(b"c"));
t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
t!(v: b"a/b\xFF/c", filename, Some(b"c"));
t!(v: &b"a/b/c"[..], filename, Some(&b"c"[..]));
t!(v: &b"a/b/c\xFF"[..], filename, Some(&b"c\xFF"[..]));
t!(v: &b"a/b\xFF/c"[..], filename, Some(&b"c"[..]));
t!(s: "a/b/c", filename, Some("c"), opt);
t!(s: "/a/b/c", filename, Some("c"), opt);
t!(s: "a", filename, Some("a"), opt);
@ -644,9 +644,9 @@ mod tests {
t!(s: "..", filename, None, opt);
t!(s: "../..", filename, None, opt);
t!(v: b"a/b/c", dirname, b"a/b");
t!(v: b"a/b/c\xFF", dirname, b"a/b");
t!(v: b"a/b\xFF/c", dirname, b"a/b\xFF");
t!(v: &b"a/b/c"[..], dirname, b"a/b");
t!(v: &b"a/b/c\xFF"[..], dirname, b"a/b");
t!(v: &b"a/b\xFF/c"[..], dirname, b"a/b\xFF");
t!(s: "a/b/c", dirname, "a/b");
t!(s: "/a/b/c", dirname, "/a/b");
t!(s: "a", dirname, ".");
@ -656,9 +656,9 @@ mod tests {
t!(s: "..", dirname, "..");
t!(s: "../..", dirname, "../..");
t!(v: b"hi/there.txt", filestem, Some(b"there"));
t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
t!(v: &b"hi/there.txt"[..], filestem, Some(&b"there"[..]));
t!(v: &b"hi/there\x80.txt"[..], filestem, Some(&b"there\x80"[..]));
t!(v: &b"hi/there.t\x80xt"[..], filestem, Some(&b"there"[..]));
t!(s: "hi/there.txt", filestem, Some("there"), opt);
t!(s: "hi/there", filestem, Some("there"), opt);
t!(s: "there.txt", filestem, Some("there"), opt);
@ -672,11 +672,11 @@ mod tests {
t!(s: "..", filestem, None, opt);
t!(s: "../..", filestem, None, opt);
t!(v: b"hi/there.txt", extension, Some(b"txt"));
t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
t!(v: b"hi/there", extension, None);
t!(v: b"hi/there\x80", extension, None);
t!(v: &b"hi/there.txt"[..], extension, Some(&b"txt"[..]));
t!(v: &b"hi/there\x80.txt"[..], extension, Some(&b"txt"[..]));
t!(v: &b"hi/there.t\x80xt"[..], extension, Some(&b"t\x80xt"[..]));
t!(v: &b"hi/there"[..], extension, None);
t!(v: &b"hi/there\x80"[..], extension, None);
t!(s: "hi/there.txt", extension, Some("txt"), opt);
t!(s: "hi/there", extension, None, opt);
t!(s: "there.txt", extension, Some("txt"), opt);
@ -756,9 +756,9 @@ mod tests {
t!(s: "a/b/c", ["d", "/e"], "/e");
t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
t!(v: &b"a/b/c"[..], [&b"d"[..], &b"e"[..]], b"a/b/c/d/e");
t!(v: &b"a/b/c"[..], [&b"d"[..], &b"/e"[..], &b"f"[..]], b"/e/f");
t!(v: &b"a/b/c"[..], [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
}
#[test]
@ -782,15 +782,15 @@ mod tests {
)
}
t!(b: b"a/b/c", b"a/b", true);
t!(b: b"a", b".", true);
t!(b: b".", b".", false);
t!(b: b"/a", b"/", true);
t!(b: b"/", b"/", false);
t!(b: b"a/b/c\x80", b"a/b", true);
t!(b: b"a/b\x80/c", b"a/b\x80", true);
t!(b: b"\xFF", b".", true);
t!(b: b"/\xFF", b"/", true);
t!(b: &b"a/b/c"[..], b"a/b", true);
t!(b: &b"a"[..], b".", true);
t!(b: &b"."[..], b".", false);
t!(b: &b"/a"[..], b"/", true);
t!(b: &b"/"[..], b"/", false);
t!(b: &b"a/b/c\x80"[..], b"a/b", true);
t!(b: &b"a/b\x80/c"[..], b"a/b\x80", true);
t!(b: &b"\xFF"[..], b".", true);
t!(b: &b"/\xFF"[..], b"/", true);
t!(s: "a/b/c", "a/b", true);
t!(s: "a", ".", true);
t!(s: ".", ".", false);
@ -800,15 +800,15 @@ mod tests {
#[test]
fn test_root_path() {
assert_eq!(Path::new(b"a/b/c").root_path(), None);
assert_eq!(Path::new(b"/a/b/c").root_path(), Some(Path::new("/")));
assert_eq!(Path::new(&b"a/b/c"[..]).root_path(), None);
assert_eq!(Path::new(&b"/a/b/c"[..]).root_path(), Some(Path::new("/")));
}
#[test]
fn test_join() {
t!(v: Path::new(b"a/b/c").join(b".."), b"a/b");
t!(v: Path::new(b"/a/b/c").join(b"d"), b"/a/b/c/d");
t!(v: Path::new(b"a/\x80/c").join(b"\xFF"), b"a/\x80/c/\xFF");
t!(v: Path::new(&b"a/b/c"[..]).join(&b".."[..]), b"a/b");
t!(v: Path::new(&b"/a/b/c"[..]).join(&b"d"[..]), b"/a/b/c/d");
t!(v: Path::new(&b"a/\x80/c"[..]).join(&b"\xFF"[..]), b"a/\x80/c/\xFF");
t!(s: Path::new("a/b/c").join(".."), "a/b");
t!(s: Path::new("/a/b/c").join("d"), "/a/b/c/d");
t!(s: Path::new("a/b").join("c/d"), "a/b/c/d");
@ -861,17 +861,17 @@ mod tests {
t!(s: "a/b/c", ["..", "d"], "a/b/d");
t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
t!(v: &b"a/b/c"[..], [&b"d"[..], &b"e"[..]], b"a/b/c/d/e");
t!(v: &b"a/b/c"[..], [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
}
#[test]
fn test_with_helpers() {
let empty: &[u8] = &[];
t!(v: Path::new(b"a/b/c").with_filename(b"d"), b"a/b/d");
t!(v: Path::new(b"a/b/c\xFF").with_filename(b"\x80"), b"a/b/\x80");
t!(v: Path::new(b"/\xFF/foo").with_filename(b"\xCD"),
t!(v: Path::new(&b"a/b/c"[..]).with_filename(&b"d"[..]), b"a/b/d");
t!(v: Path::new(&b"a/b/c\xFF"[..]).with_filename(&b"\x80"[..]), b"a/b/\x80");
t!(v: Path::new(&b"/\xFF/foo"[..]).with_filename(&b"\xCD"[..]),
b"/\xFF/\xCD");
t!(s: Path::new("a/b/c").with_filename("d"), "a/b/d");
t!(s: Path::new(".").with_filename("foo"), "foo");
@ -893,13 +893,13 @@ mod tests {
t!(s: Path::new("..").with_filename(""), "..");
t!(s: Path::new("../..").with_filename(""), "../..");
t!(v: Path::new(b"hi/there\x80.txt").with_extension(b"exe"),
t!(v: Path::new(&b"hi/there\x80.txt"[..]).with_extension(&b"exe"[..]),
b"hi/there\x80.exe");
t!(v: Path::new(b"hi/there.txt\x80").with_extension(b"\xFF"),
t!(v: Path::new(&b"hi/there.txt\x80"[..]).with_extension(&b"\xFF"[..]),
b"hi/there.\xFF");
t!(v: Path::new(b"hi/there\x80").with_extension(b"\xFF"),
t!(v: Path::new(&b"hi/there\x80"[..]).with_extension(&b"\xFF"[..]),
b"hi/there\x80.\xFF");
t!(v: Path::new(b"hi/there.\xFF").with_extension(empty), b"hi/there");
t!(v: Path::new(&b"hi/there.\xFF"[..]).with_extension(empty), b"hi/there");
t!(s: Path::new("hi/there.txt").with_extension("exe"), "hi/there.exe");
t!(s: Path::new("hi/there.txt").with_extension(""), "hi/there");
t!(s: Path::new("hi/there.txt").with_extension("."), "hi/there..");
@ -941,17 +941,17 @@ mod tests {
)
}
t!(v: b"a/b/c", set_filename, with_filename, b"d");
t!(v: b"/", set_filename, with_filename, b"foo");
t!(v: b"\x80", set_filename, with_filename, b"\xFF");
t!(v: &b"a/b/c"[..], set_filename, with_filename, &b"d"[..]);
t!(v: &b"/"[..], set_filename, with_filename, &b"foo"[..]);
t!(v: &b"\x80"[..], set_filename, with_filename, &b"\xFF"[..]);
t!(s: "a/b/c", set_filename, with_filename, "d");
t!(s: "/", set_filename, with_filename, "foo");
t!(s: ".", set_filename, with_filename, "foo");
t!(s: "a/b", set_filename, with_filename, "");
t!(s: "a", set_filename, with_filename, "");
t!(v: b"hi/there.txt", set_extension, with_extension, b"exe");
t!(v: b"hi/there.t\x80xt", set_extension, with_extension, b"exe\xFF");
t!(v: &b"hi/there.txt"[..], set_extension, with_extension, &b"exe"[..]);
t!(v: &b"hi/there.t\x80xt"[..], set_extension, with_extension, &b"exe\xFF"[..]);
t!(s: "hi/there.txt", set_extension, with_extension, "exe");
t!(s: "hi/there.", set_extension, with_extension, "txt");
t!(s: "hi/there", set_extension, with_extension, "txt");
@ -983,10 +983,10 @@ mod tests {
)
}
t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
Some(b"there"), Some(b"\xFF"));
t!(v: Path::new(&b"a/b/c"[..]), Some(&b"c"[..]), b"a/b", Some(&b"c"[..]), None);
t!(v: Path::new(&b"a/b/\xFF"[..]), Some(&b"\xFF"[..]), b"a/b", Some(&b"\xFF"[..]), None);
t!(v: Path::new(&b"hi/there.\xFF"[..]), Some(&b"there.\xFF"[..]), b"hi",
Some(&b"there"[..]), Some(&b"\xFF"[..]));
t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
t!(s: Path::new("."), None, Some("."), None, None);
t!(s: Path::new("/"), None, Some("/"), None, None);
@ -1000,16 +1000,16 @@ mod tests {
t!(s: Path::new("hi/.there"), Some(".there"), Some("hi"), Some(".there"), None);
t!(s: Path::new("hi/..there"), Some("..there"), Some("hi"),
Some("."), Some("there"));
t!(s: Path::new(b"a/b/\xFF"), None, Some("a/b"), None, None);
t!(s: Path::new(b"a/b/\xFF.txt"), None, Some("a/b"), None, Some("txt"));
t!(s: Path::new(b"a/b/c.\x80"), None, Some("a/b"), Some("c"), None);
t!(s: Path::new(b"\xFF/b"), Some("b"), None, Some("b"), None);
t!(s: Path::new(&b"a/b/\xFF"[..]), None, Some("a/b"), None, None);
t!(s: Path::new(&b"a/b/\xFF.txt"[..]), None, Some("a/b"), None, Some("txt"));
t!(s: Path::new(&b"a/b/c.\x80"[..]), None, Some("a/b"), Some("c"), None);
t!(s: Path::new(&b"\xFF/b"[..]), Some("b"), None, Some("b"), None);
}
#[test]
fn test_dir_path() {
t!(v: Path::new(b"hi/there\x80").dir_path(), b"hi");
t!(v: Path::new(b"hi\xFF/there").dir_path(), b"hi\xFF");
t!(v: Path::new(&b"hi/there\x80"[..]).dir_path(), b"hi");
t!(v: Path::new(&b"hi\xFF/there"[..]).dir_path(), b"hi\xFF");
t!(s: Path::new("hi/there").dir_path(), "hi");
t!(s: Path::new("hi").dir_path(), ".");
t!(s: Path::new("/hi").dir_path(), "/");
@ -1107,9 +1107,9 @@ mod tests {
t!(s: "/a/b/c", "d/e/f", false);
t!(s: "a/b/c", "a/b", false);
t!(s: "a/b/c", "b", false);
t!(v: b"a/b/c", b"b/c", true);
t!(v: b"a/b/\xFF", b"\xFF", true);
t!(v: b"a/b/\xFF", b"b/\xFF", true);
t!(v: &b"a/b/c"[..], &b"b/c"[..], true);
t!(v: &b"a/b/\xFF"[..], &b"\xFF"[..], true);
t!(v: &b"a/b/\xFF"[..], &b"b/\xFF"[..], true);
}
#[test]
@ -1185,9 +1185,9 @@ mod tests {
)
}
t!(b: b"a/b/c", [b"a", b"b", b"c"]);
t!(b: b"/\xFF/a/\x80", [b"\xFF", b"a", b"\x80"]);
t!(b: b"../../foo\xCDbar", [b"..", b"..", b"foo\xCDbar"]);
t!(b: &b"a/b/c"[..], [b"a", b"b", b"c"]);
t!(b: &b"/\xFF/a/\x80"[..], [b"\xFF", b"a", b"\x80"]);
t!(b: &b"../../foo\xCDbar"[..], [b"..", b"..", b"foo\xCDbar"]);
t!(s: "a/b/c", ["a", "b", "c"]);
t!(s: "a/b/d", ["a", "b", "d"]);
t!(s: "a/b/cd", ["a", "b", "cd"]);
@ -1217,9 +1217,9 @@ mod tests {
)
}
t!(b: b"a/b/c", [Some("a"), Some("b"), Some("c")]);
t!(b: b"/\xFF/a/\x80", [None, Some("a"), None]);
t!(b: b"../../foo\xCDbar", [Some(".."), Some(".."), None]);
t!(b: &b"a/b/c"[..], [Some("a"), Some("b"), Some("c")]);
t!(b: &b"/\xFF/a/\x80"[..], [None, Some("a"), None]);
t!(b: &b"../../foo\xCDbar"[..], [Some(".."), Some(".."), None]);
// str_components is a wrapper around components, so no need to do
// the full set of tests
}

View file

@ -1217,8 +1217,8 @@ mod tests {
fn test_paths() {
let empty: &[u8] = &[];
t!(v: Path::new(empty), b".");
t!(v: Path::new(b"\\"), b"\\");
t!(v: Path::new(b"a\\b\\c"), b"a\\b\\c");
t!(v: Path::new(&b"\\"[..]), b"\\");
t!(v: Path::new(&b"a\\b\\c"[..]), b"a\\b\\c");
t!(s: Path::new(""), ".");
t!(s: Path::new("\\"), "\\");
@ -1250,8 +1250,8 @@ mod tests {
t!(s: Path::new("foo\\..\\..\\.."), "..\\..");
t!(s: Path::new("foo\\..\\..\\bar"), "..\\bar");
assert_eq!(Path::new(b"foo\\bar").into_vec(), b"foo\\bar");
assert_eq!(Path::new(b"\\foo\\..\\..\\bar").into_vec(), b"\\bar");
assert_eq!(Path::new(&b"foo\\bar"[..]).into_vec(), b"foo\\bar");
assert_eq!(Path::new(&b"\\foo\\..\\..\\bar"[..]).into_vec(), b"\\bar");
t!(s: Path::new("\\\\a"), "\\a");
t!(s: Path::new("\\\\a\\"), "\\a");
@ -1304,9 +1304,9 @@ mod tests {
#[test]
fn test_opt_paths() {
assert!(Path::new_opt(b"foo\\bar\0") == None);
assert!(Path::new_opt(b"foo\\bar\x80") == None);
t!(v: Path::new_opt(b"foo\\bar").unwrap(), b"foo\\bar");
assert!(Path::new_opt(&b"foo\\bar\0"[..]) == None);
assert!(Path::new_opt(&b"foo\\bar\x80"[..]) == None);
t!(v: Path::new_opt(&b"foo\\bar"[..]).unwrap(), b"foo\\bar");
assert!(Path::new_opt("foo\\bar\0") == None);
t!(s: Path::new_opt("foo\\bar").unwrap(), "foo\\bar");
}
@ -1315,17 +1315,17 @@ mod tests {
fn test_null_byte() {
use thread;
let result = thread::spawn(move|| {
Path::new(b"foo/bar\0");
Path::new(&b"foo/bar\0"[..]);
}).join();
assert!(result.is_err());
let result = thread::spawn(move|| {
Path::new("test").set_filename(b"f\0o")
Path::new("test").set_filename(&b"f\0o"[..])
}).join();
assert!(result.is_err());
let result = thread::spawn(move || {
Path::new("test").push(b"f\0o");
Path::new("test").push(&b"f\0o"[..]);
}).join();
assert!(result.is_err());
}
@ -1333,20 +1333,20 @@ mod tests {
#[test]
#[should_panic]
fn test_not_utf8_panics() {
Path::new(b"hello\x80.txt");
Path::new(&b"hello\x80.txt"[..]);
}
#[test]
fn test_display_str() {
let path = Path::new("foo");
assert_eq!(path.display().to_string(), "foo");
let path = Path::new(b"\\");
let path = Path::new(&b"\\"[..]);
assert_eq!(path.filename_display().to_string(), "");
let path = Path::new("foo");
let mo = path.display().as_cow();
assert_eq!(mo, "foo");
let path = Path::new(b"\\");
let path = Path::new(&b"\\"[..]);
let mo = path.filename_display().as_cow();
assert_eq!(mo, "");
}
@ -1397,7 +1397,7 @@ mod tests {
)
}
t!(v: b"a\\b\\c", filename, Some(b"c"));
t!(v: &b"a\\b\\c"[..], filename, Some(&b"c"[..]));
t!(s: "a\\b\\c", filename_str, "c");
t!(s: "\\a\\b\\c", filename_str, "c");
t!(s: "a", filename_str, "a");
@ -1430,7 +1430,7 @@ mod tests {
t!(s: "\\\\.\\", filename_str, None, opt);
t!(s: "\\\\?\\a\\b\\", filename_str, "b");
t!(v: b"a\\b\\c", dirname, b"a\\b");
t!(v: &b"a\\b\\c"[..], dirname, b"a\\b");
t!(s: "a\\b\\c", dirname_str, "a\\b");
t!(s: "\\a\\b\\c", dirname_str, "\\a\\b");
t!(s: "a", dirname_str, ".");
@ -1461,7 +1461,7 @@ mod tests {
t!(s: "\\\\.\\foo", dirname_str, "\\\\.\\foo");
t!(s: "\\\\?\\a\\b\\", dirname_str, "\\\\?\\a");
t!(v: b"hi\\there.txt", filestem, Some(b"there"));
t!(v: &b"hi\\there.txt"[..], filestem, Some(&b"there"[..]));
t!(s: "hi\\there.txt", filestem_str, "there");
t!(s: "hi\\there", filestem_str, "there");
t!(s: "there.txt", filestem_str, "there");
@ -1476,8 +1476,8 @@ mod tests {
t!(s: "..\\..", filestem_str, None, opt);
// filestem is based on filename, so we don't need the full set of prefix tests
t!(v: b"hi\\there.txt", extension, Some(b"txt"));
t!(v: b"hi\\there", extension, None);
t!(v: &b"hi\\there.txt"[..], extension, Some(&b"txt"[..]));
t!(v: &b"hi\\there"[..], extension, None);
t!(s: "hi\\there.txt", extension_str, Some("txt"), opt);
t!(s: "hi\\there", extension_str, None, opt);
t!(s: "there.txt", extension_str, Some("txt"), opt);
@ -1603,9 +1603,9 @@ mod tests {
t!(s: "a\\b\\c", ["d", "\\e"], "\\e");
t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
t!(v: b"a\\b\\c", [b"d", b"\\e", b"f"], b"\\e\\f");
t!(v: b"a\\b\\c", [b"d".to_vec(), b"e".to_vec()],
t!(v: &b"a\\b\\c"[..], [&b"d"[..], &b"e"[..]], b"a\\b\\c\\d\\e");
t!(v: &b"a\\b\\c"[..], [&b"d"[..], &b"\\e"[..], &b"f"[..]], b"\\e\\f");
t!(v: &b"a\\b\\c"[..], [b"d".to_vec(), b"e".to_vec()],
b"a\\b\\c\\d\\e");
}
@ -1637,11 +1637,11 @@ mod tests {
t!(s: ".", ".", false);
t!(s: "\\a", "\\", true);
t!(s: "\\", "\\", false);
t!(b: b"a\\b\\c", b"a\\b", true);
t!(b: b"a", b".", true);
t!(b: b".", b".", false);
t!(b: b"\\a", b"\\", true);
t!(b: b"\\", b"\\", false);
t!(b: &b"a\\b\\c"[..], b"a\\b", true);
t!(b: &b"a"[..], b".", true);
t!(b: &b"."[..], b".", false);
t!(b: &b"\\a"[..], b"\\", true);
t!(b: &b"\\"[..], b"\\", false);
t!(s: "C:\\a\\b", "C:\\a", true);
t!(s: "C:\\a", "C:\\", true);
@ -1690,8 +1690,8 @@ mod tests {
t!(s: Path::new("a\\b").join("\\c\\d"), "\\c\\d");
t!(s: Path::new(".").join("a\\b"), "a\\b");
t!(s: Path::new("\\").join("a\\b"), "\\a\\b");
t!(v: Path::new(b"a\\b\\c").join(b".."), b"a\\b");
t!(v: Path::new(b"\\a\\b\\c").join(b"d"), b"\\a\\b\\c\\d");
t!(v: Path::new(&b"a\\b\\c"[..]).join(&b".."[..]), b"a\\b");
t!(v: Path::new(&b"\\a\\b\\c"[..]).join(&b"d"[..]), b"\\a\\b\\c\\d");
// full join testing is covered under test_push_path, so no need for
// the full set of prefix tests
}
@ -1742,8 +1742,8 @@ mod tests {
t!(s: "a\\b\\c", ["..", "d"], "a\\b\\d");
t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
t!(v: b"a\\b\\c", [b"d".to_vec(), b"e".to_vec()],
t!(v: &b"a\\b\\c"[..], [&b"d"[..], &b"e"[..]], b"a\\b\\c\\d\\e");
t!(v: &b"a\\b\\c"[..], [b"d".to_vec(), b"e".to_vec()],
b"a\\b\\c\\d\\e");
}
@ -1855,15 +1855,15 @@ mod tests {
)
}
t!(v: b"a\\b\\c", set_filename, with_filename, b"d");
t!(v: b"\\", set_filename, with_filename, b"foo");
t!(v: &b"a\\b\\c"[..], set_filename, with_filename, &b"d"[..]);
t!(v: &b"\\"[..], set_filename, with_filename, &b"foo"[..]);
t!(s: "a\\b\\c", set_filename, with_filename, "d");
t!(s: "\\", set_filename, with_filename, "foo");
t!(s: ".", set_filename, with_filename, "foo");
t!(s: "a\\b", set_filename, with_filename, "");
t!(s: "a", set_filename, with_filename, "");
t!(v: b"hi\\there.txt", set_extension, with_extension, b"exe");
t!(v: &b"hi\\there.txt"[..], set_extension, with_extension, &b"exe"[..]);
t!(s: "hi\\there.txt", set_extension, with_extension, "exe");
t!(s: "hi\\there.", set_extension, with_extension, "txt");
t!(s: "hi\\there", set_extension, with_extension, "txt");
@ -1898,7 +1898,7 @@ mod tests {
)
}
t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), None);
t!(v: Path::new(&b"a\\b\\c"[..]), Some(&b"c"[..]), b"a\\b", Some(&b"c"[..]), None);
t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None);
t!(s: Path::new("."), None, Some("."), None, None);
t!(s: Path::new("\\"), None, Some("\\"), None, None);
@ -2240,7 +2240,7 @@ mod tests {
);
}
t!(s: b"a\\b\\c", ["a", "b", "c"]);
t!(s: &b"a\\b\\c"[..], ["a", "b", "c"]);
t!(s: "a\\b\\c", ["a", "b", "c"]);
t!(s: "a\\b\\d", ["a", "b", "d"]);
t!(s: "a\\b\\cd", ["a", "b", "cd"]);

View file

@ -13,9 +13,13 @@
static FOO: u8 = b'\xF0';
static BAR: &'static [u8] = b"a\xF0\t";
static BAR_FIXED: &'static [u8; 3] = b"a\xF0\t";
static BAZ: &'static [u8] = br"a\n";
pub fn main() {
let bar: &'static [u8] = b"a\xF0\t";
let bar_fixed: &'static [u8; 3] = b"a\xF0\t";
assert_eq!(b'a', 97u8);
assert_eq!(b'\n', 10u8);
assert_eq!(b'\r', 13u8);
@ -44,8 +48,11 @@ pub fn main() {
b", expected);
let expected: &[_] = &[97u8, 240u8, 9u8];
assert_eq!(BAR, expected);
assert_eq!(BAR_FIXED, expected);
assert_eq!(bar, expected);
assert_eq!(bar_fixed, expected);
let val: &[_] = &[97u8, 10u8];
let val = &[97u8, 10u8];
match val {
b"a\n" => {},
_ => panic!(),
@ -53,9 +60,9 @@ pub fn main() {
let buf = vec!(97u8, 98, 99, 100);
assert_eq!(match &buf[0..3] {
b"def" => 1_usize,
b"abc" => 2_usize,
_ => 3_usize
b"def" => 1,
b"abc" => 2,
_ => 3
}, 2);
let expected: &[_] = &[97u8, 92u8, 110u8];

View file

@ -0,0 +1,25 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
const X1: &'static [u8] = &[b'1'];
const X2: &'static [u8] = b"1";
const X3: &'static [u8; 1] = &[b'1'];
const X4: &'static [u8; 1] = b"1";
static Y1: u8 = X1[0];
static Y2: u8 = X2[0];
static Y3: u8 = X3[0];
static Y4: u8 = X4[0];
fn main() {
assert_eq!(Y1, Y2);
assert_eq!(Y1, Y3);
assert_eq!(Y1, Y4);
}

View file

@ -32,11 +32,11 @@ fn rename_directory() {
/* Write the temp input file */
let fromp = CString::new(test_file.as_vec()).unwrap();
let modebuf = CString::new(b"w+b").unwrap();
let modebuf = CString::new(&b"w+b"[..]).unwrap();
let ostream = libc::fopen(fromp.as_ptr(), modebuf.as_ptr());
assert!((ostream as uint != 0));
let s = "hello".to_string();
let buf = CString::new(b"hello").unwrap();
let buf = CString::new(&b"hello"[..]).unwrap();
let write_len = libc::fwrite(buf.as_ptr() as *mut _,
1_usize as libc::size_t,
(s.len() + 1_usize) as libc::size_t,

View file

@ -28,11 +28,11 @@ pub fn main() {
unsafe {
// Call with just the named parameter
let c = CString::new(b"Hello World\n").unwrap();
let c = CString::new(&b"Hello World\n"[..]).unwrap();
check("Hello World\n", |s| sprintf(s, c.as_ptr()));
// Call with variable number of arguments
let c = CString::new(b"%d %f %c %s\n").unwrap();
let c = CString::new(&b"%d %f %c %s\n"[..]).unwrap();
check("42 42.500000 a %d %f %c %s\n\n", |s| {
sprintf(s, c.as_ptr(), 42, 42.5f64, 'a' as c_int, c.as_ptr());
});
@ -43,11 +43,11 @@ pub fn main() {
// A function that takes a function pointer
unsafe fn call(p: unsafe extern fn(*mut c_char, *const c_char, ...) -> c_int) {
// Call with just the named parameter
let c = CString::new(b"Hello World\n").unwrap();
let c = CString::new(&b"Hello World\n"[..]).unwrap();
check("Hello World\n", |s| sprintf(s, c.as_ptr()));
// Call with variable number of arguments
let c = CString::new(b"%d %f %c %s\n").unwrap();
let c = CString::new(&b"%d %f %c %s\n"[..]).unwrap();
check("42 42.500000 a %d %f %c %s\n\n", |s| {
sprintf(s, c.as_ptr(), 42, 42.5f64, 'a' as c_int, c.as_ptr());
});