Skip to content

Commit 1e9bef9

Browse files
committed
Fixed-size byte string literals (RFC 339)
1 parent bfac337 commit 1e9bef9

File tree

10 files changed

+100
-43
lines changed

10 files changed

+100
-43
lines changed

src/librustc_trans/trans/consts.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,7 @@ pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: &ast::Lit)
7575
ast::LitBool(b) => C_bool(cx, b),
7676
ast::LitStr(ref s, _) => C_str_slice(cx, (*s).clone()),
7777
ast::LitBinary(ref data) => {
78-
let g = addr_of(cx, C_bytes(cx, &data[..]), "binary", e.id);
79-
let base = ptrcast(g, Type::i8p(cx));
80-
let prev_const = cx.const_unsized().borrow_mut()
81-
.insert(base, g);
82-
assert!(prev_const.is_none() || prev_const == Some(g));
83-
assert_eq!(abi::FAT_PTR_ADDR, 0);
84-
assert_eq!(abi::FAT_PTR_EXTRA, 1);
85-
C_struct(cx, &[base, C_uint(cx, data.len())], false)
78+
addr_of(cx, C_bytes(cx, &data[..]), "binary", e.id)
8679
}
8780
}
8881
}

src/librustc_typeck/check/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,10 +2505,11 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
25052505

