Skip to content

Commit 9cf53b6

Browse files
committed
Implement Extend<AsciiChar> for String
1 parent 2b41365 commit 9cf53b6

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

alloc/src/string.rs

+26
Original file line numberDiff line numberDiff line change
@@ -2442,6 +2442,32 @@ impl<'a> Extend<Cow<'a, str>> for String {
24422442
}
24432443
}
24442444

2445+
#[cfg(not(no_global_oom_handling))]
2446+
#[unstable(feature = "ascii_char", issue = "110998")]
2447+
impl Extend<core::ascii::Char> for String {
2448+
fn extend<I: IntoIterator<Item = core::ascii::Char>>(&mut self, iter: I) {
2449+
self.vec.extend(iter.into_iter().map(|c| c.to_u8()));
2450+
}
2451+
2452+
#[inline]
2453+
fn extend_one(&mut self, c: core::ascii::Char) {
2454+
self.vec.push(c.to_u8());
2455+
}
2456+
}
2457+
2458+
#[cfg(not(no_global_oom_handling))]
2459+
#[unstable(feature = "ascii_char", issue = "110998")]
2460+
impl<'a> Extend<&'a core::ascii::Char> for String {
2461+
fn extend<I: IntoIterator<Item = &'a core::ascii::Char>>(&mut self, iter: I) {
2462+
self.extend(iter.into_iter().cloned());
2463+
}
2464+
2465+
#[inline]
2466+
fn extend_one(&mut self, c: &'a core::ascii::Char) {
2467+
self.vec.push(c.to_u8());
2468+
}
2469+
}
2470+
24452471
/// A convenience impl that delegates to the impl for `&str`.
24462472
///
24472473
/// # Examples

coretests/tests/ascii_char.rs

+12
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,15 @@ fn test_debug_control() {
2626
assert_eq!(want, format!("{chr:?}"), "byte: {byte}");
2727
}
2828
}
29+
30+
/// Tests Extend implementation for ascii::Char.
31+
#[test]
32+
fn test_extend() {
33+
let mut s = String::from("abc");
34+
s.extend_one(Char::SmallD);
35+
assert_eq!(s, String::from("abcd"));
36+
37+
let mut s = String::from("abc");
38+
s.extend(Char::CapitalA..=Char::CapitalC);
39+
assert_eq!(s, String::from("abcABC"));
40+
}

coretests/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#![feature(duration_constructors)]
2828
#![feature(error_generic_member_access)]
2929
#![feature(exact_size_is_empty)]
30+
#![feature(extend_one)]
3031
#![feature(extern_types)]
3132
#![feature(float_minimum_maximum)]
3233
#![feature(flt2dec)]

0 commit comments

Comments
 (0)