@@ -11,12 +11,12 @@ use std::str;
11
11
use std:: str:: FromStr ;
12
12
use std:: str:: Utf8Error ;
13
13
14
- use crate :: CapacityError ;
14
+ use crate :: { CapacityError , DefaultLenUint } ;
15
15
use crate :: LenUint ;
16
16
use crate :: char:: encode_utf8;
17
17
use crate :: utils:: MakeMaybeUninit ;
18
18
19
- #[ cfg( feature= "serde" ) ]
19
+ #[ cfg( feature = "serde" ) ]
20
20
use serde:: { Serialize , Deserialize , Serializer , Deserializer } ;
21
21
22
22
@@ -32,21 +32,21 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
32
32
/// if needed.
33
33
#[ derive( Copy ) ]
34
34
#[ repr( C ) ]
35
- pub struct ArrayString < const CAP : usize > {
35
+ pub struct ArrayString < const CAP : usize , LenType : LenUint = DefaultLenUint > {
36
36
// the `len` first elements of the array are initialized
37
- len : LenUint ,
37
+ len : LenType ,
38
38
xs : [ MaybeUninit < u8 > ; CAP ] ,
39
39
}
40
40
41
- impl < const CAP : usize > Default for ArrayString < CAP >
41
+ impl < const CAP : usize , LenType : LenUint > Default for ArrayString < CAP , LenType >
42
42
{
43
43
/// Return an empty `ArrayString`
44
- fn default ( ) -> ArrayString < CAP > {
44
+ fn default ( ) -> Self {
45
45
ArrayString :: new ( )
46
46
}
47
47
}
48
48
49
- impl < const CAP : usize > ArrayString < CAP >
49
+ impl < const CAP : usize , LenType : LenUint > ArrayString < CAP , LenType >
50
50
{
51
51
/// Create a new empty `ArrayString`.
52
52
///
@@ -60,11 +60,9 @@ impl<const CAP: usize> ArrayString<CAP>
60
60
/// assert_eq!(&string[..], "foo");
61
61
/// assert_eq!(string.capacity(), 16);
62
62
/// ```
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 }
68
66
}
69
67
70
68
/// Create a new empty `ArrayString` (const fn).
@@ -74,20 +72,20 @@ impl<const CAP: usize> ArrayString<CAP>
74
72
/// ```
75
73
/// use arrayvec::ArrayString;
76
74
///
77
- /// static ARRAY: ArrayString<1024> = ArrayString::new_const();
75
+ /// const ARRAY: ArrayString<1024> = ArrayString::new_const();
78
76
/// ```
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 }
82
80
}
83
81
84
82
/// Return the length of the string.
85
83
#[ inline]
86
- pub const fn len ( & self ) -> usize { self . len as usize }
84
+ pub fn len ( & self ) -> usize { LenType :: to_usize ( self . len ) }
87
85
88
86
/// Returns whether the string is empty.
89
87
#[ inline]
90
- pub const fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
88
+ pub fn is_empty ( & self ) -> bool { self . len == LenType :: ZERO }
91
89
92
90
/// Create a new `ArrayString` from a `str`.
93
91
///
@@ -141,13 +139,13 @@ impl<const CAP: usize> ArrayString<CAP>
141
139
/// ```
142
140
#[ inline]
143
141
pub fn zero_filled ( ) -> Self {
144
- assert_capacity_limit ! ( CAP ) ;
142
+ assert_capacity_limit ! ( LenType , CAP ) ;
145
143
// SAFETY: `assert_capacity_limit` asserts that `len` won't overflow and
146
144
// `zeroed` fully fills the array with nulls.
147
145
unsafe {
148
146
ArrayString {
149
147
xs : MaybeUninit :: zeroed ( ) . assume_init ( ) ,
150
- len : CAP as _
148
+ len : LenType :: from_usize ( CAP ) ,
151
149
}
152
150
}
153
151
}
@@ -173,7 +171,7 @@ impl<const CAP: usize> ArrayString<CAP>
173
171
/// string.push_str("A");
174
172
/// assert!(string.is_full());
175
173
/// ```
176
- pub const fn is_full ( & self ) -> bool { self . len ( ) == self . capacity ( ) }
174
+ pub fn is_full ( & self ) -> bool { self . len ( ) == self . capacity ( ) }
177
175
178
176
/// Returns the capacity left in the `ArrayString`.
179
177
///
@@ -184,7 +182,7 @@ impl<const CAP: usize> ArrayString<CAP>
184
182
/// string.pop();
185
183
/// assert_eq!(string.remaining_capacity(), 1);
186
184
/// ```
187
- pub const fn remaining_capacity ( & self ) -> usize {
185
+ pub fn remaining_capacity ( & self ) -> usize {
188
186
self . capacity ( ) - self . len ( )
189
187
}
190
188
@@ -299,7 +297,7 @@ impl<const CAP: usize> ArrayString<CAP>
299
297
///
300
298
/// ```
301
299
/// use arrayvec::ArrayString;
302
- ///
300
+ ///
303
301
/// let mut s = ArrayString::<3>::from("foo").unwrap();
304
302
///
305
303
/// assert_eq!(s.pop(), Some('o'));
@@ -339,7 +337,7 @@ impl<const CAP: usize> ArrayString<CAP>
339
337
pub fn truncate ( & mut self , new_len : usize ) {
340
338
if new_len <= self . len ( ) {
341
339
assert ! ( self . is_char_boundary( new_len) ) ;
342
- unsafe {
340
+ unsafe {
343
341
// In libstd truncate is called on the underlying vector,
344
342
// which in turns drops each element.
345
343
// As we know we don't have to worry about Drop,
@@ -359,7 +357,7 @@ impl<const CAP: usize> ArrayString<CAP>
359
357
///
360
358
/// ```
361
359
/// use arrayvec::ArrayString;
362
- ///
360
+ ///
363
361
/// let mut s = ArrayString::<3>::from("foo").unwrap();
364
362
///
365
363
/// assert_eq!(s.remove(0), 'f');
@@ -402,7 +400,7 @@ impl<const CAP: usize> ArrayString<CAP>
402
400
pub unsafe fn set_len ( & mut self , length : usize ) {
403
401
// type invariant that capacity always fits in LenUint
404
402
debug_assert ! ( length <= self . capacity( ) ) ;
405
- self . len = length as LenUint ;
403
+ self . len = LenUint :: from_usize ( length ) ;
406
404
}
407
405
408
406
/// Return a string slice of the whole `ArrayString`.
@@ -424,7 +422,7 @@ impl<const CAP: usize> ArrayString<CAP>
424
422
}
425
423
}
426
424
427
- impl < const CAP : usize > Deref for ArrayString < CAP >
425
+ impl < const CAP : usize , LenType : LenUint > Deref for ArrayString < CAP , LenType >
428
426
{
429
427
type Target = str ;
430
428
#[ inline]
@@ -436,7 +434,7 @@ impl<const CAP: usize> Deref for ArrayString<CAP>
436
434
}
437
435
}
438
436
439
- impl < const CAP : usize > DerefMut for ArrayString < CAP >
437
+ impl < const CAP : usize , LenType : LenUint > DerefMut for ArrayString < CAP , LenType >
440
438
{
441
439
#[ inline]
442
440
fn deref_mut ( & mut self ) -> & mut str {
@@ -448,64 +446,64 @@ impl<const CAP: usize> DerefMut for ArrayString<CAP>
448
446
}
449
447
}
450
448
451
- impl < const CAP : usize > PartialEq for ArrayString < CAP >
449
+ impl < const CAP : usize , LenType : LenUint > PartialEq for ArrayString < CAP , LenType >
452
450
{
453
451
fn eq ( & self , rhs : & Self ) -> bool {
454
452
* * self == * * rhs
455
453
}
456
454
}
457
455
458
- impl < const CAP : usize > PartialEq < str > for ArrayString < CAP >
456
+ impl < const CAP : usize , LenType : LenUint > PartialEq < str > for ArrayString < CAP , LenType >
459
457
{
460
458
fn eq ( & self , rhs : & str ) -> bool {
461
459
& * * self == rhs
462
460
}
463
461
}
464
462
465
- impl < const CAP : usize > PartialEq < ArrayString < CAP > > for str
463
+ impl < const CAP : usize , LenType : LenUint > PartialEq < ArrayString < CAP , LenType > > for str
466
464
{
467
- fn eq ( & self , rhs : & ArrayString < CAP > ) -> bool {
465
+ fn eq ( & self , rhs : & ArrayString < CAP , LenType > ) -> bool {
468
466
self == & * * rhs
469
467
}
470
468
}
471
469
472
- impl < const CAP : usize > Eq for ArrayString < CAP >
473
- { }
470
+ impl < const CAP : usize , LenType : LenUint > Eq for ArrayString < CAP , LenType >
471
+ { }
474
472
475
- impl < const CAP : usize > Hash for ArrayString < CAP >
473
+ impl < const CAP : usize , LenType : LenUint > Hash for ArrayString < CAP , LenType >
476
474
{
477
475
fn hash < H : Hasher > ( & self , h : & mut H ) {
478
476
( * * self ) . hash ( h)
479
477
}
480
478
}
481
479
482
- impl < const CAP : usize > Borrow < str > for ArrayString < CAP >
480
+ impl < const CAP : usize , LenType : LenUint > Borrow < str > for ArrayString < CAP , LenType >
483
481
{
484
482
fn borrow ( & self ) -> & str { self }
485
483
}
486
484
487
- impl < const CAP : usize > BorrowMut < str > for ArrayString < CAP >
485
+ impl < const CAP : usize , LenType : LenUint > BorrowMut < str > for ArrayString < CAP , LenType >
488
486
{
489
487
fn borrow_mut ( & mut self ) -> & mut str { self }
490
488
}
491
489
492
- impl < const CAP : usize > AsRef < str > for ArrayString < CAP >
490
+ impl < const CAP : usize , LenType : LenUint > AsRef < str > for ArrayString < CAP , LenType >
493
491
{
494
492
fn as_ref ( & self ) -> & str { self }
495
493
}
496
494
497
- impl < const CAP : usize > fmt:: Debug for ArrayString < CAP >
495
+ impl < const CAP : usize , LenType : LenUint > fmt:: Debug for ArrayString < CAP , LenType >
498
496
{
499
497
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
500
498
}
501
499
502
- impl < const CAP : usize > fmt:: Display for ArrayString < CAP >
500
+ impl < const CAP : usize , LenType : LenUint > fmt:: Display for ArrayString < CAP , LenType >
503
501
{
504
502
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
505
503
}
506
504
507
505
/// `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 >
509
507
{
510
508
fn write_char ( & mut self , c : char ) -> fmt:: Result {
511
509
self . try_push ( c) . map_err ( |_| fmt:: Error )
@@ -516,9 +514,9 @@ impl<const CAP: usize> fmt::Write for ArrayString<CAP>
516
514
}
517
515
}
518
516
519
- impl < const CAP : usize > Clone for ArrayString < CAP >
517
+ impl < const CAP : usize , LenType : LenUint > Clone for ArrayString < CAP , LenType >
520
518
{
521
- fn clone ( & self ) -> ArrayString < CAP > {
519
+ fn clone ( & self ) -> ArrayString < CAP , LenType > {
522
520
* self
523
521
}
524
522
fn clone_from ( & mut self , rhs : & Self ) {
@@ -528,7 +526,7 @@ impl<const CAP: usize> Clone for ArrayString<CAP>
528
526
}
529
527
}
530
528
531
- impl < const CAP : usize > PartialOrd for ArrayString < CAP >
529
+ impl < const CAP : usize , LenType : LenUint > PartialOrd for ArrayString < CAP , LenType >
532
530
{
533
531
fn partial_cmp ( & self , rhs : & Self ) -> Option < cmp:: Ordering > {
534
532
( * * self ) . partial_cmp ( & * * rhs)
@@ -539,7 +537,7 @@ impl<const CAP: usize> PartialOrd for ArrayString<CAP>
539
537
fn ge ( & self , rhs : & Self ) -> bool { * * self >= * * rhs }
540
538
}
541
539
542
- impl < const CAP : usize > PartialOrd < str > for ArrayString < CAP >
540
+ impl < const CAP : usize , LenType : LenUint > PartialOrd < str > for ArrayString < CAP , LenType >
543
541
{
544
542
fn partial_cmp ( & self , rhs : & str ) -> Option < cmp:: Ordering > {
545
543
( * * self ) . partial_cmp ( rhs)
@@ -550,25 +548,25 @@ impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
550
548
fn ge ( & self , rhs : & str ) -> bool { & * * self >= rhs }
551
549
}
552
550
553
- impl < const CAP : usize > PartialOrd < ArrayString < CAP > > for str
551
+ impl < const CAP : usize , LenType : LenUint > PartialOrd < ArrayString < CAP , LenType > > for str
554
552
{
555
- fn partial_cmp ( & self , rhs : & ArrayString < CAP > ) -> Option < cmp:: Ordering > {
553
+ fn partial_cmp ( & self , rhs : & ArrayString < CAP , LenType > ) -> Option < cmp:: Ordering > {
556
554
self . partial_cmp ( & * * rhs)
557
555
}
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 }
562
560
}
563
561
564
- impl < const CAP : usize > Ord for ArrayString < CAP >
562
+ impl < const CAP : usize , LenType : LenUint > Ord for ArrayString < CAP , LenType >
565
563
{
566
564
fn cmp ( & self , rhs : & Self ) -> cmp:: Ordering {
567
565
( * * self ) . cmp ( & * * rhs)
568
566
}
569
567
}
570
568
571
- impl < const CAP : usize > FromStr for ArrayString < CAP >
569
+ impl < const CAP : usize , LenType : LenUint > FromStr for ArrayString < CAP , LenType >
572
570
{
573
571
type Err = CapacityError ;
574
572
@@ -577,9 +575,9 @@ impl<const CAP: usize> FromStr for ArrayString<CAP>
577
575
}
578
576
}
579
577
580
- #[ cfg( feature= "serde" ) ]
578
+ #[ cfg( feature = "serde" ) ]
581
579
/// 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 >
583
581
{
584
582
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
585
583
where S : Serializer
@@ -588,20 +586,20 @@ impl<const CAP: usize> Serialize for ArrayString<CAP>
588
586
}
589
587
}
590
588
591
- #[ cfg( feature= "serde" ) ]
589
+ #[ cfg( feature = "serde" ) ]
592
590
/// 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 >
594
592
{
595
593
fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
596
594
where D : Deserializer < ' de >
597
595
{
598
596
use serde:: de:: { self , Visitor } ;
599
597
use std:: marker:: PhantomData ;
600
598
601
- struct ArrayStringVisitor < const CAP : usize > ( PhantomData < [ u8 ; CAP ] > ) ;
599
+ struct ArrayStringVisitor < const CAP : usize , LenType : LenUint > ( PhantomData < ( [ u8 ; CAP ] , LenType ) > ) ;
602
600
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 > ;
605
603
606
604
fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
607
605
write ! ( formatter, "a string no more than {} bytes long" , CAP )
@@ -626,7 +624,7 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
626
624
}
627
625
}
628
626
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 >
630
628
{
631
629
type Error = CapacityError < & ' a str > ;
632
630
@@ -637,7 +635,7 @@ impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
637
635
}
638
636
}
639
637
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 >
641
639
{
642
640
type Error = CapacityError < fmt:: Error > ;
643
641
@@ -664,7 +662,7 @@ impl<'a, const CAP: usize> TryFrom<fmt::Arguments<'a>> for ArrayString<CAP>
664
662
/// unsafe { string.set_len(string.capacity()) };
665
663
/// assert_eq!(&*string, "\0\0\0\0\0\0");
666
664
/// ```
667
- impl < const CAP : usize > zeroize:: Zeroize for ArrayString < CAP > {
665
+ impl < const CAP : usize , LenType : LenUint > zeroize:: Zeroize for ArrayString < CAP , LenType > {
668
666
fn zeroize ( & mut self ) {
669
667
// There are no elements to drop
670
668
self . clear ( ) ;
0 commit comments