File tree 1 file changed +18
-3
lines changed
1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change @@ -48,7 +48,12 @@ impl From<char> for u32 {
48
48
/// ```
49
49
#[ inline]
50
50
fn from ( c : char ) -> Self {
51
- c as u32
51
+ let c = c as Self ;
52
+
53
+ // SAFETY: `char::MAX` is the highest valid Unicode code point.
54
+ unsafe { core:: hint:: assert_unchecked ( c <= char:: MAX as Self ) } ;
55
+
56
+ c
52
57
}
53
58
}
54
59
@@ -69,7 +74,12 @@ impl From<char> for u64 {
69
74
fn from ( c : char ) -> Self {
70
75
// The char is casted to the value of the code point, then zero-extended to 64 bit.
71
76
// See [https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics]
72
- c as u64
77
+ let c = c as Self ;
78
+
79
+ // SAFETY: `char::MAX` is the highest valid Unicode code point.
80
+ unsafe { core:: hint:: assert_unchecked ( c <= char:: MAX as Self ) } ;
81
+
82
+ c
73
83
}
74
84
}
75
85
@@ -90,7 +100,12 @@ impl From<char> for u128 {
90
100
fn from ( c : char ) -> Self {
91
101
// The char is casted to the value of the code point, then zero-extended to 128 bit.
92
102
// See [https://doc.rust-lang.org/reference/expressions/operator-expr.html#semantics]
93
- c as u128
103
+ let c = c as Self ;
104
+
105
+ // SAFETY: `char::MAX` is the highest valid Unicode code point.
106
+ unsafe { core:: hint:: assert_unchecked ( c <= char:: MAX as Self ) } ;
107
+
108
+ c
94
109
}
95
110
}
96
111
You can’t perform that action at this time.
0 commit comments