generic_str/
traits_utf32.rs

1use generic_vec::{raw::Storage, GenericVec};
2
3#[cfg(feature = "alloc")]
4use generic_vec::HeapVec;
5#[cfg(feature = "alloc")]
6use std::{
7    borrow::Cow,
8    iter::FromIterator,
9    ops::{Add, AddAssign},
10};
11
12use crate::{string_base::StringBase, OwnedString};
13use core::{
14    ops::{Index, IndexMut},
15    slice::SliceIndex,
16};
17
18impl<I> Index<I> for crate::str32
19where
20    I: SliceIndex<crate::str32>,
21{
22    type Output = I::Output;
23
24    #[inline]
25    fn index(&self, index: I) -> &I::Output {
26        index.index(self)
27    }
28}
29
30impl<I> IndexMut<I> for crate::str32
31where
32    I: SliceIndex<crate::str32>,
33{
34    #[inline]
35    fn index_mut(&mut self, index: I) -> &mut I::Output {
36        index.index_mut(self)
37    }
38}
39
40#[cfg(feature = "alloc")]
41impl<S: ?Sized + Storage<Item = char>> std::fmt::Debug for OwnedString<char, S> {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        let s: String = self.as_ref().storage.iter().collect();
44        write!(f, "{:?}", s)
45    }
46}
47
48#[cfg(feature = "alloc")]
49impl<S: ?Sized + Storage<Item = char>> std::fmt::Display for OwnedString<char, S> {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        let s: String = self.as_ref().storage.iter().collect();
52        write!(f, "{}", s)
53    }
54}
55
56#[cfg(feature = "alloc")]
57impl std::fmt::Debug for crate::str32 {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        let s: String = self.storage.iter().collect();
60        write!(f, "{:?}", s)
61    }
62}
63
64#[cfg(feature = "alloc")]
65impl std::fmt::Display for crate::str32 {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        let s: String = self.storage.iter().collect();
68        write!(f, "{}", s)
69    }
70}
71
72#[cfg(feature = "alloc")]
73impl From<String> for crate::String32 {
74    fn from(s: String) -> Self {
75        s.chars().collect()
76    }
77}
78
79#[cfg(feature = "alloc")]
80impl FromIterator<char> for crate::String32 {
81    fn from_iter<I: IntoIterator<Item = char>>(iter: I) -> Self {
82        let mut new = Self::new();
83        iter.into_iter().for_each(|c| new.push(c));
84        new
85    }
86}
87
88#[cfg(feature = "alloc")]
89impl From<&str> for StringBase<HeapVec<char>> {
90    fn from(s: &str) -> Self {
91        s.to_owned().into()
92    }
93}
94
95impl From<&str> for &crate::str32 {
96    fn from(s: &str) -> Self {
97        unsafe { core::mem::transmute(s) }
98    }
99}
100
101impl From<&mut str> for &mut crate::str32 {
102    fn from(s: &mut str) -> Self {
103        unsafe { core::mem::transmute(s) }
104    }
105}
106
107impl<S: ?Sized + Storage<Item = char>, T: ?Sized + AsRef<[char]>> PartialEq<T>
108    for StringBase<GenericVec<S::Item, S>>
109{
110    fn eq(&self, other: &T) -> bool {
111        self.storage.as_ref() == other.as_ref()
112    }
113}
114
115impl<T: ?Sized + AsRef<[char]>> PartialEq<T> for crate::str32 {
116    fn eq(&self, other: &T) -> bool {
117        self.storage.as_ref() == other.as_ref()
118    }
119}
120
121#[cfg(feature = "alloc")]
122impl<'a> Add<&'a crate::str32> for Cow<'a, crate::str32> {
123    type Output = Cow<'a, crate::str32>;
124
125    #[inline]
126    fn add(mut self, rhs: &'a crate::str32) -> Self::Output {
127        self += rhs;
128        self
129    }
130}
131
132#[cfg(feature = "alloc")]
133impl<'a> AddAssign<&'a crate::str32> for Cow<'a, crate::str32> {
134    fn add_assign(&mut self, rhs: &'a crate::str32) {
135        if self.is_empty() {
136            *self = Cow::Borrowed(rhs)
137        } else if !rhs.is_empty() {
138            if let Cow::Borrowed(lhs) = *self {
139                let mut s = crate::String32::with_capacity(lhs.len() + rhs.len());
140                s.push_str32(lhs);
141                *self = Cow::Owned(s);
142            }
143            self.to_mut().push_str32(rhs);
144        }
145    }
146}