| Crates.io | u8lit |
| lib.rs | u8lit |
| version | 0.1.1 |
| created_at | 2025-12-26 06:15:14.122163+00 |
| updated_at | 2025-12-26 06:16:52.662205+00 |
| description | Custom literal to convert strings to UTF-8 bytes. |
| homepage | |
| repository | https://github.com/Chasmical/u8lit-rs |
| max_upload_size | |
| id | 2005308 |
| size | 19,548 |
Custom literal to convert strings to UTF-8 bytes.
'🦈'u8 - characters map to owned arrays [u8; N]."🦄"u8 - strings map to borrowed slices &[u8; N].Integrate it into an existing custom_literal configuration:
use culit::culit;
#[culit]
const SHARK: [u8; 4] = '🦈'u8;
#[culit]
fn main() {
// Unicorn Face (U+1F984)
assert_eq!("🦄"u8, &[0xF0, 0x9F, 0xA6, 0x84]);
}
mod custom_literal {
pub mod string {
pub(crate) use u8lit::string::u8;
}
pub mod character {
pub(crate) use u8lit::character::u8;
}
}
Or use it separately:
use culit::culit;
#[culit(u8lit)]
const SHARK: [u8; 4] = '🦈'u8;
#[culit(u8lit)]
fn main() {
// Unicorn Face (U+1F984)
assert_eq!("🦄"u8, &[0xF0, 0x9F, 0xA6, 0x84]);
}
Rust's byte string literals (b"abc") only work with ASCII input:
const UNICORN: [u8; 4] = *b"🦄";
error: non-ASCII character in byte string literal
--> src\lib.rs:46:29
|
46 | const UNICORN: [u8; 4] = *b"🦄";
| ^^ must be ASCII
|
help: if you meant to use the UTF-8 encoding of '🦄', use \xHH escapes
|
46 - const UNICORN: [u8; 4] = *b"🦄";
46 + const UNICORN: [u8; 4] = *b"\xF0\x9F\xA6\x84";
|
The work-around is to do this, but it's quite verbose:
const UNICORN: [u8; 4] = *"🦄".as_bytes().as_array().unwrap();
culit's custom literals allow a much cleaner look:
#[culit]
const UNICORN: [u8; 4] = *"🦄"u8;
Licensed under either Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.