Skip to content

Commit 7f107dd

Browse files
tormolThomas Bahn
authored andcommitted
Implement conversions between AsciiString and Cow<'a, AsciiStr>
Closes #43
1 parent 6b205d3 commit 7f107dd

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/ascii_string.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![cfg_attr(rustfmt, rustfmt_skip)]
22

33
use std::{fmt, mem};
4-
use std::borrow::Borrow;
4+
use std::borrow::{Borrow, Cow};
55
use std::error::Error;
66
use std::any::Any;
77
use std::str::FromStr;
@@ -449,6 +449,24 @@ impl Into<String> for AsciiString {
449449
}
450450
}
451451

452+
impl<'a> From<Cow<'a,AsciiStr>> for AsciiString {
453+
fn from(cow: Cow<'a,AsciiStr>) -> AsciiString {
454+
cow.into_owned()
455+
}
456+
}
457+
458+
impl From<AsciiString> for Cow<'static,AsciiStr> {
459+
fn from(string: AsciiString) -> Cow<'static,AsciiStr> {
460+
Cow::Owned(string)
461+
}
462+
}
463+
464+
impl<'a> From<&'a AsciiStr> for Cow<'a,AsciiStr> {
465+
fn from(s: &'a AsciiStr) -> Cow<'a,AsciiStr> {
466+
Cow::Borrowed(s)
467+
}
468+
}
469+
452470
impl AsRef<AsciiStr> for AsciiString {
453471
#[inline]
454472
fn as_ref(&self) -> &AsciiStr {
@@ -524,6 +542,14 @@ impl<'a> FromIterator<&'a AsciiStr> for AsciiString {
524542
}
525543
}
526544

545+
impl<'a> FromIterator<Cow<'a, AsciiStr>> for AsciiString {
546+
fn from_iter<I: IntoIterator<Item = Cow<'a, AsciiStr>>>(iter: I) -> AsciiString {
547+
let mut buf = AsciiString::new();
548+
buf.extend(iter);
549+
buf
550+
}
551+
}
552+
527553
impl Extend<AsciiChar> for AsciiString {
528554
fn extend<I: IntoIterator<Item = AsciiChar>>(&mut self, iterable: I) {
529555
let iterator = iterable.into_iter();
@@ -552,6 +578,17 @@ impl<'a> Extend<&'a AsciiStr> for AsciiString {
552578
}
553579
}
554580

581+
impl<'a> Extend<Cow<'a, AsciiStr>> for AsciiString {
582+
fn extend<I: IntoIterator<Item = Cow<'a,AsciiStr>>>(&mut self, iterable: I) {
583+
let iterator = iterable.into_iter();
584+
let (lower_bound, _) = iterator.size_hint();
585+
self.reserve(lower_bound);
586+
for s in iterator {
587+
self.push_str(&*s);
588+
}
589+
}
590+
}
591+
555592
impl<'a> Add<&'a AsciiStr> for AsciiString {
556593
type Output = AsciiString;
557594

tests.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,31 @@ fn compare_ascii_string_slice() {
111111
assert_eq!(&b[..2], &c[..]);
112112
assert_eq!(c[1].as_char(), 'b');
113113
}
114+
115+
#[test]
116+
#[cfg(feature = "std")]
117+
fn extend_from_iterator() {
118+
use ::std::borrow::Cow;
119+
120+
let abc = "abc".as_ascii_str().unwrap();
121+
let mut s = abc.chars().cloned().collect::<AsciiString>();
122+
assert_eq!(s, abc);
123+
s.extend(abc);
124+
assert_eq!(s, "abcabc");
125+
126+
let lines = "one\ntwo\nthree".as_ascii_str().unwrap().lines();
127+
s.extend(lines);
128+
assert_eq!(s, "abcabconetwothree");
129+
130+
let cows = "ASCII Ascii ascii".as_ascii_str().unwrap()
131+
.split(AsciiChar::Space)
132+
.map(|case| {
133+
if case.chars().all(|a| a.is_uppercase() ) {
134+
Cow::from(case)
135+
} else {
136+
Cow::from(case.to_ascii_uppercase())
137+
}
138+
});
139+
s.extend(cows);
140+
assert_eq!(s, "abcabconetwothreeASCIIASCIIASCII");
141+
}

0 commit comments

Comments
 (0)