@@ -11,12 +11,12 @@ use std::str;
1111use std:: str:: FromStr ;
1212use std:: str:: Utf8Error ;
1313
14- use crate :: CapacityError ;
14+ use crate :: { CapacityError , DefaultLenUint } ;
1515use crate :: LenUint ;
1616use crate :: char:: encode_utf8;
1717use crate :: utils:: MakeMaybeUninit ;
1818
19- #[ cfg( feature= "serde" ) ]
19+ #[ cfg( feature = "serde" ) ]
2020use serde:: { Serialize , Deserialize , Serializer , Deserializer } ;
2121
2222
@@ -32,21 +32,21 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
3232/// if needed.
3333#[ derive( Copy ) ]
3434#[ repr( C ) ]
35- pub struct ArrayString < const CAP : usize > {
35+ pub struct ArrayString < const CAP : usize , LenType : LenUint = DefaultLenUint > {
3636 // the `len` first elements of the array are initialized
37- len : LenUint ,
37+ len : LenType ,
3838 xs : [ MaybeUninit < u8 > ; CAP ] ,
3939}
4040
41- impl < const CAP : usize > Default for ArrayString < CAP >
41+ impl < const CAP : usize , LenType : LenUint > Default for ArrayString < CAP , LenType >
4242{
4343 /// Return an empty `ArrayString`
44- fn default ( ) -> ArrayString < CAP > {
44+ fn default ( ) -> Self {
4545 ArrayString :: new ( )
4646 }
4747}
4848
49- impl < const CAP : usize > ArrayString < CAP >
49+ impl < const CAP : usize , LenType : LenUint > ArrayString < CAP , LenType >
5050{
5151 /// Create a new empty `ArrayString`.
5252 ///
@@ -60,11 +60,9 @@ impl<const CAP: usize> ArrayString<CAP>
6060 /// assert_eq!(&string[..], "foo");
6161 /// assert_eq!(string.capacity(), 16);
6262 /// ```
63- pub fn new ( ) -> ArrayString < CAP > {
64- assert_capacity_limit ! ( CAP ) ;
65- unsafe {
66- ArrayString { xs : MaybeUninit :: uninit ( ) . assume_init ( ) , len : 0 }
67- }
63+ pub fn new ( ) -> Self {
64+ assert_capacity_limit ! ( LenType , CAP ) ;
65+ ArrayString { len : LenType :: from_usize ( 0 ) , xs : MakeMaybeUninit :: ARRAY }
6866 }
6967
7068 /// Create a new empty `ArrayString` (const fn).
@@ -74,20 +72,20 @@ impl<const CAP: usize> ArrayString<CAP>
7472 /// ```
7573 /// use arrayvec::ArrayString;
7674 ///
77- /// static ARRAY: ArrayString<1024> = ArrayString::new_const();
75+ /// const ARRAY: ArrayString<1024> = ArrayString::new_const();
7876 /// ```
79- pub const fn new_const ( ) -> ArrayString < CAP > {
80- assert_capacity_limit_const ! ( CAP ) ;
81- ArrayString { xs : MakeMaybeUninit :: ARRAY , len : 0 }
77+ pub const fn new_const ( ) -> Self {
78+ assert_capacity_limit_const ! ( LenType , CAP ) ;
79+ ArrayString { len : LenType :: ZERO , xs : MakeMaybeUninit :: ARRAY }
8280 }
8381
8482 /// Return the length of the string.
8583 #[ inline]
86- pub const fn len ( & self ) -> usize { self . len as usize }
84+ pub fn len ( & self ) -> usize { LenType :: to_usize ( self . len ) }
8785
8886 /// Returns whether the string is empty.
8987 #[ inline]
90- pub const fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
88+ pub fn is_empty ( & self ) -> bool { self . len == LenType :: ZERO }
9189
9290 /// Create a new `ArrayString` from a `str`.
9391 ///
@@ -141,13 +139,13 @@ impl<const CAP: usize> ArrayString<CAP>
141139 /// ```
142140 #[ inline]
143141 pub fn zero_filled ( ) -> Self {
144- assert_capacity_limit ! ( CAP ) ;
142+ assert_capacity_limit ! ( LenType , CAP ) ;
145143 // SAFETY: `assert_capacity_limit` asserts that `len` won't overflow and
146144 // `zeroed` fully fills the array with nulls.
147145 unsafe {
148146 ArrayString {
149147 xs : MaybeUninit :: zeroed ( ) . assume_init ( ) ,
150- len : CAP as _
148+ len : LenType :: from_usize ( CAP ) ,
151149 }
152150 }
153151 }
@@ -173,7 +171,7 @@ impl<const CAP: usize> ArrayString<CAP>
173171 /// string.push_str("A");
174172 /// assert!(string.is_full());
175173 /// ```
176- pub const fn is_full ( & self ) -> bool { self . len ( ) == self . capacity ( ) }
174+ pub fn is_full ( & self ) -> bool { self . len ( ) == self . capacity ( ) }
177175
178176 /// Returns the capacity left in the `ArrayString`.
179177 ///
@@ -184,7 +182,7 @@ impl<const CAP: usize> ArrayString<CAP>
184182 /// string.pop();
185183 /// assert_eq!(string.remaining_capacity(), 1);
186184 /// ```
187- pub const fn remaining_capacity ( & self ) -> usize {
185+ pub fn remaining_capacity ( & self ) -> usize {
188186 self . capacity ( ) - self . len ( )
189187 }
190188
@@ -299,7 +297,7 @@ impl<const CAP: usize> ArrayString<CAP>
299297 ///
300298 /// ```
301299 /// use arrayvec::ArrayString;
302- ///
300+ ///
303301 /// let mut s = ArrayString::<3>::from("foo").unwrap();
304302 ///
305303 /// assert_eq!(s.pop(), Some('o'));
@@ -339,7 +337,7 @@ impl<const CAP: usize> ArrayString<CAP>
339337 pub fn truncate ( & mut self , new_len : usize ) {
340338 if new_len <= self . len ( ) {
341339 assert ! ( self . is_char_boundary( new_len) ) ;
342- unsafe {
340+ unsafe {
343341 // In libstd truncate is called on the underlying vector,
344342 // which in turns drops each element.
345343 // As we know we don't have to worry about Drop,
@@ -359,7 +357,7 @@ impl<const CAP: usize> ArrayString<CAP>
359357 ///
360358 /// ```
361359 /// use arrayvec::ArrayString;
362- ///
360+ ///
363361 /// let mut s = ArrayString::<3>::from("foo").unwrap();
364362 ///
365363 /// assert_eq!(s.remove(0), 'f');
@@ -402,7 +400,7 @@ impl<const CAP: usize> ArrayString<CAP>
402400 pub unsafe fn set_len ( & mut self , length : usize ) {
403401 // type invariant that capacity always fits in LenUint
404402 debug_assert ! ( length <= self . capacity( ) ) ;
405- self . len = length as LenUint ;
403+ self . len = LenUint :: from_usize ( length ) ;
406404 }
407405
408406 /// Return a string slice of the whole `ArrayString`.
@@ -424,7 +422,7 @@ impl<const CAP: usize> ArrayString<CAP>
424422 }
425423}
426424
427- impl < const CAP : usize > Deref for ArrayString < CAP >
425+ impl < const CAP : usize , LenType : LenUint > Deref for ArrayString < CAP , LenType >
428426{
429427 type Target = str ;
430428 #[ inline]
@@ -436,7 +434,7 @@ impl<const CAP: usize> Deref for ArrayString<CAP>
436434 }
437435}
438436
439- impl < const CAP : usize > DerefMut for ArrayString < CAP >
437+ impl < const CAP : usize , LenType : LenUint > DerefMut for ArrayString < CAP , LenType >
440438{
441439 #[ inline]
442440 fn deref_mut ( & mut self ) -> & mut str {
@@ -448,64 +446,64 @@ impl<const CAP: usize> DerefMut for ArrayString<CAP>
448446 }
449447}
450448
451- impl < const CAP : usize > PartialEq for ArrayString < CAP >
449+ impl < const CAP : usize , LenType : LenUint > PartialEq for ArrayString < CAP , LenType >
452450{
453451 fn eq ( & self , rhs : & Self ) -> bool {
454452 * * self == * * rhs
455453 }
456454}
457455
458- impl < const CAP : usize > PartialEq < str > for ArrayString < CAP >
456+ impl < const CAP : usize , LenType : LenUint > PartialEq < str > for ArrayString < CAP , LenType >
459457{
460458 fn eq ( & self , rhs : & str ) -> bool {
461459 & * * self == rhs
462460 }
463461}
464462
465- impl < const CAP : usize > PartialEq < ArrayString < CAP > > for str
463+ impl < const CAP : usize , LenType : LenUint > PartialEq < ArrayString < CAP , LenType > > for str
466464{
467- fn eq ( & self , rhs : & ArrayString < CAP > ) -> bool {
465+ fn eq ( & self , rhs : & ArrayString < CAP , LenType > ) -> bool {
468466 self == & * * rhs
469467 }
470468}
471469
472- impl < const CAP : usize > Eq for ArrayString < CAP >
473- { }
470+ impl < const CAP : usize , LenType : LenUint > Eq for ArrayString < CAP , LenType >
471+ { }
474472
475- impl < const CAP : usize > Hash for ArrayString < CAP >
473+ impl < const CAP : usize , LenType : LenUint > Hash for ArrayString < CAP , LenType >
476474{
477475 fn hash < H : Hasher > ( & self , h : & mut H ) {
478476 ( * * self ) . hash ( h)
479477 }
480478}
481479
482- impl < const CAP : usize > Borrow < str > for ArrayString < CAP >
480+ impl < const CAP : usize , LenType : LenUint > Borrow < str > for ArrayString < CAP , LenType >
483481{
484482 fn borrow ( & self ) -> & str { self }
485483}
486484
487- impl < const CAP : usize > BorrowMut < str > for ArrayString < CAP >
485+ impl < const CAP : usize , LenType : LenUint > BorrowMut < str > for ArrayString < CAP , LenType >
488486{
489487 fn borrow_mut ( & mut self ) -> & mut str { self }
490488}
491489
492- impl < const CAP : usize > AsRef < str > for ArrayString < CAP >
490+ impl < const CAP : usize , LenType : LenUint > AsRef < str > for ArrayString < CAP , LenType >
493491{
494492 fn as_ref ( & self ) -> & str { self }
495493}
496494
497- impl < const CAP : usize > fmt:: Debug for ArrayString < CAP >
495+ impl < const CAP : usize , LenType : LenUint > fmt:: Debug for ArrayString < CAP , LenType >
498496{
499497 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
500498}
501499
502- impl < const CAP : usize > fmt:: Display for ArrayString < CAP >
500+ impl < const CAP : usize , LenType : LenUint > fmt:: Display for ArrayString < CAP , LenType >
503501{
504502 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
505503}
506504
507505/// `Write` appends written data to the end of the string.
508- impl < const CAP : usize > fmt:: Write for ArrayString < CAP >
506+ impl < const CAP : usize , LenType : LenUint > fmt:: Write for ArrayString < CAP , LenType >
509507{
510508 fn write_char ( & mut self , c : char ) -> fmt:: Result {
511509 self . try_push ( c) . map_err ( |_| fmt:: Error )
@@ -516,9 +514,9 @@ impl<const CAP: usize> fmt::Write for ArrayString<CAP>
516514 }
517515}
518516
519- impl < const CAP : usize > Clone for ArrayString < CAP >
517+ impl < const CAP : usize , LenType : LenUint > Clone for ArrayString < CAP , LenType >
520518{
521- fn clone ( & self ) -> ArrayString < CAP > {
519+ fn clone ( & self ) -> ArrayString < CAP , LenType > {
522520 * self
523521 }
524522 fn clone_from ( & mut self , rhs : & Self ) {
@@ -528,7 +526,7 @@ impl<const CAP: usize> Clone for ArrayString<CAP>
528526 }
529527}
530528
531- impl < const CAP : usize > PartialOrd for ArrayString < CAP >
529+ impl < const CAP : usize , LenType : LenUint > PartialOrd for ArrayString < CAP , LenType >
532530{
533531 fn partial_cmp ( & self , rhs : & Self ) -> Option < cmp:: Ordering > {
534532 ( * * self ) . partial_cmp ( & * * rhs)
@@ -539,7 +537,7 @@ impl<const CAP: usize> PartialOrd for ArrayString<CAP>
539537 fn ge ( & self , rhs : & Self ) -> bool { * * self >= * * rhs }
540538}
541539
542- impl < const CAP : usize > PartialOrd < str > for ArrayString < CAP >
540+ impl < const CAP : usize , LenType : LenUint > PartialOrd < str > for ArrayString < CAP , LenType >
543541{
544542 fn partial_cmp ( & self , rhs : & str ) -> Option < cmp:: Ordering > {
545543 ( * * self ) . partial_cmp ( rhs)
@@ -550,25 +548,25 @@ impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
550548 fn ge ( & self , rhs : & str ) -> bool { & * * self >= rhs }
551549}
552550
553- impl < const CAP : usize > PartialOrd < ArrayString < CAP > > for str
551+ impl < const CAP : usize , LenType : LenUint > PartialOrd < ArrayString < CAP , LenType > > for str
554552{
555- fn partial_cmp ( & self , rhs : & ArrayString < CAP > ) -> Option < cmp:: Ordering > {
553+ fn partial_cmp ( & self , rhs : & ArrayString < CAP , LenType > ) -> Option < cmp:: Ordering > {
556554 self . partial_cmp ( & * * rhs)
557555 }
558- fn lt ( & self , rhs : & ArrayString < CAP > ) -> bool { self < & * * rhs }
559- fn le ( & self , rhs : & ArrayString < CAP > ) -> bool { self <= & * * rhs }
560- fn gt ( & self , rhs : & ArrayString < CAP > ) -> bool { self > & * * rhs }
561- fn ge ( & self , rhs : & ArrayString < CAP > ) -> bool { self >= & * * rhs }
556+ fn lt ( & self , rhs : & ArrayString < CAP , LenType > ) -> bool { self < & * * rhs }
557+ fn le ( & self , rhs : & ArrayString < CAP , LenType > ) -> bool { self <= & * * rhs }
558+ fn gt ( & self , rhs : & ArrayString < CAP , LenType > ) -> bool { self > & * * rhs }
559+ fn ge ( & self , rhs : & ArrayString < CAP , LenType > ) -> bool { self >= & * * rhs }
562560}
563561
564- impl < const CAP : usize > Ord for ArrayString < CAP >
562+ impl < const CAP : usize , LenType : LenUint > Ord for ArrayString < CAP , LenType >
565563{
566564 fn cmp ( & self , rhs : & Self ) -> cmp:: Ordering {
567565 ( * * self ) . cmp ( & * * rhs)
568566 }
569567}
570568
571- impl < const CAP : usize > FromStr for ArrayString < CAP >
569+ impl < const CAP : usize , LenType : LenUint > FromStr for ArrayString < CAP , LenType >
572570{
573571 type Err = CapacityError ;
574572
@@ -577,9 +575,9 @@ impl<const CAP: usize> FromStr for ArrayString<CAP>
577575 }
578576}
579577
580- #[ cfg( feature= "serde" ) ]
578+ #[ cfg( feature = "serde" ) ]
581579/// Requires crate feature `"serde"`
582- impl < const CAP : usize > Serialize for ArrayString < CAP >
580+ impl < const CAP : usize , LenType : LenUint > Serialize for ArrayString < CAP , LenType >
583581{
584582 fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
585583 where S : Serializer
@@ -588,20 +586,20 @@ impl<const CAP: usize> Serialize for ArrayString<CAP>
588586 }
589587}
590588
591- #[ cfg( feature= "serde" ) ]
589+ #[ cfg( feature = "serde" ) ]
592590/// Requires crate feature `"serde"`
593- impl < ' de , const CAP : usize > Deserialize < ' de > for ArrayString < CAP >
591+ impl < ' de , const CAP : usize , LenType : LenUint > Deserialize < ' de > for ArrayString < CAP , LenType >
594592{
595593 fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
596594 where D : Deserializer < ' de >
597595 {
598596 use serde:: de:: { self , Visitor } ;
599597 use std:: marker:: PhantomData ;
600598
601- struct ArrayStringVisitor < const CAP : usize > ( PhantomData < [ u8 ; CAP ] > ) ;
599+ struct ArrayStringVisitor < const CAP : usize , LenType : LenUint > ( PhantomData < ( [ u8 ; CAP ] , LenType ) > ) ;
602600
603- impl < ' de , const CAP : usize > Visitor < ' de > for ArrayStringVisitor < CAP > {
604- type Value = ArrayString < CAP > ;
601+ impl < ' de , const CAP : usize , LenType : LenUint > Visitor < ' de > for ArrayStringVisitor < CAP , LenType > {
602+ type Value = ArrayString < CAP , LenType > ;
605603
606604 fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
607605 write ! ( formatter, "a string no more than {} bytes long" , CAP )
@@ -626,7 +624,7 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
626624 }
627625}
628626
629- impl < ' a , const CAP : usize > TryFrom < & ' a str > for ArrayString < CAP >
627+ impl < ' a , const CAP : usize , LenType : LenUint > TryFrom < & ' a str > for ArrayString < CAP , LenType >
630628{
631629 type Error = CapacityError < & ' a str > ;
632630
@@ -637,7 +635,7 @@ impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
637635 }
638636}
639637
640- impl < ' a , const CAP : usize > TryFrom < fmt:: Arguments < ' a > > for ArrayString < CAP >
638+ impl < ' a , const CAP : usize , LenType : LenUint > TryFrom < fmt:: Arguments < ' a > > for ArrayString < CAP , LenType >
641639{
642640 type Error = CapacityError < fmt:: Error > ;
643641
@@ -664,7 +662,7 @@ impl<'a, const CAP: usize> TryFrom<fmt::Arguments<'a>> for ArrayString<CAP>
664662/// unsafe { string.set_len(string.capacity()) };
665663/// assert_eq!(&*string, "\0\0\0\0\0\0");
666664/// ```
667- impl < const CAP : usize > zeroize:: Zeroize for ArrayString < CAP > {
665+ impl < const CAP : usize , LenType : LenUint > zeroize:: Zeroize for ArrayString < CAP , LenType > {
668666 fn zeroize ( & mut self ) {
669667 // There are no elements to drop
670668 self . clear ( ) ;
0 commit comments