@@ -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');
@@ -372,9 +383,11 @@ impl<const CAP: usize> ArrayString<CAP>
372
383
let next = idx + ch. len_utf8 ( ) ;
373
384
let len = self . len ( ) ;
374
385
unsafe {
375
- ptr:: copy ( self . as_ptr ( ) . add ( next) ,
376
- self . as_mut_ptr ( ) . add ( idx) ,
377
- len - next) ;
386
+ ptr:: copy (
387
+ self . as_ptr ( ) . add ( next) ,
388
+ self . as_mut_ptr ( ) . add ( idx) ,
389
+ len - next,
390
+ ) ;
378
391
self . set_len ( len - ( next - idx) ) ;
379
392
}
380
393
ch
@@ -419,8 +432,7 @@ impl<const CAP: usize> ArrayString<CAP>
419
432
}
420
433
}
421
434
422
- impl < const CAP : usize > Deref for ArrayString < CAP >
423
- {
435
+ impl < const CAP : usize > Deref for ArrayString < CAP > {
424
436
type Target = str ;
425
437
#[ inline]
426
438
fn deref ( & self ) -> & str {
@@ -431,8 +443,7 @@ impl<const CAP: usize> Deref for ArrayString<CAP>
431
443
}
432
444
}
433
445
434
- impl < const CAP : usize > DerefMut for ArrayString < CAP >
435
- {
446
+ impl < const CAP : usize > DerefMut for ArrayString < CAP > {
436
447
#[ inline]
437
448
fn deref_mut ( & mut self ) -> & mut str {
438
449
unsafe {
@@ -443,60 +454,58 @@ impl<const CAP: usize> DerefMut for ArrayString<CAP>
443
454
}
444
455
}
445
456
446
- impl < const CAP : usize > PartialEq for ArrayString < CAP >
447
- {
457
+ impl < const CAP : usize > PartialEq for ArrayString < CAP > {
448
458
fn eq ( & self , rhs : & Self ) -> bool {
449
459
* * self == * * rhs
450
460
}
451
461
}
452
462
453
- impl < const CAP : usize > PartialEq < str > for ArrayString < CAP >
454
- {
463
+ impl < const CAP : usize > PartialEq < str > for ArrayString < CAP > {
455
464
fn eq ( & self , rhs : & str ) -> bool {
456
465
& * * self == rhs
457
466
}
458
467
}
459
468
460
- impl < const CAP : usize > PartialEq < ArrayString < CAP > > for str
461
- {
469
+ impl < const CAP : usize > PartialEq < ArrayString < CAP > > for str {
462
470
fn eq ( & self , rhs : & ArrayString < CAP > ) -> bool {
463
471
self == & * * rhs
464
472
}
465
473
}
466
474
467
- impl < const CAP : usize > Eq for ArrayString < CAP >
468
- { }
475
+ impl < const CAP : usize > Eq for ArrayString < CAP > { }
469
476
470
- impl < const CAP : usize > Hash for ArrayString < CAP >
471
- {
477
+ impl < const CAP : usize > Hash for ArrayString < CAP > {
472
478
fn hash < H : Hasher > ( & self , h : & mut H ) {
473
479
( * * self ) . hash ( h)
474
480
}
475
481
}
476
482
477
- impl < const CAP : usize > Borrow < str > for ArrayString < CAP >
478
- {
479
- fn borrow ( & self ) -> & str { self }
483
+ impl < const CAP : usize > Borrow < str > for ArrayString < CAP > {
484
+ fn borrow ( & self ) -> & str {
485
+ self
486
+ }
480
487
}
481
488
482
- impl < const CAP : usize > AsRef < str > for ArrayString < CAP >
483
- {
484
- fn as_ref ( & self ) -> & str { self }
489
+ impl < const CAP : usize > AsRef < str > for ArrayString < CAP > {
490
+ fn as_ref ( & self ) -> & str {
491
+ self
492
+ }
485
493
}
486
494
487
- impl < const CAP : usize > fmt:: Debug for ArrayString < CAP >
488
- {
489
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
495
+ impl < const CAP : usize > fmt:: Debug for ArrayString < CAP > {
496
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
497
+ ( * * self ) . fmt ( f)
498
+ }
490
499
}
491
500
492
- impl < const CAP : usize > fmt:: Display for ArrayString < CAP >
493
- {
494
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result { ( * * self ) . fmt ( f) }
501
+ impl < const CAP : usize > fmt:: Display for ArrayString < CAP > {
502
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
503
+ ( * * self ) . fmt ( f)
504
+ }
495
505
}
496
506
497
507
/// `Write` appends written data to the end of the string.
498
- impl < const CAP : usize > fmt:: Write for ArrayString < CAP >
499
- {
508
+ impl < const CAP : usize > fmt:: Write for ArrayString < CAP > {
500
509
fn write_char ( & mut self , c : char ) -> fmt:: Result {
501
510
self . try_push ( c) . map_err ( |_| fmt:: Error )
502
511
}
@@ -506,8 +515,7 @@ impl<const CAP: usize> fmt::Write for ArrayString<CAP>
506
515
}
507
516
}
508
517
509
- impl < const CAP : usize > Clone for ArrayString < CAP >
510
- {
518
+ impl < const CAP : usize > Clone for ArrayString < CAP > {
511
519
fn clone ( & self ) -> ArrayString < CAP > {
512
520
* self
513
521
}
@@ -518,8 +526,8 @@ impl<const CAP: usize> Clone for ArrayString<CAP>
518
526
}
519
527
}
520
528
521
- impl < const CAP : usize > PartialOrd for ArrayString < CAP >
522
- {
529
+ # [ cfg_attr ( rustfmt , rustfmt :: skip ) ]
530
+ impl < const CAP : usize > PartialOrd for ArrayString < CAP > {
523
531
fn partial_cmp ( & self , rhs : & Self ) -> Option < cmp:: Ordering > {
524
532
( * * self ) . partial_cmp ( & * * rhs)
525
533
}
@@ -529,8 +537,8 @@ impl<const CAP: usize> PartialOrd for ArrayString<CAP>
529
537
fn ge ( & self , rhs : & Self ) -> bool { * * self >= * * rhs }
530
538
}
531
539
532
- impl < const CAP : usize > PartialOrd < str > for ArrayString < CAP >
533
- {
540
+ # [ cfg_attr ( rustfmt , rustfmt :: skip ) ]
541
+ impl < const CAP : usize > PartialOrd < str > for ArrayString < CAP > {
534
542
fn partial_cmp ( & self , rhs : & str ) -> Option < cmp:: Ordering > {
535
543
( * * self ) . partial_cmp ( rhs)
536
544
}
@@ -540,8 +548,8 @@ impl<const CAP: usize> PartialOrd<str> for ArrayString<CAP>
540
548
fn ge ( & self , rhs : & str ) -> bool { & * * self >= rhs }
541
549
}
542
550
543
- impl < const CAP : usize > PartialOrd < ArrayString < CAP > > for str
544
- {
551
+ # [ cfg_attr ( rustfmt , rustfmt :: skip ) ]
552
+ impl < const CAP : usize > PartialOrd < ArrayString < CAP > > for str {
545
553
fn partial_cmp ( & self , rhs : & ArrayString < CAP > ) -> Option < cmp:: Ordering > {
546
554
self . partial_cmp ( & * * rhs)
547
555
}
@@ -551,39 +559,37 @@ impl<const CAP: usize> PartialOrd<ArrayString<CAP>> for str
551
559
fn ge ( & self , rhs : & ArrayString < CAP > ) -> bool { self >= & * * rhs }
552
560
}
553
561
554
- impl < const CAP : usize > Ord for ArrayString < CAP >
555
- {
562
+ impl < const CAP : usize > Ord for ArrayString < CAP > {
556
563
fn cmp ( & self , rhs : & Self ) -> cmp:: Ordering {
557
564
( * * self ) . cmp ( & * * rhs)
558
565
}
559
566
}
560
567
561
- impl < const CAP : usize > FromStr for ArrayString < CAP >
562
- {
568
+ impl < const CAP : usize > FromStr for ArrayString < CAP > {
563
569
type Err = CapacityError ;
564
570
565
571
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
566
572
Self :: from ( s) . map_err ( CapacityError :: simplify)
567
573
}
568
574
}
569
575
570
- #[ cfg( feature= "serde" ) ]
576
+ #[ cfg( feature = "serde" ) ]
571
577
/// Requires crate feature `"serde"`
572
- impl < const CAP : usize > Serialize for ArrayString < CAP >
573
- {
578
+ impl < const CAP : usize > Serialize for ArrayString < CAP > {
574
579
fn serialize < S > ( & self , serializer : S ) -> Result < S :: Ok , S :: Error >
575
- where S : Serializer
580
+ where
581
+ S : Serializer ,
576
582
{
577
583
serializer. serialize_str ( & * self )
578
584
}
579
585
}
580
586
581
- #[ cfg( feature= "serde" ) ]
587
+ #[ cfg( feature = "serde" ) ]
582
588
/// Requires crate feature `"serde"`
583
- impl < ' de , const CAP : usize > Deserialize < ' de > for ArrayString < CAP >
584
- {
589
+ impl < ' de , const CAP : usize > Deserialize < ' de > for ArrayString < CAP > {
585
590
fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
586
- where D : Deserializer < ' de >
591
+ where
592
+ D : Deserializer < ' de > ,
587
593
{
588
594
use serde:: de:: { self , Visitor } ;
589
595
use std:: marker:: PhantomData ;
@@ -598,15 +604,18 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
598
604
}
599
605
600
606
fn visit_str < E > ( self , v : & str ) -> Result < Self :: Value , E >
601
- where E : de:: Error ,
607
+ where
608
+ E : de:: Error ,
602
609
{
603
610
ArrayString :: from ( v) . map_err ( |_| E :: invalid_length ( v. len ( ) , & self ) )
604
611
}
605
612
606
613
fn visit_bytes < E > ( self , v : & [ u8 ] ) -> Result < Self :: Value , E >
607
- where E : de:: Error ,
614
+ where
615
+ E : de:: Error ,
608
616
{
609
- let s = str:: from_utf8 ( v) . map_err ( |_| E :: invalid_value ( de:: Unexpected :: Bytes ( v) , & self ) ) ?;
617
+ let s = str:: from_utf8 ( v)
618
+ . map_err ( |_| E :: invalid_value ( de:: Unexpected :: Bytes ( v) , & self ) ) ?;
610
619
611
620
ArrayString :: from ( s) . map_err ( |_| E :: invalid_length ( s. len ( ) , & self ) )
612
621
}
@@ -616,8 +625,7 @@ impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
616
625
}
617
626
}
618
627
619
- impl < ' a , const CAP : usize > TryFrom < & ' a str > for ArrayString < CAP >
620
- {
628
+ impl < ' a , const CAP : usize > TryFrom < & ' a str > for ArrayString < CAP > {
621
629
type Error = CapacityError < & ' a str > ;
622
630
623
631
fn try_from ( f : & ' a str ) -> Result < Self , Self :: Error > {
@@ -627,8 +635,7 @@ impl<'a, const CAP: usize> TryFrom<&'a str> for ArrayString<CAP>
627
635
}
628
636
}
629
637
630
- impl < ' a , const CAP : usize > TryFrom < fmt:: Arguments < ' a > > for ArrayString < CAP >
631
- {
638
+ impl < ' a , const CAP : usize > TryFrom < fmt:: Arguments < ' a > > for ArrayString < CAP > {
632
639
type Error = CapacityError < fmt:: Error > ;
633
640
634
641
fn try_from ( f : fmt:: Arguments < ' a > ) -> Result < Self , Self :: Error > {
0 commit comments