konst/chr.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
//! Const equivalents of `char` functions.
//!
//! The module is called `chr` to avoid name collisions with the `char` type.
/// A char encoded as a utf8 string.
///
/// # Example
///
/// ```rust
/// use konst::chr;
///
/// const ENC: &chr::Utf8Encoded = &chr::encode_utf8('û');
/// const ENC_STR: &str = ENC.as_str();
///
/// assert_eq!(ENC_STR, "û");
///
/// ```
pub use konst_kernel::chr::Utf8Encoded;
/// Encodes `c` into utf8, const analog of [`char::encode_utf8`].
///
/// # Const stabilization
///
/// The equivalent std function was const-stabilized in Rust 1.83.0.
///
/// # Example
///
/// ```rust
/// use konst::chr;
///
/// const ENC: &chr::Utf8Encoded = &chr::encode_utf8('🤔');
/// const ENC_STR: &str = ENC.as_str();
///
/// assert_eq!(ENC_STR, "🤔");
///
/// ```
pub use konst_kernel::chr::encode_utf8;
/// Unsafely coerces `u32` to `char`,
/// const equivalent of [`char::from_u32_unchecked`]
///
/// # Const stabilization
///
/// The equivalent std function was const-stabilized in Rust 1.82.0.
///
/// # Safety
///
/// The input `u32` must be within either of these ranges:
///
/// - `0..=0xD7FF`
/// - `0xE000..=0x10FFFF`
///
/// # Example
///
/// ```rust
/// use konst::chr;
///
/// const AT: char = unsafe { chr::from_u32_unchecked(64) };
///
/// assert_eq!(AT, '@');
/// ```
pub use konst_kernel::chr::from_u32_unchecked;
/// Fallible conversion from `u32` to `char`,
/// const equivalent of [`char::from_u32`]
///
/// # Const stabilization
///
/// The equivalent std function was const-stabilized in Rust 1.67.0.
///
/// # Example
///
/// ```rust
/// use konst::{chr, option};
///
/// const AT: char = option::unwrap!(chr::from_u32(64));
///
/// assert_eq!(AT, '@');
/// ```
pub use konst_kernel::chr::from_u32;