U+ 1.0.0
[u-plus] / src / lib.rs
1 // 99 characters, I guess I can’t add any more feature flags. Just as well I have all those I need.
2 #![feature(const_trait_impl, const_char_convert, const_option, const_fn_floating_point_arithmetic)]
3 #![no_std]
4 // What do you want *docs* for!? (But I will begrudgingly allow doctests to run.)
5 #![cfg(any(not(doc), doctest))]
6
7 use core::ops::Add;
8
9 pub struct U;
10
11 impl const Add<u32> for U {
12 type Output = char;
13
14 fn add(self, n: u32) -> char {
15 char::from_u32(
16 n % 10 +
17 (n / 10) % 10 * 0x10 +
18 (n / 100) % 10 * 0x100 +
19 (n / 1000) % 10 * 0x1000 +
20 (n / 10000) % 10 * 0x10000 +
21 (n / 100000) % 10 * 0x100000
22 ).expect("I expected better of you. Depart, and reflect upon your transgressions.")
23 }
24 }
25
26 impl const Add<f32> for U {
27 type Output = char;
28
29 fn add(self, f: f32) -> char {
30 let n = f as u32;
31 if f != (n as f32) {
32 panic!("Begone, fractionaliser of U+ literals!");
33 }
34 char::from_u32(
35 0xf32 +
36 n % 10 * 0x1000 +
37 (n / 10) % 10 * 0x10000 +
38 (n / 100) % 10 * 0x100000
39 ).expect("Will you never be better?")
40 }
41 }
42
43 impl const Add<f64> for U {
44 type Output = char;
45
46 fn add(self, f: f64) -> char {
47 let n = f as u32;
48 if f != (n as f64) {
49 panic!("Get ye hence, fracticious one!");
50 }
51 char::from_u32(
52 0xf64 +
53 n % 10 * 0x1000 +
54 (n / 10) % 10 * 0x10000 +
55 (n / 100) % 10 * 0x100000
56 ).expect("I am exceedingly wrothful to youwards.")
57 }
58 }
59
60 #[test]
61 fn success() {
62 assert_eq!(U+0000, '\u{0}');
63 assert_eq!(U+1234, '\u{1234}');
64 assert_eq!(U+2f64, '\u{2f64}');
65 assert_eq!(U+102f64, '\u{102f64}');
66 assert_eq!(U+104f32, '\u{104f32}');
67 // Evaluated at const time!
68 const MAX: char = U+109999;
69 assert_eq!(MAX, '\u{109999}');
70 }
71
72 /// ```rust
73 /// #![feature(const_trait_impl)] // Not sure quite why this is needed, but meh.
74 /// use u_plus::U;
75 /// const HAPPINESS: char = U+2323;
76 /// ```
77 ///
78 /// ```rust,compile_fail
79 /// #![feature(const_trait_impl)]
80 /// use u_plus::U;
81 /// const MISERY: char = U+119999;
82 /// ```
83 ///
84 /// ```rust,compile_fail
85 /// #![feature(const_trait_impl)]
86 /// use u_plus::U;
87 /// const SORROW: char = U+1F622;
88 /// ```
89 #[cfg(doctest)]
90 const _: () = ();