1use std::borrow::{Borrow, BorrowMut};
2use std::cmp;
3use std::convert::TryFrom;
4use std::fmt;
5use std::hash::{Hash, Hasher};
6use std::mem::MaybeUninit;
7use std::ops::{Deref, DerefMut};
8#[cfg(feature="std")]
9use std::path::Path;
10use std::ptr;
11use std::slice;
12use std::str;
13use std::str::FromStr;
14use std::str::Utf8Error;
15
16use crate::CapacityError;
17use crate::LenUint;
18use crate::char::encode_utf8;
19use crate::utils::MakeMaybeUninit;
20
21#[cfg(feature="serde")]
22use serde::{Serialize, Deserialize, Serializer, Deserializer};
23
24
25#[derive(Copy)]
36#[repr(C)]
37pub struct ArrayString<const CAP: usize> {
38 len: LenUint,
40 xs: [MaybeUninit<u8>; CAP],
41}
42
43impl<const CAP: usize> Default for ArrayString<CAP>
44{
45 fn default() -> ArrayString<CAP> {
47 ArrayString::new()
48 }
49}
50
51impl<const CAP: usize> ArrayString<CAP>
52{
53 pub fn new() -> ArrayString<CAP> {
66 assert_capacity_limit!(CAP);
67 unsafe {
68 ArrayString { xs: MaybeUninit::uninit().assume_init(), len: 0 }
69 }
70 }
71
72 pub const fn new_const() -> ArrayString<CAP> {
82 assert_capacity_limit_const!(CAP);
83 ArrayString { xs: MakeMaybeUninit::ARRAY, len: 0 }
84 }
85
86 #[inline]
88 pub const fn len(&self) -> usize { self.len as usize }
89
90 #[inline]
92 pub const fn is_empty(&self) -> bool { self.len() == 0 }
93
94 pub fn from(s: &str) -> Result<Self, CapacityError<&str>> {
109 let mut arraystr = Self::new();
110 arraystr.try_push_str(s)?;
111 Ok(arraystr)
112 }
113
114 pub fn from_byte_string(b: &[u8; CAP]) -> Result<Self, Utf8Error> {
124 let len = str::from_utf8(b)?.len();
125 debug_assert_eq!(len, CAP);
126 let mut vec = Self::new();
127 unsafe {
128 (b as *const [u8; CAP] as *const [MaybeUninit<u8>; CAP])
129 .copy_to_nonoverlapping(&mut vec.xs as *mut [MaybeUninit<u8>; CAP], 1);
130 vec.set_len(CAP);
131 }
132 Ok(vec)
133 }
134
135 #[inline]
145 pub fn zero_filled() -> Self {
146 assert_capacity_limit!(CAP);
147 unsafe {
150 ArrayString {
151 xs: MaybeUninit::zeroed().assume_init(),
152 len: CAP as _
153 }
154 }
155 }
156
157 #[inline(always)]
166 pub const fn capacity(&self) -> usize { CAP }
167
168 pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
179
180 pub const fn remaining_capacity(&self) -> usize {
190 self.capacity() - self.len()
191 }
192
193 #[track_caller]
208 pub fn push(&mut self, c: char) {
209 self.try_push(c).unwrap();
210 }
211
212 pub fn try_push(&mut self, c: char) -> Result<(), CapacityError<char>> {
231 let len = self.len();
232 unsafe {
233 let ptr = self.as_mut_ptr().add(len);
234 let remaining_cap = self.capacity() - len;
235 match encode_utf8(c, ptr, remaining_cap) {
236 Ok(n) => {
237 self.set_len(len + n);
238 Ok(())
239 }
240 Err(_) => Err(CapacityError::new(c)),
241 }
242 }
243 }
244
245 #[track_caller]
260 pub fn push_str(&mut self, s: &str) {
261 self.try_push_str(s).unwrap()
262 }
263
264 pub fn try_push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>> {
285 if s.len() > self.capacity() - self.len() {
286 return Err(CapacityError::new(s));
287 }
288 unsafe {
289 let dst = self.as_mut_ptr().add(self.len());
290 let src = s.as_ptr();
291 ptr::copy_nonoverlapping(src, dst, s.len());
292 let newl = self.len() + s.len();
293 self.set_len(newl);
294 }
295 Ok(())
296 }
297
298 pub fn pop(&mut self) -> Option<char> {
314 let ch = match self.chars().rev().next() {
315 Some(ch) => ch,
316 None => return None,
317 };
318 let new_len = self.len() - ch.len_utf8();
319 unsafe {
320 self.set_len(new_len);
321 }
322 Some(ch)
323 }
324
325 pub fn truncate(&mut self, new_len: usize) {
342 if new_len <= self.len() {
343 assert!(self.is_char_boundary(new_len));
344 unsafe {
345 self.set_len(new_len);
350 }
351 }
352 }
353
354 pub fn remove(&mut self, idx: usize) -> char {
372 let ch = match self[idx..].chars().next() {
373 Some(ch) => ch,
374 None => panic!("cannot remove a char from the end of a string"),
375 };
376
377 let next = idx + ch.len_utf8();
378 let len = self.len();
379 let ptr = self.as_mut_ptr();
380 unsafe {
381 ptr::copy(
382 ptr.add(next),
383 ptr.add(idx),
384 len - next);
385 self.set_len(len - (next - idx));
386 }
387 ch
388 }
389
390 pub fn clear(&mut self) {
392 unsafe {
393 self.set_len(0);
394 }
395 }
396
397 pub unsafe fn set_len(&mut self, length: usize) {
405 debug_assert!(length <= self.capacity());
407 self.len = length as LenUint;
408 }
409
410 pub fn as_str(&self) -> &str {
412 self
413 }
414
415 pub fn as_mut_str(&mut self) -> &mut str {
417 self
418 }
419
420 pub fn as_ptr(&self) -> *const u8 {
422 self.xs.as_ptr() as *const u8
423 }
424
425 pub fn as_mut_ptr(&mut self) -> *mut u8 {
427 self.xs.as_mut_ptr() as *mut u8
428 }
429}
430
431impl<const CAP: usize> Deref for ArrayString<CAP>
432{
433 type Target = str;
434 #[inline]
435 fn deref(&self) -> &str {
436 unsafe {
437 let sl = slice::from_raw_parts(self.as_ptr(), self.len());
438 str::from_utf8_unchecked(sl)
439 }
440 }
441}
442
443impl<const CAP: usize> DerefMut for ArrayString<CAP>
444{
445 #[inline]
446 fn deref_mut(&mut self) -> &mut str {
447 unsafe {
448 let len = self.len();
449 let sl = slice::from_raw_parts_mut(self.as_mut_ptr(), len);
450 str::from_utf8_unchecked_mut(sl)
451 }
452 }
453}
454
455impl<const CAP: usize> PartialEq for ArrayString<CAP>
456{
457 fn eq(&self, rhs: &Self) -> bool {
458 **self == **rhs
459 }
460}
461
462impl<const CAP: usize> PartialEq<str> for ArrayString<CAP>
463{
464 fn eq(&self, rhs: &str) -> bool {
465 &**self == rhs
466 }
467}
468
469impl<const CAP: usize> PartialEq<ArrayString<CAP>> for str
470{
471 fn eq(&self, rhs: &ArrayString<CAP>) -> bool {
472 self == &**rhs
473 }
474}
475
476impl<const CAP: usize> Eq for ArrayString<CAP>
477{ }
478
479impl<const CAP: usize> Hash for ArrayString<CAP>
480{
481 fn hash<H: Hasher>(&self, h: &mut H) {
482 (**self).hash(h)
483 }
484}
485
486impl<const CAP: usize> Borrow<str> for ArrayString<CAP>
487{
488 fn borrow(&self) -> &str { self }
489}
490
491impl<const CAP: usize> BorrowMut<str> for ArrayString<CAP>
492{
493 fn borrow_mut(&mut self) -> &mut str { self }
494}
495
496impl<const CAP: usize> AsRef<str> for ArrayString<CAP>
497{
498 fn as_ref(&self) -> &str { self }
499}
500
501impl<const CAP: usize> fmt::Debug for ArrayString<CAP>
502{
503 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
504}
505
506#[cfg(feature="std")]
507impl<const CAP: usize> AsRef<Path> for ArrayString<CAP> {
508 fn as_ref(&self) -> &Path {
509 self.as_str().as_ref()
510 }
511}
512
513impl<const CAP: usize> fmt::Display for ArrayString<CAP>
514{
515 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
516}
517
518impl<const CAP: usize> fmt::Write for ArrayString<CAP>
520{
521 fn write_char(&mut self, c: char) -> fmt::Result {
522 self.try_push(c).map_err(|_| fmt::Error)
523 }
524
525 fn write_str(&mut self, s: &str) -> fmt::Result {
526 self.try_push_str(s).map_err(|_| fmt::Error)
527 }
528}
529
530impl<const CAP: usize> Clone for ArrayString<CAP>
531{
532 fn clone(&self) -> ArrayString<CAP> {
533 *self
534 }
535 fn clone_from(&mut self, rhs: &Self) {
536 self.clear();
538 self.try_push_str(rhs).ok();
539 }
540}
541
542impl<const CAP: usize> PartialOrd for ArrayString<CAP>
543{
544 fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
545 (**self).partial_cmp(&**rhs)
546 }
547 fn lt(&self, rhs: &Self) -> bool { **self < **rhs }
548 fn le(&self, rhs: &Self) -> bool { **self <= **rhs }
549 fn gt(&self, rhs: &Self) -> bool { **self > **rhs }
550 fn ge(&self, rhs: &Self) -> bool { **self >= **rhs }
551}
552
553impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
554{
555 fn partial_cmp(&self, rhs: &str) -> Option<cmp::Ordering> {
556 (**self).partial_cmp(rhs)
557 }
558 fn lt(&self, rhs: &str) -> bool { &**self < rhs }
559 fn le(&self, rhs: &str) -> bool { &**self <= rhs }
560 fn gt(&self, rhs: &str) -> bool { &**self > rhs }
561 fn ge(&self, rhs: &str) -> bool { &**self >= rhs }
562}
563
564impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str
565{
566 fn partial_cmp(&self, rhs: &ArrayString<CAP>) -> Option<cmp::Ordering> {
567 self.partial_cmp(&**rhs)
568 }
569 fn lt(&self, rhs: &ArrayString<CAP>) -> bool { self < &**rhs }
570 fn le(&self, rhs: &ArrayString<CAP>) -> bool { self <= &**rhs }
571 fn gt(&self, rhs: &ArrayString<CAP>) -> bool { self > &**rhs }
572 fn ge(&self, rhs: &ArrayString<CAP>) -> bool { self >= &**rhs }
573}
574
575impl<const CAP: usize> Ord for ArrayString<CAP>
576{
577 fn cmp(&self, rhs: &Self) -> cmp::Ordering {
578 (**self).cmp(&**rhs)
579 }
580}
581
582impl<const CAP: usize> FromStr for ArrayString<CAP>
583{
584 type Err = CapacityError;
585
586 fn from_str(s: &str) -> Result<Self, Self::Err> {
587 Self::from(s).map_err(CapacityError::simplify)
588 }
589}
590
591#[cfg(feature="serde")]
592impl<const CAP: usize> Serialize for ArrayString<CAP>
594{
595 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
596 where S: Serializer
597 {
598 serializer.serialize_str(&*self)
599 }
600}
601
602#[cfg(feature="serde")]
603impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
605{
606 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
607 where D: Deserializer<'de>
608 {
609 use serde::de::{self, Visitor};
610 use std::marker::PhantomData;
611
612 struct ArrayStringVisitor<const CAP: usize>(PhantomData<[u8; CAP]>);
613
614 impl<'de, const CAP: usize> Visitor<'de> for ArrayStringVisitor<CAP> {
615 type Value = ArrayString<CAP>;
616
617 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
618 write!(formatter, "a string no more than {} bytes long", CAP)
619 }
620
621 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
622 where E: de::Error,
623 {
624 ArrayString::from(v).map_err(|_| E::invalid_length(v.len(), &self))
625 }
626
627 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
628 where E: de::Error,
629 {
630 let s = str::from_utf8(v).map_err(|_| E::invalid_value(de::Unexpected::Bytes(v), &self))?;
631
632 ArrayString::from(s).map_err(|_| E::invalid_length(s.len(), &self))
633 }
634 }
635
636 deserializer.deserialize_str(ArrayStringVisitor(PhantomData))
637 }
638}
639
640#[cfg(feature = "borsh")]
641impl<const CAP: usize> borsh::BorshSerialize for ArrayString<CAP> {
643 fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
644 <str as borsh::BorshSerialize>::serialize(&*self, writer)
645 }
646}
647
648#[cfg(feature = "borsh")]
649impl<const CAP: usize> borsh::BorshDeserialize for ArrayString<CAP> {
651 fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
652 let len = <u32 as borsh::BorshDeserialize>::deserialize_reader(reader)? as usize;
653 if len > CAP {
654 return Err(borsh::io::Error::new(
655 borsh::io::ErrorKind::InvalidData,
656 format!("Expected a string no more than {} bytes long", CAP),
657 ))
658 }
659
660 let mut buf = [0u8; CAP];
661 let buf = &mut buf[..len];
662 reader.read_exact(buf)?;
663
664 let s = str::from_utf8(&buf).map_err(|err| {
665 borsh::io::Error::new(borsh::io::ErrorKind::InvalidData, err.to_string())
666 })?;
667 Ok(Self::from(s).unwrap())
668 }
669}
670
671impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
672{
673 type Error = CapacityError<&'a str>;
674
675 fn try_from(f: &'a str) -> Result<Self, Self::Error> {
676 let mut v = Self::new();
677 v.try_push_str(f)?;
678 Ok(v)
679 }
680}
681
682impl<'a, const CAP: usize> TryFrom<fmt::Arguments<'a>> for ArrayString<CAP>
683{
684 type Error = CapacityError<fmt::Error>;
685
686 fn try_from(f: fmt::Arguments<'a>) -> Result<Self, Self::Error> {
687 use fmt::Write;
688 let mut v = Self::new();
689 v.write_fmt(f).map_err(|e| CapacityError::new(e))?;
690 Ok(v)
691 }
692}
693
694#[cfg(feature = "zeroize")]
695impl<const CAP: usize> zeroize::Zeroize for ArrayString<CAP> {
710 fn zeroize(&mut self) {
711 self.clear();
713 self.xs.zeroize();
715 }
716}