25062506
match lit.node {
25072507
ast::LitStr(..) => ty::mk_str_slice(tcx, tcx.mk_region(ty::ReStatic), ast::MutImmutable),
2508-
ast::LitBinary(..) => {
2509-
ty::mk_slice(tcx,
2510-
tcx.mk_region(ty::ReStatic),
2511-
ty::mt{ ty: tcx.types.u8, mutbl: ast::MutImmutable })
2508+
ast::LitBinary(ref v) => {
2509+
ty::mk_rptr(tcx, tcx.mk_region(ty::ReStatic), ty::mt {
2510+
ty: ty::mk_vec(tcx, tcx.types.u8, Some(v.len())),
2511+
mutbl: ast::MutImmutable,
2512+
})
25122513
}
25132514
ast::LitByte(_) => tcx.types.u8,
25142515
ast::LitChar(_) => tcx.types.char,

src/libstd/ffi/c_str.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,23 @@ impl IntoBytes for Vec<u8> {
451451
fn into_bytes(self) -> Vec<u8> { self }
452452
}
453453

454+
macro_rules! array_impls {
455+
($($N: expr)+) => {
456+
$(
457+
impl<'a> IntoBytes for &'a [u8; $N] {
458+
fn into_bytes(self) -> Vec<u8> { self.to_vec() }
459+
}
460+
)+
461+
}
462+
}
463+
464+
array_impls! {
465+
0 1 2 3 4 5 6 7 8 9
466+
10 11 12 13 14 15 16 17 18 19
467+
20 21 22 23 24 25 26 27 28 29
468+
30 31 32
469+
}
470+
454471
#[cfg(test)]
455472
mod tests {
456473
use prelude::v1::*;

src/libstd/io/buffered.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ mod tests {
622622

623623
#[test]
624624
fn test_read_line() {
625-
let in_buf = b"a\nb\nc";
625+
let in_buf: &[u8] = b"a\nb\nc";
626626
let mut reader = BufReader::with_capacity(2, in_buf);
627627
let mut s = String::new();
628628
reader.read_line(&mut s).unwrap();
@@ -640,7 +640,7 @@ mod tests {
640640

641641
#[test]
642642
fn test_lines() {
643-
let in_buf = b"a\nb\nc";
643+
let in_buf: &[u8] = b"a\nb\nc";
644644
let reader = BufReader::with_capacity(2, in_buf);
645645
let mut it = reader.lines();
646646
assert_eq!(it.next(), Some(Ok("a".to_string())));

src/libstd/io/cursor.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,23 @@ impl<'a> BufRead for Cursor<&'a mut [u8]> { buffer!(); }
129129
#[stable(feature = "rust1", since = "1.0.0")]
130130
impl<'a> BufRead for Cursor<Vec<u8>> { buffer!(); }
131131

132+
macro_rules! array_impls {
133+
($($N: expr)+) => {
134+
$(
135+
impl<'a> io::Seek for Cursor<&'a [u8; $N]> { seek!(); }
136+
impl<'a> Read for Cursor<&'a [u8; $N]> { read!(); }
137+
impl<'a> BufRead for Cursor<&'a [u8; $N]> { buffer!(); }
138+
)+
139+
}
140+
}
141+
142+
array_impls! {
143+
0 1 2 3 4 5 6 7 8 9
144+
10 11 12 13 14 15 16 17 18 19
145+
20 21 22 23 24 25 26 27 28 29
146+
30 31 32
147+
}
148+
132149
#[stable(feature = "rust1", since = "1.0.0")]
133150
impl<'a> Write for Cursor<&'a mut [u8]> {
134151
fn write(&mut self, data: &[u8]) -> io::Result<usize> {

src/libstd/old_io/util.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,8 @@ mod test {
445445

446446
#[test]
447447
fn limit_reader_buffer() {
448-
let r = &mut b"0123456789\n0123456789\n";
448+
let mut r: &[u8] = b"0123456789\n0123456789\n";
449+
let r = &mut r;
449450
{
450451
let mut r = LimitReader::new(r.by_ref(), 3);
451452
assert_eq!(r.read_line(), Ok("012".to_string()));

src/libstd/old_path/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,26 @@ impl BytesContainer for [u8] {
895895
}
896896
}
897897

898+
macro_rules! array_impls {
899+
($($N: expr)+) => {
900+
$(
901+
impl BytesContainer for [u8; $N] {
902+
#[inline]
903+
fn container_as_bytes(&self) -> &[u8] {
904+
&self[..]
905+
}
906+
}
907+
)+
908+
}
909+
}
910+
911+
array_impls! {
912+
0 1 2 3 4 5 6 7 8 9
913+
10 11 12 13 14 15 16 17 18 19
914+
20 21 22 23 24 25 26 27 28 29
915+
30 31 32
916+
}
917+
898918
impl BytesContainer for Vec<u8> {
899919
#[inline]
900920
fn container_as_bytes(&self) -> &[u8] {

src/libstd/old_path/posix.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,9 @@ mod tests {
632632
);
633633
}
634634

635-
t!(v: b"a/b/c", filename, Some(b"c"));
636-
t!(v: b"a/b/c\xFF", filename, Some(b"c\xFF"));
637-
t!(v: b"a/b\xFF/c", filename, Some(b"c"));
635+
t!(v: b"a/b/c", filename, Some(&b"c"[..]));
636+
t!(v: b"a/b/c\xFF", filename, Some(&b"c\xFF"[..]));
637+
t!(v: b"a/b\xFF/c", filename, Some(&b"c"[..]));
638638
t!(s: "a/b/c", filename, Some("c"), opt);
639639
t!(s: "/a/b/c", filename, Some("c"), opt);
640640
t!(s: "a", filename, Some("a"), opt);
@@ -656,9 +656,9 @@ mod tests {
656656
t!(s: "..", dirname, "..");
657657
t!(s: "../..", dirname, "../..");
658658

659-
t!(v: b"hi/there.txt", filestem, Some(b"there"));
660-
t!(v: b"hi/there\x80.txt", filestem, Some(b"there\x80"));
661-
t!(v: b"hi/there.t\x80xt", filestem, Some(b"there"));
659+
t!(v: b"hi/there.txt", filestem, Some(&b"there"[..]));
660+
t!(v: b"hi/there\x80.txt", filestem, Some(&b"there\x80"[..]));
661+
t!(v: b"hi/there.t\x80xt", filestem, Some(&b"there"[..]));
662662
t!(s: "hi/there.txt", filestem, Some("there"), opt);
663663
t!(s: "hi/there", filestem, Some("there"), opt);
664664
t!(s: "there.txt", filestem, Some("there"), opt);
@@ -672,9 +672,9 @@ mod tests {
672672
t!(s: "..", filestem, None, opt);
673673
t!(s: "../..", filestem, None, opt);
674674

675-
t!(v: b"hi/there.txt", extension, Some(b"txt"));
676-
t!(v: b"hi/there\x80.txt", extension, Some(b"txt"));
677-
t!(v: b"hi/there.t\x80xt", extension, Some(b"t\x80xt"));
675+
t!(v: b"hi/there.txt", extension, Some(&b"txt"[..]));
676+
t!(v: b"hi/there\x80.txt", extension, Some(&b"txt"[..]));
677+
t!(v: b"hi/there.t\x80xt", extension, Some(&b"t\x80xt"[..]));
678678
t!(v: b"hi/there", extension, None);
679679
t!(v: b"hi/there\x80", extension, None);
680680
t!(s: "hi/there.txt", extension, Some("txt"), opt);
@@ -756,8 +756,8 @@ mod tests {
756756
t!(s: "a/b/c", ["d", "/e"], "/e");
757757
t!(s: "a/b/c", ["d", "/e", "f"], "/e/f");
758758
t!(s: "a/b/c", ["d".to_string(), "e".to_string()], "a/b/c/d/e");
759-
t!(v: b"a/b/c", [b"d", b"e"], b"a/b/c/d/e");
760-
t!(v: b"a/b/c", [b"d", b"/e", b"f"], b"/e/f");
759+
t!(v: b"a/b/c", [&b"d"[..], &b"e"[..]], b"a/b/c/d/e");
760+
t!(v: b"a/b/c", [&b"d"[..], &b"/e"[..], &b"f"[..]], b"/e/f");
761761
t!(v: b"a/b/c", [b"d".to_vec(), b"e".to_vec()], b"a/b/c/d/e");
762762
}
763763

@@ -983,10 +983,10 @@ mod tests {
983983
)
984984
}
985985

986-
t!(v: Path::new(b"a/b/c"), Some(b"c"), b"a/b", Some(b"c"), None);
987-
t!(v: Path::new(b"a/b/\xFF"), Some(b"\xFF"), b"a/b", Some(b"\xFF"), None);
988-
t!(v: Path::new(b"hi/there.\xFF"), Some(b"there.\xFF"), b"hi",
989-
Some(b"there"), Some(b"\xFF"));
986+
t!(v: Path::new(b"a/b/c"), Some(&b"c"[..]), b"a/b", Some(&b"c"[..]), None);
987+
t!(v: Path::new(b"a/b/\xFF"), Some(&b"\xFF"[..]), b"a/b", Some(&b"\xFF"[..]), None);
988+
t!(v: Path::new(b"hi/there.\xFF"), Some(&b"there.\xFF"[..]), b"hi",
989+
Some(&b"there"[..]), Some(&b"\xFF"[..]));
990990
t!(s: Path::new("a/b/c"), Some("c"), Some("a/b"), Some("c"), None);
991991
t!(s: Path::new("."), None, Some("."), None, None);
992992
t!(s: Path::new("/"), None, Some("/"), None, None);

src/libstd/old_path/windows.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ mod tests {
13971397
)
13981398
}
13991399

1400-
t!(v: b"a\\b\\c", filename, Some(b"c"));
1400+
t!(v: b"a\\b\\c", filename, Some(&b"c"[..]));
14011401
t!(s: "a\\b\\c", filename_str, "c");
14021402
t!(s: "\\a\\b\\c", filename_str, "c");
14031403
t!(s: "a", filename_str, "a");
@@ -1461,7 +1461,7 @@ mod tests {
14611461
t!(s: "\\\\.\\foo", dirname_str, "\\\\.\\foo");
14621462
t!(s: "\\\\?\\a\\b\\", dirname_str, "\\\\?\\a");
14631463

1464-
t!(v: b"hi\\there.txt", filestem, Some(b"there"));
1464+
t!(v: b"hi\\there.txt", filestem, Some(&b"there"[..]));
14651465
t!(s: "hi\\there.txt", filestem_str, "there");
14661466
t!(s: "hi\\there", filestem_str, "there");
14671467
t!(s: "there.txt", filestem_str, "there");
@@ -1476,7 +1476,7 @@ mod tests {
14761476
t!(s: "..\\..", filestem_str, None, opt);
14771477
// filestem is based on filename, so we don't need the full set of prefix tests
14781478

1479-
t!(v: b"hi\\there.txt", extension, Some(b"txt"));
1479+
t!(v: b"hi\\there.txt", extension, Some(&b"txt"[..]));
14801480
t!(v: b"hi\\there", extension, None);
14811481
t!(s: "hi\\there.txt", extension_str, Some("txt"), opt);
14821482
t!(s: "hi\\there", extension_str, None, opt);
@@ -1603,8 +1603,8 @@ mod tests {
16031603
t!(s: "a\\b\\c", ["d", "\\e"], "\\e");
16041604
t!(s: "a\\b\\c", ["d", "\\e", "f"], "\\e\\f");
16051605
t!(s: "a\\b\\c", ["d".to_string(), "e".to_string()], "a\\b\\c\\d\\e");
1606-
t!(v: b"a\\b\\c", [b"d", b"e"], b"a\\b\\c\\d\\e");
1607-
t!(v: b"a\\b\\c", [b"d", b"\\e", b"f"], b"\\e\\f");
1606+
t!(v: b"a\\b\\c", [&b"d"[..], &b"e"[..]], b"a\\b\\c\\d\\e");
1607+
t!(v: b"a\\b\\c", [&b"d"[..], &b"\\e"[..], &b"f"[..]], b"\\e\\f");
16081608
t!(v: b"a\\b\\c", [b"d".to_vec(), b"e".to_vec()],
16091609
b"a\\b\\c\\d\\e");
16101610
}
@@ -1898,7 +1898,7 @@ mod tests {
18981898
)
18991899
}
19001900

1901-
t!(v: Path::new(b"a\\b\\c"), Some(b"c"), b"a\\b", Some(b"c"), None);
1901+
t!(v: Path::new(b"a\\b\\c"), Some(&b"c"[..]), b"a\\b", Some(&b"c"[..]), None);
19021902
t!(s: Path::new("a\\b\\c"), Some("c"), Some("a\\b"), Some("c"), None);
19031903
t!(s: Path::new("."), None, Some("."), None, None);
19041904
t!(s: Path::new("\\"), None, Some("\\"), None, None);

src/test/run-pass/byte-literals.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313

1414
static FOO: u8 = b'\xF0';
1515
static BAR: &'static [u8] = b"a\xF0\t";
16+
static BAR_FIXED: &'static [u8; 3] = b"a\xF0\t";
1617
static BAZ: &'static [u8] = br"a\n";
1718

1819
pub fn main() {
20+
let bar: &'static [u8] = b"a\xF0\t";
21+
let bar_fixed: &'static [u8; 3] = b"a\xF0\t";
22+
1923
assert_eq!(b'a', 97u8);
2024
assert_eq!(b'\n', 10u8);
2125
assert_eq!(b'\r', 13u8);
@@ -44,19 +48,23 @@ pub fn main() {
4448
b", expected);
4549
let expected: &[_] = &[97u8, 240u8, 9u8];
4650
assert_eq!(BAR, expected);
51+
assert_eq!(BAR_FIXED, expected);
52+
assert_eq!(bar, expected);
53+
assert_eq!(bar_fixed, expected);
4754

48-
let val: &[_] = &[97u8, 10u8];
55+
let val = &[97u8, 10u8];
4956
match val {
5057
b"a\n" => {},
5158
_ => panic!(),
5259
}
5360

54-
let buf = vec!(97u8, 98, 99, 100);
55-
assert_eq!(match &buf[0..3] {
56-
b"def" => 1_usize,
57-
b"abc" => 2_usize,
58-
_ => 3_usize
59-
}, 2);
61+
// FIXME: There are no DST coercions &[T; N] -> &[T] in patterns
62+
// let buf = vec!(97u8, 98, 99, 100);
63+
// assert_eq!(match &buf[0..3] {
64+
// b"def" => 1_usize,
65+
// b"abc" => 2_usize,
66+
// _ => 3_usize
67+
// }, 2);
6068

6169
let expected: &[_] = &[97u8, 92u8, 110u8];
6270
assert_eq!(BAZ, expected);

0 commit comments

Comments
 (0)