+
+/// An error which can be returned when parsing a char.
+#[stable(feature = "char_from_str", since = "1.19.0")]
+#[derive(Clone, Debug)]
+pub struct ParseCharError {
+ kind: CharErrorKind,
+}
+
+impl ParseCharError {
+ #[unstable(feature = "char_error_internals",
+ reason = "this method should not be available publicly",
+ issue = "0")]
+ #[doc(hidden)]
+ pub fn __description(&self) -> &str {
+ match self.kind {
+ CharErrorKind::EmptyString => {
+ "cannot parse char from empty string"
+ },
+ CharErrorKind::TooManyChars => "too many characters in string"
+ }
+ }
+}
+
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+enum CharErrorKind {
+ EmptyString,
+ TooManyChars,
+}
+
+#[stable(feature = "char_from_str", since = "1.19.0")]
+impl fmt::Display for ParseCharError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ self.__description().fmt(f)
+ }
+}
+
+
+#[stable(feature = "char_from_str", since = "1.19.0")]
+impl FromStr for char {
+ type Err = ParseCharError;
+
+ #[inline]
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ let mut chars = s.chars();
+ match (chars.next(), chars.next()) {
+ (None, _) => {
+ Err(ParseCharError { kind: CharErrorKind::EmptyString })
+ },
+ (Some(c), None) => Ok(c),
+ _ => {
+ Err(ParseCharError { kind: CharErrorKind::TooManyChars })
+ }
+ }
+ }
+}
+
+