Skip to content

Commit a36db0b

Browse files
committed
Add section to the book explaining the why and how of hex encoding.
1 parent 072d481 commit a36db0b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

book/src/interning.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,19 @@ static SYM: u8 = 0;
7373

7474
The next thing to note is that each interned string symbol is one byte in size (because `static SYM` has type `u8`).
7575
Thanks to this the addresses of the symbols are consecutive: 0, 1, 2, etc.
76+
77+
## Encoding
78+
79+
Storing strings as-is in symbol names can cause compatibility problems, since it can contain any arbitrary character. For example:
80+
81+
- The `'@'` character can't be used in symbol names because it's reserved for denoting symbol versions.
82+
- The double-quote character `'"'` causes issues with escaping if you use it with `sym` inside an `asm!()` call.
83+
84+
To deal with this, as of Wire Format Version 5, strings are encoded to bytes as UTF-8, and then the bytes are hex-encoded.
85+
The symbol is prefixed with `__defmt_hex_` to denote it's is hex-encoded, and to allow for future expansion.
86+
87+
88+
``` rust
89+
#[export_name = "__defmt_hex_55534220636f6e74726f6c6c6572206973207265616479"]
90+
static SYM: u8 = 0;
91+
```

0 commit comments

Comments
 (0)