Crate encoded

source ·
Expand description

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!("鷗外");
//                                       ^^^^^^

Macros

Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples
Examples