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 const 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 const fn from(s: &str) -> Result<Self, CapacityError<&str>> {
109 let mut arraystr = Self::new();
110 match arraystr.try_push_str(s) {
111 Ok(()) => Ok(arraystr),
112 Err(e) => Err(e),
113 }
114 }
115
116 pub const fn from_byte_string(b: &[u8; CAP]) -> Result<Self, Utf8Error> {
126 let len = match str::from_utf8(b) {
127 Ok(str) => str.len(),
128 Err(e) => return Err(e),
129 };
130 debug_assert!(len == CAP);
131 let mut vec = Self::new();
132 unsafe {
133 (b as *const [u8; CAP] as *const [MaybeUninit<u8>; CAP])
134 .copy_to_nonoverlapping(&mut vec.xs as *mut [MaybeUninit<u8>; CAP], 1);
135 vec.set_len(CAP);
136 }
137 Ok(vec)
138 }
139
140 #[inline]
150 pub const fn zero_filled() -> Self {
151 assert_capacity_limit!(CAP);
152 unsafe {
155 ArrayString {
156 xs: MaybeUninit::zeroed().assume_init(),
157 len: CAP as _
158 }
159 }
160 }
161
162 #[inline(always)]
171 pub const fn capacity(&self) -> usize { CAP }
172
173 pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
184
185 pub const fn remaining_capacity(&self) -> usize {
195 self.capacity() - self.len()
196 }
197
198 #[track_caller]
213 pub fn push(&mut self, c: char) {
214 self.try_push(c).unwrap();
215 }
216
217 pub const fn try_push(&mut self, c: char) -> Result<(), CapacityError<char>> {
236 let len = self.len();
237 unsafe {
238 let ptr = self.as_mut_ptr().add(len);
239 let remaining_cap = self.capacity() - len;
240 match encode_utf8(c, ptr, remaining_cap) {
241 Ok(n) => {
242 self.set_len(len + n);
243 Ok(())
244 }
245 Err(_) => Err(CapacityError::new(c)),
246 }
247 }
248 }
249
250 #[track_caller]
265 pub fn push_str(&mut self, s: &str) {
266 self.try_push_str(s).unwrap()
267 }
268
269 pub const fn try_push_str<'a>(&mut self, s: &'a str) -> Result<(), CapacityError<&'a str>> {
290 if s.len() > self.capacity() - self.len() {
291 return Err(CapacityError::new(s));
292 }
293 unsafe {
294 let dst = self.as_mut_ptr().add(self.len());
295 let src = s.as_ptr();
296 ptr::copy_nonoverlapping(src, dst, s.len());
297 let newl = self.len() + s.len();
298 self.set_len(newl);
299 }
300 Ok(())
301 }
302
303 pub fn pop(&mut self) -> Option<char> {
319 let ch = match self.chars().rev().next() {
320 Some(ch) => ch,
321 None => return None,
322 };
323 let new_len = self.len() - ch.len_utf8();
324 unsafe {
325 self.set_len(new_len);
326 }
327 Some(ch)
328 }
329
330 pub fn truncate(&mut self, new_len: usize) {
347 if new_len <= self.len() {
348 assert!(self.as_str().is_char_boundary(new_len));
349 unsafe {
350 self.set_len(new_len);
355 }
356 }
357 }
358
359 pub fn remove(&mut self, idx: usize) -> char {
377 let ch = match self[idx..].chars().next() {
378 Some(ch) => ch,
379 None => panic!("cannot remove a char from the end of a string"),
380 };
381
382 let next = idx + ch.len_utf8();
383 let len = self.len();
384 let ptr = self.as_mut_ptr();
385 unsafe {
386 ptr::copy(
387 ptr.add(next),
388 ptr.add(idx),
389 len - next);
390 self.set_len(len - (next - idx));
391 }
392 ch
393 }
394
395 pub const fn clear(&mut self) {
397 unsafe {
398 self.set_len(0);
399 }
400 }
401
402 pub const unsafe fn set_len(&mut self, length: usize) {
410 debug_assert!(length <= self.capacity());
412 self.len = length as LenUint;
413 }
414
415 pub const fn as_str(&self) -> &str {
417 unsafe {
418 let sl = slice::from_raw_parts(self.as_ptr(), self.len());
419 str::from_utf8_unchecked(sl)
420 }
421 }
422
423 pub const fn as_mut_str(&mut self) -> &mut str {
425 unsafe {
426 let len = self.len();
427 let sl = slice::from_raw_parts_mut(self.as_mut_ptr(), len);
428 str::from_utf8_unchecked_mut(sl)
429 }
430 }
431
432 pub const fn as_ptr(&self) -> *const u8 {
434 self.xs.as_ptr() as *const u8
435 }
436
437 pub const fn as_mut_ptr(&mut self) -> *mut u8 {
439 self.xs.as_mut_ptr() as *mut u8
440 }
441}
442
443impl<const CAP: usize> Deref for ArrayString<CAP>
444{
445 type Target = str;
446 #[inline]
447 fn deref(&self) -> &str {
448 self.as_str()
449 }
450}
451
452impl<const CAP: usize> DerefMut for ArrayString<CAP>
453{
454 #[inline]
455 fn deref_mut(&mut self) -> &mut str {
456 self.as_mut_str()
457 }
458}
459
460impl<const CAP: usize> PartialEq for ArrayString<CAP>
461{
462 fn eq(&self, rhs: &Self) -> bool {
463 **self == **rhs
464 }
465}
466
467impl<const CAP: usize> PartialEq<str> for ArrayString<CAP>
468{
469 fn eq(&self, rhs: &str) -> bool {
470 &**self == rhs
471 }
472}
473
474impl<const CAP: usize> PartialEq<ArrayString<CAP>> for str
475{
476 fn eq(&self, rhs: &ArrayString<CAP>) -> bool {
477 self == &**rhs
478 }
479}
480
481impl<const CAP: usize> Eq for ArrayString<CAP>
482{ }
483
484impl<const CAP: usize> Hash for ArrayString<CAP>
485{
486 fn hash<H: Hasher>(&self, h: &mut H) {
487 (**self).hash(h)
488 }
489}
490
491impl<const CAP: usize> Borrow<str> for ArrayString<CAP>
492{
493 fn borrow(&self) -> &str { self }
494}
495
496impl<const CAP: usize> BorrowMut<str> for ArrayString<CAP>
497{
498 fn borrow_mut(&mut self) -> &mut str { self }
499}
500
501impl<const CAP: usize> AsRef<str> for ArrayString<CAP>
502{
503 fn as_ref(&self) -> &str { self }
504}
505
506impl<const CAP: usize> fmt::Debug for ArrayString<CAP>
507{
508 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
509}
510
511#[cfg(feature="std")]
512impl<const CAP: usize> AsRef<Path> for ArrayString<CAP> {
513 fn as_ref(&self) -> &Path {
514 self.as_str().as_ref()
515 }
516}
517
518impl<const CAP: usize> fmt::Display for ArrayString<CAP>
519{
520 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { (**self).fmt(f) }
521}
522
523impl<const CAP: usize> fmt::Write for ArrayString<CAP>
525{
526 fn write_char(&mut self, c: char) -> fmt::Result {
527 self.try_push(c).map_err(|_| fmt::Error)
528 }
529
530 fn write_str(&mut self, s: &str) -> fmt::Result {
531 self.try_push_str(s).map_err(|_| fmt::Error)
532 }
533}
534
535impl<const CAP: usize> Clone for ArrayString<CAP>
536{
537 fn clone(&self) -> ArrayString<CAP> {
538 *self
539 }
540 fn clone_from(&mut self, rhs: &Self) {
541 self.clear();
543 self.try_push_str(rhs).ok();
544 }
545}
546
547impl<const CAP: usize> PartialOrd for ArrayString<CAP>
548{
549 fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
550 (**self).partial_cmp(&**rhs)
551 }
552 fn lt(&self, rhs: &Self) -> bool { **self < **rhs }
553 fn le(&self, rhs: &Self) -> bool { **self <= **rhs }
554 fn gt(&self, rhs: &Self) -> bool { **self > **rhs }
555 fn ge(&self, rhs: &Self) -> bool { **self >= **rhs }
556}
557
558impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
559{
560 fn partial_cmp(&self, rhs: &str) -> Option<cmp::Ordering> {
561 (**self).partial_cmp(rhs)
562 }
563 fn lt(&self, rhs: &str) -> bool { &**self < rhs }
564 fn le(&self, rhs: &str) -> bool { &**self <= rhs }
565 fn gt(&self, rhs: &str) -> bool { &**self > rhs }
566 fn ge(&self, rhs: &str) -> bool { &**self >= rhs }
567}
568
569impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str
570{
571 fn partial_cmp(&self, rhs: &ArrayString<CAP>) -> Option<cmp::Ordering> {
572 self.partial_cmp(&**rhs)
573 }
574 fn lt(&self, rhs: &ArrayString<CAP>) -> bool { self < &**rhs }
575 fn le(&self, rhs: &ArrayString<CAP>) -> bool { self <= &**rhs }
576 fn gt(&self, rhs: &ArrayString<CAP>) -> bool { self > &**rhs }
577 fn ge(&self, rhs: &ArrayString<CAP>) -> bool { self >= &**rhs }
578}
579
580impl<const CAP: usize> Ord for ArrayString<CAP>
581{
582 fn cmp(&self, rhs: &Self) -> cmp::Ordering {
583 (**self).cmp(&**rhs)
584 }
585}
586
587impl<const CAP: usize> FromStr for ArrayString<CAP>
588{
589 type Err = CapacityError;
590
591 fn from_str(s: &str) -> Result<Self, Self::Err> {
592 Self::from(s).map_err(CapacityError::simplify)
593 }
594}
595
596#[cfg(feature="serde")]
597impl<const CAP: usize> Serialize for ArrayString<CAP>
599{
600 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
601 where S: Serializer
602 {
603 serializer.serialize_str(&*self)
604 }
605}
606
607#[cfg(feature="serde")]
608impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
610{
611 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
612 where D: Deserializer<'de>
613 {
614 use serde::de::{self, Visitor};
615 use std::marker::PhantomData;
616
617 struct ArrayStringVisitor<const CAP: usize>(PhantomData<[u8; CAP]>);
618
619 impl<'de, const CAP: usize> Visitor<'de> for ArrayStringVisitor<CAP> {
620 type Value = ArrayString<CAP>;
621
622 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
623 write!(formatter, "a string no more than {} bytes long", CAP)
624 }
625
626 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
627 where E: de::Error,
628 {
629 ArrayString::from(v).map_err(|_| E::invalid_length(v.len(), &self))
630 }
631
632 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
633 where E: de::Error,
634 {
635 let s = str::from_utf8(v).map_err(|_| E::invalid_value(de::Unexpected::Bytes(v), &self))?;
636
637 ArrayString::from(s).map_err(|_| E::invalid_length(s.len(), &self))
638 }
639 }
640
641 deserializer.deserialize_str(ArrayStringVisitor(PhantomData))
642 }
643}
644
645#[cfg(feature = "borsh")]
646impl<const CAP: usize> borsh::BorshSerialize for ArrayString<CAP> {
648 fn serialize<W: borsh::io::Write>(&self, writer: &mut W) -> borsh::io::Result<()> {
649 <str as borsh::BorshSerialize>::serialize(&*self, writer)
650 }
651}
652
653#[cfg(feature = "borsh")]
654impl<const CAP: usize> borsh::BorshDeserialize for ArrayString<CAP> {
656 fn deserialize_reader<R: borsh::io::Read>(reader: &mut R) -> borsh::io::Result<Self> {
657 let len = <u32 as borsh::BorshDeserialize>::deserialize_reader(reader)? as usize;
658 if len > CAP {
659 return Err(borsh::io::Error::new(
660 borsh::io::ErrorKind::InvalidData,
661 format!("Expected a string no more than {} bytes long", CAP),
662 ))
663 }
664
665 let mut buf = [0u8; CAP];
666 let buf = &mut buf[..len];
667 reader.read_exact(buf)?;
668
669 let s = str::from_utf8(&buf).map_err(|err| {
670 borsh::io::Error::new(borsh::io::ErrorKind::InvalidData, err.to_string())
671 })?;
672 Ok(Self::from(s).unwrap())
673 }
674}
675
676impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
677{
678 type Error = CapacityError<&'a str>;
679
680 fn try_from(f: &'a str) -> Result<Self, Self::Error> {
681 let mut v = Self::new();
682 v.try_push_str(f)?;
683 Ok(v)
684 }
685}
686
687impl<'a, const CAP: usize> TryFrom<fmt::Arguments<'a>> for ArrayString<CAP>
688{
689 type Error = CapacityError<fmt::Error>;
690
691 fn try_from(f: fmt::Arguments<'a>) -> Result<Self, Self::Error> {
692 use fmt::Write;
693 let mut v = Self::new();
694 v.write_fmt(f).map_err(|e| CapacityError::new(e))?;
695 Ok(v)
696 }
697}
698
699#[cfg(feature = "zeroize")]
700impl<const CAP: usize> zeroize::Zeroize for ArrayString<CAP> {
715 fn zeroize(&mut self) {
716 self.clear();
718 self.xs.zeroize();
720 }
721}