arrayvec_const/
array_string.rs

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/// A string with a fixed capacity.
26///
27/// The `ArrayString` is a string backed by a fixed size array. It keeps track
28/// of its length, and is parameterized by `CAP` for the maximum capacity.
29///
30/// `CAP` is of type `usize` but is range limited to `u32::MAX` (or `u16` on 16-bit targets);
31/// attempting to create larger arrayvecs with larger capacity will panic.
32///
33/// The string is a contiguous value that you can store directly on the stack
34/// if needed.
35#[derive(Copy)]
36#[repr(C)]
37pub struct ArrayString<const CAP: usize> {
38    // the `len` first elements of the array are initialized
39    len: LenUint,
40    xs: [MaybeUninit<u8>; CAP],
41}
42
43impl<const CAP: usize> Default for ArrayString<CAP>
44{
45    /// Return an empty `ArrayString`
46    fn default() -> ArrayString<CAP> {
47        ArrayString::new()
48    }
49}
50
51impl<const CAP: usize> ArrayString<CAP>
52{
53    /// Create a new empty `ArrayString`.
54    ///
55    /// Capacity is inferred from the type parameter.
56    ///
57    /// ```
58    /// use arrayvec::ArrayString;
59    ///
60    /// let mut string = ArrayString::<16>::new();
61    /// string.push_str("foo");
62    /// assert_eq!(&string[..], "foo");
63    /// assert_eq!(string.capacity(), 16);
64    /// ```
65    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    /// Create a new empty `ArrayString` (const fn).
73    ///
74    /// Capacity is inferred from the type parameter.
75    ///
76    /// ```
77    /// use arrayvec::ArrayString;
78    ///
79    /// static ARRAY: ArrayString<1024> = ArrayString::new_const();
80    /// ```
81    pub const fn new_const() -> ArrayString<CAP> {
82        assert_capacity_limit_const!(CAP);
83        ArrayString { xs: MakeMaybeUninit::ARRAY, len: 0 }
84    }
85
86    /// Return the length of the string.
87    #[inline]
88    pub const fn len(&self) -> usize { self.len as usize }
89
90    /// Returns whether the string is empty.
91    #[inline]
92    pub const fn is_empty(&self) -> bool { self.len() == 0 }
93
94    /// Create a new `ArrayString` from a `str`.
95    ///
96    /// Capacity is inferred from the type parameter.
97    ///
98    /// **Errors** if the backing array is not large enough to fit the string.
99    ///
100    /// ```
101    /// use arrayvec::ArrayString;
102    ///
103    /// let mut string = ArrayString::<3>::from("foo").unwrap();
104    /// assert_eq!(&string[..], "foo");
105    /// assert_eq!(string.len(), 3);
106    /// assert_eq!(string.capacity(), 3);
107    /// ```
108    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    /// Create a new `ArrayString` from a byte string literal.
117    ///
118    /// **Errors** if the byte string literal is not valid UTF-8.
119    ///
120    /// ```
121    /// use arrayvec::ArrayString;
122    ///
123    /// let string = ArrayString::from_byte_string(b"hello world").unwrap();
124    /// ```
125    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    /// Create a new `ArrayString` value fully filled with ASCII NULL characters (`\0`). Useful
141    /// to be used as a buffer to collect external data or as a buffer for intermediate processing.
142    ///
143    /// ```
144    /// use arrayvec::ArrayString;
145    ///
146    /// let string = ArrayString::<16>::zero_filled();
147    /// assert_eq!(string.len(), 16);
148    /// ```
149    #[inline]
150    pub const fn zero_filled() -> Self {
151        assert_capacity_limit!(CAP);
152        // SAFETY: `assert_capacity_limit` asserts that `len` won't overflow and
153        // `zeroed` fully fills the array with nulls.
154        unsafe {
155            ArrayString {
156                xs: MaybeUninit::zeroed().assume_init(),
157                len: CAP as _
158            }
159        }
160    }
161
162    /// Return the capacity of the `ArrayString`.
163    ///
164    /// ```
165    /// use arrayvec::ArrayString;
166    ///
167    /// let string = ArrayString::<3>::new();
168    /// assert_eq!(string.capacity(), 3);
169    /// ```
170    #[inline(always)]
171    pub const fn capacity(&self) -> usize { CAP }
172
173    /// Return if the `ArrayString` is completely filled.
174    ///
175    /// ```
176    /// use arrayvec::ArrayString;
177    ///
178    /// let mut string = ArrayString::<1>::new();
179    /// assert!(!string.is_full());
180    /// string.push_str("A");
181    /// assert!(string.is_full());
182    /// ```
183    pub const fn is_full(&self) -> bool { self.len() == self.capacity() }
184
185    /// Returns the capacity left in the `ArrayString`.
186    ///
187    /// ```
188    /// use arrayvec::ArrayString;
189    ///
190    /// let mut string = ArrayString::<3>::from("abc").unwrap();
191    /// string.pop();
192    /// assert_eq!(string.remaining_capacity(), 1);
193    /// ```
194    pub const fn remaining_capacity(&self) -> usize {
195        self.capacity() - self.len()
196    }
197
198    /// Adds the given char to the end of the string.
199    ///
200    /// ***Panics*** if the backing array is not large enough to fit the additional char.
201    ///
202    /// ```
203    /// use arrayvec::ArrayString;
204    ///
205    /// let mut string = ArrayString::<2>::new();
206    ///
207    /// string.push('a');
208    /// string.push('b');
209    ///
210    /// assert_eq!(&string[..], "ab");
211    /// ```
212    #[track_caller]
213    pub fn push(&mut self, c: char) {
214        self.try_push(c).unwrap();
215    }
216
217    /// Adds the given char to the end of the string.
218    ///
219    /// Returns `Ok` if the push succeeds.
220    ///
221    /// **Errors** if the backing array is not large enough to fit the additional char.
222    ///
223    /// ```
224    /// use arrayvec::ArrayString;
225    ///
226    /// let mut string = ArrayString::<2>::new();
227    ///
228    /// string.try_push('a').unwrap();
229    /// string.try_push('b').unwrap();
230    /// let overflow = string.try_push('c');
231    ///
232    /// assert_eq!(&string[..], "ab");
233    /// assert_eq!(overflow.unwrap_err().element(), 'c');
234    /// ```
235    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    /// Adds the given string slice to the end of the string.
251    ///
252    /// ***Panics*** if the backing array is not large enough to fit the string.
253    ///
254    /// ```
255    /// use arrayvec::ArrayString;
256    ///
257    /// let mut string = ArrayString::<2>::new();
258    ///
259    /// string.push_str("a");
260    /// string.push_str("d");
261    ///
262    /// assert_eq!(&string[..], "ad");
263    /// ```
264    #[track_caller]
265    pub fn push_str(&mut self, s: &str) {
266        self.try_push_str(s).unwrap()
267    }
268
269    /// Adds the given string slice to the end of the string.
270    ///
271    /// Returns `Ok` if the push succeeds.
272    ///
273    /// **Errors** if the backing array is not large enough to fit the string.
274    ///
275    /// ```
276    /// use arrayvec::ArrayString;
277    ///
278    /// let mut string = ArrayString::<2>::new();
279    ///
280    /// string.try_push_str("a").unwrap();
281    /// let overflow1 = string.try_push_str("bc");
282    /// string.try_push_str("d").unwrap();
283    /// let overflow2 = string.try_push_str("ef");
284    ///
285    /// assert_eq!(&string[..], "ad");
286    /// assert_eq!(overflow1.unwrap_err().element(), "bc");
287    /// assert_eq!(overflow2.unwrap_err().element(), "ef");
288    /// ```
289    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    /// Removes the last character from the string and returns it.
304    ///
305    /// Returns `None` if this `ArrayString` is empty.
306    ///
307    /// ```
308    /// use arrayvec::ArrayString;
309    /// 
310    /// let mut s = ArrayString::<3>::from("foo").unwrap();
311    ///
312    /// assert_eq!(s.pop(), Some('o'));
313    /// assert_eq!(s.pop(), Some('o'));
314    /// assert_eq!(s.pop(), Some('f'));
315    ///
316    /// assert_eq!(s.pop(), None);
317    /// ```
318    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    /// Shortens this `ArrayString` to the specified length.
331    ///
332    /// If `new_len` is greater than the string’s current length, this has no
333    /// effect.
334    ///
335    /// ***Panics*** if `new_len` does not lie on a `char` boundary.
336    ///
337    /// ```
338    /// use arrayvec::ArrayString;
339    ///
340    /// let mut string = ArrayString::<6>::from("foobar").unwrap();
341    /// string.truncate(3);
342    /// assert_eq!(&string[..], "foo");
343    /// string.truncate(4);
344    /// assert_eq!(&string[..], "foo");
345    /// ```
346    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                // In libstd truncate is called on the underlying vector,
351                // which in turns drops each element.
352                // As we know we don't have to worry about Drop,
353                // we can just set the length (a la clear.)
354                self.set_len(new_len);
355            }
356        }
357    }
358
359    /// Removes a `char` from this `ArrayString` at a byte position and returns it.
360    ///
361    /// This is an `O(n)` operation, as it requires copying every element in the
362    /// array.
363    ///
364    /// ***Panics*** if `idx` is larger than or equal to the `ArrayString`’s length,
365    /// or if it does not lie on a `char` boundary.
366    ///
367    /// ```
368    /// use arrayvec::ArrayString;
369    /// 
370    /// let mut s = ArrayString::<3>::from("foo").unwrap();
371    ///
372    /// assert_eq!(s.remove(0), 'f');
373    /// assert_eq!(s.remove(1), 'o');
374    /// assert_eq!(s.remove(0), 'o');
375    /// ```
376    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    /// Make the string empty.
396    pub const fn clear(&mut self) {
397        unsafe {
398            self.set_len(0);
399        }
400    }
401
402    /// Set the strings’s length.
403    ///
404    /// This function is `unsafe` because it changes the notion of the
405    /// number of “valid” bytes in the string. Use with care.
406    ///
407    /// This method uses *debug assertions* to check the validity of `length`
408    /// and may use other debug assertions.
409    pub const unsafe fn set_len(&mut self, length: usize) {
410        // type invariant that capacity always fits in LenUint
411        debug_assert!(length <= self.capacity());
412        self.len = length as LenUint;
413    }
414
415    /// Return a string slice of the whole `ArrayString`.
416    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    /// Return a mutable string slice of the whole `ArrayString`.
424    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    /// Return a raw pointer to the string's buffer.
433    pub const fn as_ptr(&self) -> *const u8 {
434        self.xs.as_ptr() as *const u8
435    }
436
437    /// Return a raw mutable pointer to the string's buffer.
438    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
523/// `Write` appends written data to the end of the string.
524impl<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        // guaranteed to fit due to types matching.
542        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")]
597/// Requires crate feature `"serde"`
598impl<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")]
608/// Requires crate feature `"serde"`
609impl<'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")]
646/// Requires crate feature `"borsh"`
647impl<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")]
654/// Requires crate feature `"borsh"`
655impl<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")]
700/// "Best efforts" zeroing of the `ArrayString`'s buffer when the `zeroize` feature is enabled.
701///
702/// The length is set to 0, and the buffer is dropped and zeroized.
703/// Cannot ensure that previous moves of the `ArrayString` did not leave values on the stack.
704///
705/// ```
706/// use arrayvec::ArrayString;
707/// use zeroize::Zeroize;
708/// let mut string = ArrayString::<6>::from("foobar").unwrap();
709/// string.zeroize();
710/// assert_eq!(string.len(), 0);
711/// unsafe { string.set_len(string.capacity()) };
712/// assert_eq!(&*string, "\0\0\0\0\0\0");
713/// ```
714impl<const CAP: usize> zeroize::Zeroize for ArrayString<CAP> {
715    fn zeroize(&mut self) {
716        // There are no elements to drop
717        self.clear();
718        // Zeroize the backing array.
719        self.xs.zeroize();
720    }
721}