@@ -11,14 +11,13 @@ use std::str;
11
11
use std:: str:: FromStr ;
12
12
use std:: str:: Utf8Error ;
13
13
14
- use crate :: CapacityError ;
15
- use crate :: LenUint ;
16
14
use crate :: char:: encode_utf8;
17
15
use crate :: utils:: MakeMaybeUninit ;
16
+ use crate :: CapacityError ;
17
+ use crate :: LenUint ;
18
18
19
- #[ cfg( feature="serde" ) ]
20
- use serde:: { Serialize , Deserialize , Serializer , Deserializer } ;
21
-
19
+ #[ cfg( feature = "serde" ) ]
20
+ use serde:: { Deserialize , Deserializer , Serialize , Serializer } ;
22
21
23
22
/// A string with a fixed capacity.
24
23
///
@@ -37,16 +36,14 @@ pub struct ArrayString<const CAP: usize> {
37
36
len : LenUint ,
38
37
}
39
38
40
- impl < const CAP : usize > Default for ArrayString < CAP >
41
- {
39
+ impl < const CAP : usize > Default for ArrayString < CAP > {
42
40
/// Return an empty `ArrayString`
43
41
fn default ( ) -> ArrayString < CAP > {
44
42
ArrayString :: new ( )
45
43
}
46
44
}
47
45
48
- impl < const CAP : usize > ArrayString < CAP >
49
- {
46
+ impl < const CAP : usize > ArrayString < CAP > {
50
47
/// Create a new empty `ArrayString`.
51
48
///
52
49
/// Capacity is inferred from the type parameter.
@@ -62,7 +59,10 @@ impl<const CAP: usize> ArrayString<CAP>
62
59
pub fn new ( ) -> ArrayString < CAP > {
63
60
assert_capacity_limit ! ( CAP ) ;
64
61
unsafe {
65
- ArrayString { xs : MaybeUninit :: uninit ( ) . assume_init ( ) , len : 0 }
62
+ ArrayString {
63
+ xs : MaybeUninit :: uninit ( ) . assume_init ( ) ,
64
+ len : 0 ,
65
+ }
66
66
}
67
67
}
68
68
@@ -77,16 +77,23 @@ impl<const CAP: usize> ArrayString<CAP>
77
77
/// ```
78
78
pub const fn new_const ( ) -> ArrayString < CAP > {
79
79
assert_capacity_limit_const ! ( CAP ) ;
80
- ArrayString { xs : MakeMaybeUninit :: ARRAY , len : 0 }
80
+ ArrayString {
81
+ xs : MakeMaybeUninit :: ARRAY ,
82
+ len : 0 ,
83
+ }
81
84
}
82
85
83
86
/// Return the length of the string.
84
87
#[ inline]
85
- pub const fn len ( & self ) -> usize { self . len as usize }
88
+ pub const fn len ( & self ) -> usize {
89
+ self . len as usize
90
+ }
86
91
87
92
/// Returns whether the string is empty.
88
93
#[ inline]
89
- pub const fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
94
+ pub const fn is_empty ( & self ) -> bool {
95
+ self . len ( ) == 0
96
+ }
90
97
91
98
/// Create a new `ArrayString` from a `str`.
92
99
///
@@ -146,7 +153,7 @@ impl<const CAP: usize> ArrayString<CAP>
146
153
unsafe {
147
154
ArrayString {
148
155
xs : MaybeUninit :: zeroed ( ) . assume_init ( ) ,
149
- len : CAP as _
156
+ len : CAP as _ ,
150
157
}
151
158
}
152
159
}
@@ -160,7 +167,9 @@ impl<const CAP: usize> ArrayString<CAP>
160
167
/// assert_eq!(string.capacity(), 3);
161
168
/// ```
162
169
#[ inline( always) ]
163
- pub const fn capacity ( & self ) -> usize { CAP }
170
+ pub const fn capacity ( & self ) -> usize {
171
+ CAP
172
+ }
164
173
165
174
/// Return if the `ArrayString` is completely filled.
166
175
///
@@ -172,7 +181,9 @@ impl<const CAP: usize> ArrayString<CAP>
172
181
/// string.push_str("A");
173
182
/// assert!(string.is_full());
174
183
/// ```
175
- pub const fn is_full ( & self ) -> bool { self . len ( ) == self . capacity ( ) }
184
+ pub const fn is_full ( & self ) -> bool {
185
+ self . len ( ) == self . capacity ( )
186
+ }
176
187
177
188
/// Returns the capacity left in the `ArrayString`.
178
189
///
@@ -296,7 +307,7 @@ impl<const CAP: usize> ArrayString<CAP>
296
307
///
297
308
/// ```
298
309
/// use arrayvec::ArrayString;
299
- ///
310
+ ///
300
311
/// let mut s = ArrayString::<3>::from("foo").unwrap();
301
312
///
302
313
/// assert_eq!(s.pop(), Some('o'));
@@ -336,7 +347,7 @@ impl<const CAP: usize> ArrayString<CAP>
336
347
pub fn truncate ( & mut self , new_len : usize ) {
337
348
if new_len <= self . len ( ) {
338
349
assert ! ( self . is_char_boundary( new_len) ) ;
339
- unsafe {
350
+ unsafe {
340
351
// In libstd truncate is called on the underlying vector,
341
352
// which in turns drops each element.
342
353
// As we know we don't have to worry about Drop,
@@ -356,7 +367,7 @@ impl<const CAP: usize> ArrayString<CAP>
356
367
///
357
368
/// ```
358
369
/// use arrayvec::ArrayString;
359
- ///
370
+ ///
360
371
/// let mut s = ArrayString::<3>::from("foo").unwrap();
361
372
///
362
373
/// assert_eq!(s.remove(0), 'f');
@@ -373,10 +384,7 @@ impl<const CAP: usize> ArrayString<CAP>
373
384
let len = self . len ( ) ;
374
385
let ptr = self . as_mut_ptr ( ) ;
375
386
unsafe {
376
- ptr:: copy (
377
- ptr. add ( next) ,
378
- ptr. add ( idx) ,
379
- len - next) ;
387
+ ptr:: copy ( ptr. add ( next) , ptr. add ( idx) , len - next) ;
380
388
self . set_len ( len - ( next - idx) ) ;
381
389
}
382
390
ch
@@ -421,8 +429,7 @@ impl<const CAP: usize> ArrayString<CAP>
421
429
}
422
430
}
423
431
424
- impl < const CAP : usize > Deref for ArrayString < CAP >
425
- {
432
+ impl < const CAP : usize > Deref for ArrayString < CAP > {
426
433
type Target = str ;
427
434
#[ inline]
428
435
fn deref ( & self ) -> & str {
@@ -433,8 +440,7 @@ impl<const CAP: usize> Deref for ArrayString<CAP>
433
440
}
434
441
}
435
442
436
- impl < const CAP : usize > DerefMut for ArrayString < CAP >
437
- {
443
+ impl < const CAP : usize > DerefMut for ArrayString < CAP > {
438
444
#[ inline]
439
445
fn deref_mut ( & mut self ) -> & mut str {
440
446
unsafe {
@@ -445,65 +451,64 @@ impl<const CAP: usize> DerefMut for ArrayString<CAP>
445
451
}
446
452
}
447
453
448
- impl < const CAP : usize > PartialEq for ArrayString < CAP >
449
- {
454
+ impl < const CAP : usize > PartialEq for ArrayString < CAP > {
450
455
fn eq ( & self , rhs : & Self ) -> bool {
451
456
* * self == * * rhs
452
457
}
453
458
}
454
459
455
- impl < const CAP : usize > PartialEq < str > for ArrayString < CAP >
456
- {
460
+ impl < const CAP : usize > PartialEq < str > for ArrayString < CAP > {
457
461
fn eq ( & self , rhs : & str ) -> bool {
458
462
& * * self == rhs
459
463
}
460
464
}
461
465
462
- impl < const CAP : usize > PartialEq < ArrayString < CAP > > for str
463
- {
466
+ impl < const CAP : usize > PartialEq < ArrayString < CAP > > for str {
464
467
fn eq ( & self , rhs : & ArrayString < CAP > ) -> bool {
465
468
self == & * * rhs
466
469
}
467
470
}
468
471
469
- impl < const CAP : usize > Eq for ArrayString < CAP >
470
- { }
472
+ impl < const CAP : usize > Eq for ArrayString < CAP > { }
471
473
472
- impl < const CAP : usize > Hash for ArrayString < CAP >
473
- {
474
+ impl < const CAP : usize > Hash for ArrayString < CAP > {
474
475
fn hash < H : Hasher > ( & self , h : & mut H ) {
475
476
( * * self ) . hash ( h)
476
477
}
477
478
}
478
479
479
- impl < const CAP : usize > Borrow < str > for ArrayString < CAP >
480
- {
481
- fn borrow ( & self ) -> & str { self }
480
+ impl < const CAP : usize > Borrow < str > for ArrayString < CAP > {
481
+ fn borrow ( & self ) -> & str {
482
+ self
483
+ }
482
484
}
483
485
484
- impl < const CAP : usize > BorrowMut < str > for ArrayString < CAP >
485
- {
486
- fn borrow_mut ( & mut self ) -> & mut str { self }
486
+ impl < const CAP : usize > BorrowMut < str > for ArrayString < CAP > {
487
+ fn borrow_mut ( & mut self ) -> & mut str {
488
+ self
489
+ }
487
490
}
488
491
489
- impl < const CAP : usize > AsRef < str > for ArrayString < CAP >
490
- {
491
- fn as_ref ( & self ) -> & str { self }
492
+ impl < const CAP : usize > AsRef < str > for ArrayString < CAP > {
493
+ fn as_ref ( & self ) -> & str {
494
+ self
495
+ }
492
496
}
493
497
494
- impl < const CAP : usize > fmt:: Debug for ArrayString < CAP >
495
- {
496
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
498
+ impl < const CAP : usize > fmt:: Debug for ArrayString < CAP > {
499
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
500
+ ( * * self ) . fmt ( f)
501
+ }
497
502
}
498
503
499
- impl < const CAP : usize > fmt:: Display for ArrayString < CAP >
500
- {
501
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
504
+ impl < const CAP : usize > fmt:: Display for ArrayString < CAP > {
505
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
506
+ ( * * self ) . fmt ( f)
507
+ }
502
508
}
503
509
504
510
/// `Write` appends written data to the end of the string.
505
- impl < const CAP : usize > fmt:: Write for ArrayString < CAP >
506
- {
511
+ impl < const CAP : usize > fmt:: Write for ArrayString < CAP > {
507
512
fn write_char ( & mut self , c : char ) -> fmt:: Result {
508
513
self . try_push ( c) . map_err ( |_| fmt:: Error )
509
514
}
@@ -513,8 +518,7 @@ impl<const CAP: usize> fmt::Write for ArrayString<CAP>
513
518
}
514
519
}
515
520
516
- impl < const CAP : usize > Clone for ArrayString < CAP >
517
- {
521
+ impl < const CAP : usize > Clone for ArrayString < CAP > {
518
522
fn clone ( & self ) -> ArrayString < CAP > {
519
523
* self
520
524
}
@@ -525,8 +529,8 @@ impl<const CAP: usize> Clone for ArrayString<CAP>
525
529
}
526
530
}
527
531
528
- impl < const CAP : usize > PartialOrd for ArrayString < CAP >
529
- {
532
+ # [ cfg_attr ( rustfmt , rustfmt :: skip ) ]
533
+ impl < const CAP : usize > PartialOrd for ArrayString < CAP > {
530
534
fn partial_cmp ( & self , rhs : & Self ) -> Option < cmp:: Ordering > {
531
535
( * * self ) . partial_cmp ( & * * rhs)
532
536
}
@@ -536,8 +540,8 @@ impl<const CAP: usize> PartialOrd for ArrayString<CAP>
536
540
fn ge ( & self , rhs : & Self ) -> bool { * * self >= * * rhs }
537
541
}
538
542
539
- impl < const CAP : usize > PartialOrd < str > for ArrayString < CAP >
540
- {
543
+ # [ cfg_attr ( rustfmt , rustfmt :: skip ) ]
544
+ impl < const CAP : usize > PartialOrd < str > for ArrayString < CAP > {
541
545
fn partial_cmp ( & self , rhs : & str ) -> Option < cmp:: Ordering > {
542
546
( * * self ) . partial_cmp ( rhs)
543
547
}
@@ -547,8 +551,8 @@ impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
547
551
fn ge ( & self , rhs : & str ) -> bool { & * * self >= rhs }
548
552
}
549
553
550
- impl < const CAP : usize > PartialOrd < ArrayString < CAP > > for str
551
- {
554
+ # [ cfg_attr ( rustfmt , rustfmt :: skip ) ]
555
+ impl < const CAP : usize > PartialOrd < ArrayString < CAP > > for str {
552
556
fn partial_cmp ( & self , rhs : & ArrayString < CAP > ) -> Option < cmp:: Ordering > {
553
557
self . partial_cmp ( & * * rhs)
554
558
}
@@ -558,39 +562,37 @@ impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str
558
562
fn ge ( & self , rhs : & ArrayString < CAP > ) -> bool { self >= & * * rhs }
559
563
}
560
564
561
- impl < const CAP : usize > Ord for ArrayString < CAP >
562
- {
565
+ impl < const CAP : usize > Ord for ArrayString < CAP > {
563
566
fn cmp ( & self , rhs : & Self ) -> cmp:: Ordering {
564
567
( * * self ) . cmp ( & * * rhs)
565
568
}
566
569
}
567
570
568
- impl < const CAP : usize > FromStr for ArrayString < CAP >
569
- {
571
+ impl < const CAP : usize > FromStr for ArrayString < CAP > {
570
572
type Err = CapacityError ;
571
573
572
574
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
573
575
Self :: from ( s) . map_err ( CapacityError :: simplify)
574
576
}
575
577
}
576
578
577
- #[ cfg( feature= "serde" ) ]
579
+ #[ cfg( feature = "serde" ) ]
578
580
/// Requires crate feature `"serde"`
579
- impl < const CAP : usize > Serialize for ArrayString < CAP >
580
- {
581
+ impl < const CAP : usize > Serialize for ArrayString < CAP > {
581
582
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
582
- where S : Serializer
583
+ where
584
+ S : Serializer ,
583
585
{
584
586
serializer. serialize_str ( & * self )
585
587
}
586
588
}
587
589
588
- #[ cfg( feature= "serde" ) ]
590
+ #[ cfg( feature = "serde" ) ]
589
591
/// Requires crate feature `"serde"`
590
- impl < ' de , const CAP : usize > Deserialize < ' de > for ArrayString < CAP >
591
- {
592
+ impl < ' de , const CAP : usize > Deserialize < ' de > for ArrayString < CAP > {
592
593
fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
593
- where D : Deserializer < ' de >
594
+ where
595
+ D : Deserializer < ' de > ,
594
596
{
595
597
use serde:: de:: { self , Visitor } ;
596
598
use std:: marker:: PhantomData ;
@@ -605,15 +607,18 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
605
607
}
606
608
607
609
fn visit_str < E > ( self , v : & str ) -> Result < Self :: Value , E >
608
- where E : de:: Error ,
610
+ where
611
+ E : de:: Error ,
609
612
{
610
613
ArrayString :: from ( v) . map_err ( |_| E :: invalid_length ( v. len ( ) , & self ) )
611
614
}
612
615
613
616
fn visit_bytes < E > ( self , v : & [ u8 ] ) -> Result < Self :: Value , E >
614
- where E : de:: Error ,
617
+ where
618
+ E : de:: Error ,
615
619
{
616
- let s = str:: from_utf8 ( v) . map_err ( |_| E :: invalid_value ( de:: Unexpected :: Bytes ( v) , & self ) ) ?;
620
+ let s = str:: from_utf8 ( v)
621
+ . map_err ( |_| E :: invalid_value ( de:: Unexpected :: Bytes ( v) , & self ) ) ?;
617
622
618
623
ArrayString :: from ( s) . map_err ( |_| E :: invalid_length ( s. len ( ) , & self ) )
619
624
}
@@ -623,8 +628,7 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
623
628
}
624
629
}
625
630
626
- impl < ' a , const CAP : usize > TryFrom < & ' a str > for ArrayString < CAP >
627
- {
631
+ impl < ' a , const CAP : usize > TryFrom < & ' a str > for ArrayString < CAP > {
628
632
type Error = CapacityError < & ' a str > ;
629
633
630
634
fn try_from ( f : & ' a str ) -> Result < Self , Self :: Error > {
@@ -634,8 +638,7 @@ impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
634
638
}
635
639
}
636
640
637
- impl < ' a , const CAP : usize > TryFrom < fmt:: Arguments < ' a > > for ArrayString < CAP >
638
- {
641
+ impl < ' a , const CAP : usize > TryFrom < fmt:: Arguments < ' a > > for ArrayString < CAP > {
639
642
type Error = CapacityError < fmt:: Error > ;
640
643
641
644
fn try_from ( f : fmt:: Arguments < ' a > ) -> Result < Self , Self :: Error > {
0 commit comments