@@ -6,97 +6,22 @@ use crate::fmt;
66use crate :: mem:: transmute;
77use crate :: str:: FromStr ;
88
9- /// Converts a `u32` to a `char`.
10- ///
11- /// Note that all [`char`]s are valid [`u32`]s, and can be cast to one with
12- /// `as`:
13- ///
14- /// ```
15- /// let c = '💯';
16- /// let i = c as u32;
17- ///
18- /// assert_eq!(128175, i);
19- /// ```
20- ///
21- /// However, the reverse is not true: not all valid [`u32`]s are valid
22- /// [`char`]s. `from_u32()` will return `None` if the input is not a valid value
23- /// for a [`char`].
24- ///
25- /// For an unsafe version of this function which ignores these checks, see
26- /// [`from_u32_unchecked`].
27- ///
28- /// # Examples
29- ///
30- /// Basic usage:
31- ///
32- /// ```
33- /// use std::char;
34- ///
35- /// let c = char::from_u32(0x2764);
36- ///
37- /// assert_eq!(Some('❤'), c);
38- /// ```
39- ///
40- /// Returning `None` when the input is not a valid [`char`]:
41- ///
42- /// ```
43- /// use std::char;
44- ///
45- /// let c = char::from_u32(0x110000);
46- ///
47- /// assert_eq!(None, c);
48- /// ```
49- #[ doc( alias = "chr" ) ]
9+ /// Converts a `u32` to a `char`. See [`char::from_u32`].
5010#[ must_use]
5111#[ inline]
52- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
53- #[ rustc_const_unstable( feature = "const_char_convert" , issue = "89259" ) ]
54- pub const fn from_u32 ( i : u32 ) -> Option < char > {
12+ pub ( super ) const fn from_u32 ( i : u32 ) -> Option < char > {
5513 // FIXME: once Result::ok is const fn, use it here
5614 match char_try_from_u32 ( i) {
5715 Ok ( c) => Some ( c) ,
5816 Err ( _) => None ,
5917 }
6018}
6119
62- /// Converts a `u32` to a `char`, ignoring validity.
63- ///
64- /// Note that all [`char`]s are valid [`u32`]s, and can be cast to one with
65- /// `as`:
66- ///
67- /// ```
68- /// let c = '💯';
69- /// let i = c as u32;
70- ///
71- /// assert_eq!(128175, i);
72- /// ```
73- ///
74- /// However, the reverse is not true: not all valid [`u32`]s are valid
75- /// [`char`]s. `from_u32_unchecked()` will ignore this, and blindly cast to
76- /// [`char`], possibly creating an invalid one.
77- ///
78- /// # Safety
79- ///
80- /// This function is unsafe, as it may construct invalid `char` values.
81- ///
82- /// For a safe version of this function, see the [`from_u32`] function.
83- ///
84- /// # Examples
85- ///
86- /// Basic usage:
87- ///
88- /// ```
89- /// use std::char;
90- ///
91- /// let c = unsafe { char::from_u32_unchecked(0x2764) };
92- ///
93- /// assert_eq!('❤', c);
94- /// ```
20+ /// Converts a `u32` to a `char`, ignoring validity. See [`char::from_u32_unchecked`].
21+ #[ rustc_const_unstable( feature = "const_char_convert" , issue = "89259" ) ]
9522#[ inline]
9623#[ must_use]
97- #[ stable( feature = "char_from_unchecked" , since = "1.5.0" ) ]
98- #[ rustc_const_unstable( feature = "const_char_convert" , issue = "89259" ) ]
99- pub const unsafe fn from_u32_unchecked ( i : u32 ) -> char {
24+ pub ( super ) const unsafe fn from_u32_unchecked ( i : u32 ) -> char {
10025 // SAFETY: the caller must guarantee that `i` is a valid char value.
10126 if cfg ! ( debug_assertions) { char:: from_u32 ( i) . unwrap ( ) } else { unsafe { transmute ( i) } }
10227}
@@ -317,60 +242,10 @@ impl fmt::Display for CharTryFromError {
317242 }
318243}
319244
320- /// Converts a digit in the given radix to a `char`.
321- ///
322- /// A 'radix' here is sometimes also called a 'base'. A radix of two
323- /// indicates a binary number, a radix of ten, decimal, and a radix of
324- /// sixteen, hexadecimal, to give some common values. Arbitrary
325- /// radices are supported.
326- ///
327- /// `from_digit()` will return `None` if the input is not a digit in
328- /// the given radix.
329- ///
330- /// # Panics
331- ///
332- /// Panics if given a radix larger than 36.
333- ///
334- /// # Examples
335- ///
336- /// Basic usage:
337- ///
338- /// ```
339- /// use std::char;
340- ///
341- /// let c = char::from_digit(4, 10);
342- ///
343- /// assert_eq!(Some('4'), c);
344- ///
345- /// // Decimal 11 is a single digit in base 16
346- /// let c = char::from_digit(11, 16);
347- ///
348- /// assert_eq!(Some('b'), c);
349- /// ```
350- ///
351- /// Returning `None` when the input is not a digit:
352- ///
353- /// ```
354- /// use std::char;
355- ///
356- /// let c = char::from_digit(20, 10);
357- ///
358- /// assert_eq!(None, c);
359- /// ```
360- ///
361- /// Passing a large radix, causing a panic:
362- ///
363- /// ```should_panic
364- /// use std::char;
365- ///
366- /// // this panics
367- /// let c = char::from_digit(1, 37);
368- /// ```
245+ /// Converts a digit in the given radix to a `char`. See [`char::from_digit`].
369246#[ inline]
370247#[ must_use]
371- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
372- #[ rustc_const_unstable( feature = "const_char_convert" , issue = "89259" ) ]
373- pub const fn from_digit ( num : u32 , radix : u32 ) -> Option < char > {
248+ pub ( super ) const fn from_digit ( num : u32 , radix : u32 ) -> Option < char > {
374249 if radix > 36 {
375250 panic ! ( "from_digit: radix is too high (maximum 36)" ) ;
376251 }
0 commit comments