Skip to content

Add FromIterator impls for ascii::Chars to Strings #141445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,28 @@ impl<'a> FromIterator<Cow<'a, str>> for String {
}
}

#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "ascii_char", issue = "110998")]
impl FromIterator<core::ascii::Char> for String {
fn from_iter<T: IntoIterator<Item = core::ascii::Char>>(iter: T) -> Self {
let buf = iter.into_iter().map(core::ascii::Char::to_u8).collect();
// SAFETY: `buf` is guaranteed to be valid UTF-8 because the `core::ascii::Char` type
// only contains ASCII values (0x00-0x7F), which are valid UTF-8.
unsafe { String::from_utf8_unchecked(buf) }
}
}

#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "ascii_char", issue = "110998")]
impl<'a> FromIterator<&'a core::ascii::Char> for String {
fn from_iter<T: IntoIterator<Item = &'a core::ascii::Char>>(iter: T) -> Self {
let buf = iter.into_iter().copied().map(core::ascii::Char::to_u8).collect();
// SAFETY: `buf` is guaranteed to be valid UTF-8 because the `core::ascii::Char` type
// only contains ASCII values (0x00-0x7F), which are valid UTF-8.
unsafe { String::from_utf8_unchecked(buf) }
}
}

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "rust1", since = "1.0.0")]
impl Extend<char> for String {
Expand Down Expand Up @@ -3200,6 +3222,14 @@ impl<'a> FromIterator<String> for Cow<'a, str> {
}
}

#[cfg(not(no_global_oom_handling))]
#[unstable(feature = "ascii_char", issue = "110998")]
impl<'a> FromIterator<core::ascii::Char> for Cow<'a, str> {
fn from_iter<T: IntoIterator<Item = core::ascii::Char>>(it: T) -> Self {
Cow::Owned(FromIterator::from_iter(it))
}
}

#[stable(feature = "from_string_for_vec_u8", since = "1.14.0")]
impl From<String> for Vec<u8> {
/// Converts the given [`String`] to a vector [`Vec`] that holds values of type [`u8`].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ LL | .collect::<String>();
|
= help: the trait `FromIterator<()>` is not implemented for `String`
= help: the following other types implement trait `FromIterator<A>`:
`String` implements `FromIterator<&Char>`
`String` implements `FromIterator<&char>`
`String` implements `FromIterator<&str>`
`String` implements `FromIterator<Box<str, A>>`
`String` implements `FromIterator<Char>`
`String` implements `FromIterator<Cow<'_, str>>`
`String` implements `FromIterator<String>`
`String` implements `FromIterator<char>`
Expand Down
Loading