Skip to content

Commit 8569bb2

Browse files
committed
Add FromIterator impls for ascii::Chars to Strings
1 parent 52bf0cf commit 8569bb2

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

library/alloc/src/string.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,28 @@ impl<'a> FromIterator<Cow<'a, str>> for String {
23732373
}
23742374
}
23752375

2376+
#[cfg(not(no_global_oom_handling))]
2377+
#[unstable(feature = "ascii_char", issue = "110998")]
2378+
impl FromIterator<core::ascii::Char> for String {
2379+
fn from_iter<T: IntoIterator<Item = core::ascii::Char>>(iter: T) -> Self {
2380+
let buf = iter.into_iter().map(core::ascii::Char::to_u8).collect();
2381+
// SAFETY: `buf` is guaranteed to be valid UTF-8 because the `core::ascii::Char` type
2382+
// only contains ASCII values (0x00-0x7F), which are valid UTF-8.
2383+
unsafe { String::from_utf8_unchecked(buf) }
2384+
}
2385+
}
2386+
2387+
#[cfg(not(no_global_oom_handling))]
2388+
#[unstable(feature = "ascii_char", issue = "110998")]
2389+
impl<'a> FromIterator<&'a core::ascii::Char> for String {
2390+
fn from_iter<T: IntoIterator<Item = &'a core::ascii::Char>>(iter: T) -> Self {
2391+
let buf = iter.into_iter().copied().map(core::ascii::Char::to_u8).collect();
2392+
// SAFETY: `buf` is guaranteed to be valid UTF-8 because the `core::ascii::Char` type
2393+
// only contains ASCII values (0x00-0x7F), which are valid UTF-8.
2394+
unsafe { String::from_utf8_unchecked(buf) }
2395+
}
2396+
}
2397+
23762398
#[cfg(not(no_global_oom_handling))]
23772399
#[stable(feature = "rust1", since = "1.0.0")]
23782400
impl Extend<char> for String {
@@ -3200,6 +3222,14 @@ impl<'a> FromIterator<String> for Cow<'a, str> {
32003222
}
32013223
}
32023224

3225+
#[cfg(not(no_global_oom_handling))]
3226+
#[unstable(feature = "ascii_char", issue = "110998")]
3227+
impl<'a> FromIterator<core::ascii::Char> for Cow<'a, str> {
3228+
fn from_iter<T: IntoIterator<Item = core::ascii::Char>>(it: T) -> Self {
3229+
Cow::Owned(FromIterator::from_iter(it))
3230+
}
3231+
}
3232+
32033233
#[stable(feature = "from_string_for_vec_u8", since = "1.14.0")]
32043234
impl From<String> for Vec<u8> {
32053235
/// Converts the given [`String`] to a vector [`Vec`] that holds values of type [`u8`].

tests/ui/suggestions/semi-suggestion-when-stmt-and-expr-span-equal.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ LL | .collect::<String>();
2121
|
2222
= help: the trait `FromIterator<()>` is not implemented for `String`
2323
= help: the following other types implement trait `FromIterator<A>`:
24+
`String` implements `FromIterator<&Char>`
2425
`String` implements `FromIterator<&char>`
2526
`String` implements `FromIterator<&str>`
2627
`String` implements `FromIterator<Box<str, A>>`
28+
`String` implements `FromIterator<Char>`
2729
`String` implements `FromIterator<Cow<'_, str>>`
2830
`String` implements `FromIterator<String>`
2931
`String` implements `FromIterator<char>`

0 commit comments

Comments
 (0)