Skip to content

Commit aba44c1

Browse files
committed
Merge branch 'byte-concat'
2 parents 3ab0214 + 3fdc142 commit aba44c1

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

text/2509-byte-concat.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
- Feature Name: concat_bytes
2+
- Start Date: 2018-07-31
3+
- RFC PR: [#2509](https://github.com/rust-lang/rfcs/pull/2509)
4+
- Rust Issue: [#87555](https://github.com/rust-lang/rust/issues/87555)
5+
6+
# Summary
7+
[summary]: #summary
8+
9+
Add a macro `concat_bytes!()` to join byte sequences onto an `u8` array,
10+
the same way `concat!()` currently supports for `str` literals.
11+
12+
# Motivation
13+
[motivation]: #motivation
14+
15+
`concat!()` is convenient and useful to create compile time `str` literals
16+
from `str`, `bool`, numeric and `char` literals in the code. This RFC adds an
17+
equivalent capability for `[u8]` instead of `str`.
18+
19+
# Guide-level explanation
20+
[guide-level-explanation]: #guide-level-explanation
21+
22+
The `concat_bytes!()` macro concatenates literals into a byte string literal
23+
(an expression of the type `&[u8; N]`). The following literal types are
24+
supported as inputs:
25+
26+
- byte string literals (`b"..."`)
27+
- byte literals (`b'b'`)
28+
- numeric array literals – if any literal is outside of `u8` range, it will
29+
cause a compile time error:
30+
31+
```
32+
error: cannot concatenate a non-`u8` literal in a byte string literal
33+
--> $FILE:XX:YY
34+
|
35+
XX | concat_bytes!([300, 1, 2, 256], b"val");
36+
| ^^^ ^^^ this value is larger than `255`
37+
| |
38+
| this value is larger than `255`
39+
```
40+
41+
For example, `concat_bytes!(42, b"va", b'l', [1, 2])` evaluates to
42+
`[42, 118, 97, 108, 1, 2]`.
43+
44+
# Drawbacks
45+
[drawbacks]: #drawbacks
46+
47+
None known.
48+
49+
# Rationale and alternatives
50+
[rationale-and-alternatives]: #rationale-and-alternatives
51+
52+
`concat!` could instead be changed to sometimes produce byte literals instead of
53+
string literals, like a previous revision of this RFC proposed. This would make
54+
it hard to ensure the right output type is produced – users would have to use
55+
hacks like adding a dummy `b""` argument to force a byte literal output.
56+
57+
An earlier version of this RFC proposed to support integer literals outside of
58+
arrays, but that was rejected since it would make the output of
59+
`byte_concat!(123, b"\n")` inconsistent with the equivalent `concat!`
60+
invocation.
61+
62+
# Unresolved questions
63+
[unresolved-questions]: #unresolved-questions
64+
65+
- Should additional literal types be supported? Byte string literals are
66+
basically the same thing as byte slice references, so it might make sense to
67+
support those as well (support `&[0, 1, 2]` in addition to `[0, 1, 2]`).
68+
- What to do with string and character literals? They could either be supported
69+
with their underlying UTF-8 representation being concatenated, or rejected.

0 commit comments

Comments
 (0)