use crate::NonEmptyString;
use std::borrow::Cow;
use std::fmt::Display;
use std::ops::{self, Add, AddAssign};
#[cfg(not(no_global_oom_handling))]
impl Extend<char> for NonEmptyString {
fn extend<I: IntoIterator<Item = char>>(&mut self, iter: I) {
self.0.extend(iter)
}
}
#[cfg(not(no_global_oom_handling))]
impl<'a> Extend<&'a char> for NonEmptyString {
fn extend<I: IntoIterator<Item = &'a char>>(&mut self, iter: I) {
self.extend(iter.into_iter().cloned());
}
}
#[cfg(not(no_global_oom_handling))]
impl<'a> Extend<&'a str> for NonEmptyString {
fn extend<I: IntoIterator<Item = &'a str>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(s));
}
}
#[cfg(not(no_global_oom_handling))]
impl Extend<Box<str>> for NonEmptyString {
fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(&s));
}
}
#[cfg(not(no_global_oom_handling))]
impl Extend<String> for NonEmptyString {
fn extend<I: IntoIterator<Item = String>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.0.push_str(&s));
}
}
#[cfg(not(no_global_oom_handling))]
impl<'a> Extend<Cow<'a, str>> for NonEmptyString {
fn extend<I: IntoIterator<Item = Cow<'a, str>>>(&mut self, iter: I) {
iter.into_iter().for_each(move |s| self.push_str(&s));
}
}
macro_rules! impl_eq {
($lhs:ty, $rhs: ty) => {
#[allow(unused_lifetimes)]
impl<'a, 'b> PartialEq<$rhs> for $lhs {
#[inline]
fn eq(&self, other: &$rhs) -> bool {
PartialEq::eq(&self[..], &other[..])
}
}
#[allow(unused_lifetimes)]
impl<'a, 'b> PartialEq<$lhs> for $rhs {
#[inline]
fn eq(&self, other: &$lhs) -> bool {
PartialEq::eq(&self[..], &other[..])
}
}
};
}
impl_eq! { NonEmptyString, str }
impl_eq! { NonEmptyString, &'a str }
#[cfg(not(no_global_oom_handling))]
impl_eq! { Cow<'a, str>, NonEmptyString }
#[cfg(not(no_global_oom_handling))]
impl_eq! { String, NonEmptyString }
impl Display for NonEmptyString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
#[cfg(not(no_global_oom_handling))]
impl Add<&str> for NonEmptyString {
type Output = NonEmptyString;
#[inline]
fn add(mut self, other: &str) -> NonEmptyString {
self.push_str(other);
self
}
}
#[cfg(not(no_global_oom_handling))]
impl AddAssign<&str> for NonEmptyString {
#[inline]
fn add_assign(&mut self, other: &str) {
self.push_str(other);
}
}
macro_rules! index_impl {
($t:ty) => {
impl ops::Index<$t> for NonEmptyString {
type Output = str;
#[inline]
fn index(&self, index: $t) -> &str {
<String as ops::Index<$t>>::index(&self.0, index)
}
}
};
}
index_impl!(ops::Range<usize>);
index_impl!(ops::RangeTo<usize>);
index_impl!(ops::RangeFrom<usize>);
index_impl!(ops::RangeFull);
index_impl!(ops::RangeInclusive<usize>);
index_impl!(ops::RangeToInclusive<usize>);
#[cfg(not(no_global_oom_handling))]
impl std::fmt::Write for NonEmptyString {
#[inline]
fn write_str(&mut self, s: &str) -> std::fmt::Result {
self.push_str(s);
Ok(())
}
#[inline]
fn write_char(&mut self, c: char) -> std::fmt::Result {
self.push(c);
Ok(())
}
}