3 releases

0.4.2 Dec 7, 2022
0.4.1 Dec 5, 2022
0.4.0 Dec 4, 2022

#55 in #character-encoding

MIT/Apache

14KB
143 lines

encoded

Rust macros converting character encodings at compile time

install

cargo add encoded

usage

const BYTES: &[u8] = encoded::shift_jis!("漢字");
assert_eq!(BYTES, b"\x8a\xbf\x8e\x9a");

For more information, see the documentation.


lib.rs:

Macros converting character encodings at compile time which reduce runtime processing costs and binary size.

Encoding definitions depend on the encoding_rs crate.

Examples

Convert a string literal into encoded bytes array:

const BYTES: &[u8] = encoded::shift_jis!("漢字");
assert_eq!(BYTES, b"\x8a\xbf\x8e\x9a");

Can also be used with std::io::Write:

use std::io::{Cursor, Write};

let mut buff = Cursor::new(Vec::new());
buff.write_all(encoded::shift_jis!("漢字"));
assert_eq!(buff.get_ref(), b"\x8a\xbf\x8e\x9a");

Compile Errors

Argument must be a literal:

const KANJI: &str = "漢字";
const BYTES: &[u8] = encoded::shift_jis!(KANJI);
//                                       ^^^^^

Any unmappable characters result a compile error:

const BYTES: &[u8] = encoded::shift_jis!("鷗外");
//                                       ^^^^^^

Dependencies

~3.5MB
~123K SLoC