//! Trait implementations for `ArrayString` (that aren't for integration) use crate::{generic::ArraySlice, prelude::*}; use core::fmt::{self, Debug, Display, Formatter, Write}; use core::iter::FromIterator; use core::ops::{Add, Deref, DerefMut, Index, IndexMut}; use core::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}; use core::str::{self, FromStr}; use core::{borrow::Borrow, borrow::BorrowMut, cmp::Ordering, hash::Hash, hash::Hasher}; impl Default for ArrayString where SIZE: Capacity, { #[inline] fn default() -> Self { Self { array: SIZE::Array::zeroed(), size: Default::default(), } } } impl Copy for ArrayString where SIZE::Array: Copy {} impl AsRef for ArrayString where SIZE: Capacity, { #[inline] fn as_ref(&self) -> &str { unsafe { str::from_utf8_unchecked(self.as_ref()) } } } impl AsMut for ArrayString where SIZE: Capacity, { #[inline] fn as_mut(&mut self) -> &mut str { let len = self.size as usize; let slice = unsafe { self.array.as_mut_slice().get_unchecked_mut(..len) }; unsafe { str::from_utf8_unchecked_mut(slice) } } } impl AsRef<[u8]> for ArrayString where SIZE: Capacity, { #[inline] fn as_ref(&self) -> &[u8] { unsafe { self.array.as_slice().get_unchecked(..self.size.into()) } } } impl<'a, SIZE> From<&'a str> for ArrayString where SIZE: Capacity, { #[inline] fn from(s: &str) -> Self { Self::from_str_truncate(s) } } impl FromStr for ArrayString where SIZE: Capacity, { type Err = OutOfBounds; #[inline] fn from_str(s: &str) -> Result { Self::try_from_str(s) } } impl Debug for ArrayString where SIZE: Capacity, { #[inline] fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.debug_struct("ArrayString") .field("array", &self.as_str()) .field("size", &self.size) .finish() } } impl<'a, 'b, SIZE> PartialEq for ArrayString where SIZE: Capacity, { #[inline] fn eq(&self, other: &str) -> bool { self.as_str().eq(other) } } impl Borrow for ArrayString where SIZE: Capacity, { #[inline] fn borrow(&self) -> &str { self.as_str() } } impl BorrowMut for ArrayString where SIZE: Capacity, { #[inline] fn borrow_mut(&mut self) -> &mut str { self.as_mut_str() } } impl Hash for ArrayString where SIZE: Capacity, { #[inline] fn hash(&self, hasher: &mut H) { self.as_str().hash(hasher); } } impl PartialEq for ArrayString where SIZE: Capacity, { #[inline] fn eq(&self, other: &Self) -> bool { self.as_str().eq(other.as_str()) } } impl Eq for ArrayString {} impl Ord for ArrayString where SIZE: Capacity, { #[inline] fn cmp(&self, other: &Self) -> Ordering { self.as_str().cmp(other.as_str()) } } impl PartialOrd for ArrayString where SIZE: Capacity, { #[inline] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl<'a, SIZE> Add<&'a str> for ArrayString where SIZE: Capacity, { type Output = Self; #[inline] fn add(self, other: &str) -> Self::Output { let mut out = unsafe { Self::from_str_unchecked(self) }; out.push_str(other); out } } impl Write for ArrayString where SIZE: Capacity, { #[inline] fn write_str(&mut self, slice: &str) -> fmt::Result { self.try_push_str(slice).map_err(|_| fmt::Error) } } impl Display for ArrayString where SIZE: Capacity, { #[inline] fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "{}", self.as_str()) } } impl Deref for ArrayString where SIZE: Capacity, { type Target = str; #[inline] fn deref(&self) -> &Self::Target { self.as_ref() } } impl DerefMut for ArrayString where SIZE: Capacity, { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { self.as_mut() } } impl FromIterator for ArrayString where SIZE: Capacity, { fn from_iter>(iter: I) -> Self { Self::from_chars(iter) } } impl<'a, SIZE> FromIterator<&'a str> for ArrayString where SIZE: Capacity, { fn from_iter>(iter: I) -> Self { Self::from_iterator(iter) } } impl Extend for ArrayString where SIZE: Capacity, { fn extend>(&mut self, iterable: I) { self.push_str(Self::from_chars(iterable)) } } impl<'a, SIZE> Extend<&'a char> for ArrayString where SIZE: Capacity, { fn extend>(&mut self, iter: I) { self.extend(iter.into_iter().cloned()); } } impl<'a, SIZE> Extend<&'a str> for ArrayString where SIZE: Capacity, { fn extend>(&mut self, iterable: I) { self.push_str(Self::from_iterator(iterable)) } } impl IndexMut> for ArrayString where SIZE: Capacity, { #[inline] fn index_mut(&mut self, index: RangeFrom) -> &mut str { let start = index.start as usize; self.as_mut_str().index_mut(RangeFrom { start }) } } impl IndexMut> for ArrayString where SIZE: Capacity, { #[inline] fn index_mut(&mut self, index: RangeTo) -> &mut str { let end = index.end as usize; self.as_mut_str().index_mut(RangeTo { end }) } } impl IndexMut for ArrayString where SIZE: Capacity, { #[inline] fn index_mut(&mut self, index: RangeFull) -> &mut str { self.as_mut_str().index_mut(index) } } impl IndexMut> for ArrayString where SIZE: Capacity, { #[inline] fn index_mut(&mut self, index: Range) -> &mut str { let (start, end) = (index.start as usize, index.end as usize); let range = Range { start, end }; self.as_mut_str().index_mut(range) } } impl IndexMut> for ArrayString where SIZE: Capacity, { #[inline] fn index_mut(&mut self, index: RangeToInclusive) -> &mut str { let end = index.end as usize; let range = RangeToInclusive { end }; self.as_mut_str().index_mut(range) } } impl IndexMut> for ArrayString where SIZE: Capacity, { #[inline] fn index_mut(&mut self, index: RangeInclusive) -> &mut str { let (start, end) = (*index.start() as usize, *index.end() as usize); let range = RangeInclusive::new(start, end); self.as_mut_str().index_mut(range) } } impl Index> for ArrayString where SIZE: Capacity, { type Output = str; #[inline] fn index(&self, index: RangeFrom) -> &Self::Output { let start = index.start as usize; self.as_str().index(RangeFrom { start }) } } impl Index> for ArrayString where SIZE: Capacity, { type Output = str; #[inline] fn index(&self, index: RangeTo) -> &Self::Output { let end = index.end as usize; self.as_str().index(RangeTo { end }) } } impl Index for ArrayString where SIZE: Capacity, { type Output = str; #[inline] fn index(&self, index: RangeFull) -> &Self::Output { self.as_str().index(index) } } impl Index> for ArrayString where SIZE: Capacity, { type Output = str; #[inline] fn index(&self, index: Range) -> &Self::Output { let (start, end) = (index.start as usize, index.end as usize); self.as_str().index(Range { start, end }) } } impl Index> for ArrayString where SIZE: Capacity, { type Output = str; #[inline] fn index(&self, index: RangeToInclusive) -> &Self::Output { let end = index.end as usize; self.as_str().index(RangeToInclusive { end }) } } impl Index> for ArrayString where SIZE: Capacity, { type Output = str; #[inline] fn index(&self, index: RangeInclusive) -> &Self::Output { let (start, end) = (*index.start() as usize, *index.end() as usize); let range = RangeInclusive::new(start, end); self.as_str().index(range) } }