@@ -13,7 +13,7 @@ use core::{
13
13
14
14
use crate :: {
15
15
storage:: { OwnedStorage , Storage , ViewStorage } ,
16
- vec:: VecInner ,
16
+ vec:: { CapacityError , VecInner } ,
17
17
Vec ,
18
18
} ;
19
19
@@ -28,20 +28,28 @@ pub use drain::Drain;
28
28
#[ derive( Debug ) ]
29
29
pub enum FromUtf16Error {
30
30
/// The capacity of the `String` is too small for the given operation.
31
- Capacity ,
31
+ Capacity ( CapacityError ) ,
32
32
/// Error decoding UTF-16.
33
33
DecodeUtf16Error ( DecodeUtf16Error ) ,
34
34
}
35
35
36
36
impl fmt:: Display for FromUtf16Error {
37
37
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
38
38
match self {
39
- Self :: Capacity => "insufficient capacity" . fmt ( f ) ,
39
+ Self :: Capacity ( e ) => write ! ( f , "{}" , e ) ,
40
40
Self :: DecodeUtf16Error ( e) => write ! ( f, "invalid UTF-16: {}" , e) ,
41
41
}
42
42
}
43
43
}
44
44
45
+ impl core:: error:: Error for FromUtf16Error { }
46
+
47
+ impl From < CapacityError > for FromUtf16Error {
48
+ fn from ( e : CapacityError ) -> Self {
49
+ FromUtf16Error :: Capacity ( e)
50
+ }
51
+ }
52
+
45
53
/// Base struct for [`String`] and [`StringView`], generic over the [`Storage`].
46
54
///
47
55
/// In most cases you should use [`String`] or [`StringView`] directly. Only use this
@@ -168,7 +176,7 @@ impl<const N: usize> String<N> {
168
176
for c in char:: decode_utf16 ( v. iter ( ) . cloned ( ) ) {
169
177
match c {
170
178
Ok ( c) => {
171
- s. push ( c) . map_err ( |_| FromUtf16Error :: Capacity ) ?;
179
+ s. push ( c) ?;
172
180
}
173
181
Err ( err) => {
174
182
return Err ( FromUtf16Error :: DecodeUtf16Error ( err) ) ;
@@ -257,7 +265,7 @@ impl<const N: usize> String<N> {
257
265
/// assert!(b.len() == 2);
258
266
///
259
267
/// assert_eq!(&[b'a', b'b'], &b[..]);
260
- /// # Ok::<(), () >(())
268
+ /// # Ok::<(), heapless::vec::CapacityError >(())
261
269
/// ```
262
270
#[ inline]
263
271
pub fn into_bytes ( self ) -> Vec < u8 , N > {
@@ -364,7 +372,7 @@ impl<S: Storage> StringInner<S> {
364
372
///
365
373
/// let _s = s.as_str();
366
374
/// // s.push('c'); // <- cannot borrow `s` as mutable because it is also borrowed as immutable
367
- /// # Ok::<(), () >(())
375
+ /// # Ok::<(), heapless::vec::CapacityError >(())
368
376
/// ```
369
377
#[ inline]
370
378
pub fn as_str ( & self ) -> & str {
@@ -383,7 +391,7 @@ impl<S: Storage> StringInner<S> {
383
391
/// let mut s: String<4> = String::try_from("ab")?;
384
392
/// let s = s.as_mut_str();
385
393
/// s.make_ascii_uppercase();
386
- /// # Ok::<(), () >(())
394
+ /// # Ok::<(), heapless::vec::CapacityError >(())
387
395
/// ```
388
396
#[ inline]
389
397
pub fn as_mut_str ( & mut self ) -> & mut str {
@@ -415,7 +423,7 @@ impl<S: Storage> StringInner<S> {
415
423
/// vec.reverse();
416
424
/// }
417
425
/// assert_eq!(s, "olleh");
418
- /// # Ok::<(), () >(())
426
+ /// # Ok::<(), heapless::vec::CapacityError >(())
419
427
/// ```
420
428
pub unsafe fn as_mut_vec ( & mut self ) -> & mut VecInner < u8 , S > {
421
429
& mut self . vec
@@ -437,11 +445,10 @@ impl<S: Storage> StringInner<S> {
437
445
/// assert_eq!("foobar", s);
438
446
///
439
447
/// assert!(s.push_str("tender").is_err());
440
- /// # Ok::<(), () >(())
448
+ /// # Ok::<(), heapless::vec::CapacityError >(())
441
449
/// ```
442
450
#[ inline]
443
- #[ allow( clippy:: result_unit_err) ]
444
- pub fn push_str ( & mut self , string : & str ) -> Result < ( ) , ( ) > {
451
+ pub fn push_str ( & mut self , string : & str ) -> Result < ( ) , CapacityError > {
445
452
self . vec . extend_from_slice ( string. as_bytes ( ) )
446
453
}
447
454
@@ -480,13 +487,12 @@ impl<S: Storage> StringInner<S> {
480
487
/// assert!("abc123" == s.as_str());
481
488
///
482
489
/// assert_eq!("abc123", s);
483
- /// # Ok::<(), () >(())
490
+ /// # Ok::<(), heapless::vec::CapacityError >(())
484
491
/// ```
485
492
#[ inline]
486
- #[ allow( clippy:: result_unit_err) ]
487
- pub fn push ( & mut self , c : char ) -> Result < ( ) , ( ) > {
493
+ pub fn push ( & mut self , c : char ) -> Result < ( ) , CapacityError > {
488
494
match c. len_utf8 ( ) {
489
- 1 => self . vec . push ( c as u8 ) . map_err ( |_| { } ) ,
495
+ 1 => self . vec . push ( c as u8 ) . map_err ( |_| CapacityError { } ) ,
490
496
_ => self
491
497
. vec
492
498
. extend_from_slice ( c. encode_utf8 ( & mut [ 0 ; 4 ] ) . as_bytes ( ) ) ,
@@ -517,7 +523,7 @@ impl<S: Storage> StringInner<S> {
517
523
/// s.truncate(2);
518
524
///
519
525
/// assert_eq!("he", s);
520
- /// # Ok::<(), () >(())
526
+ /// # Ok::<(), heapless::vec::CapacityError >(())
521
527
/// ```
522
528
#[ inline]
523
529
pub fn truncate ( & mut self , new_len : usize ) {
@@ -545,7 +551,7 @@ impl<S: Storage> StringInner<S> {
545
551
/// assert_eq!(s.pop(), Some('f'));
546
552
///
547
553
/// assert_eq!(s.pop(), None);
548
- /// Ok::<(), () >(())
554
+ /// # Ok::<(), heapless::vec::CapacityError >(())
549
555
/// ```
550
556
pub fn pop ( & mut self ) -> Option < char > {
551
557
let ch = self . chars ( ) . next_back ( ) ?;
@@ -577,11 +583,12 @@ impl<S: Storage> StringInner<S> {
577
583
/// ```
578
584
/// use heapless::String;
579
585
///
580
- /// let mut s: String<8> = String::try_from("foo").unwrap() ;
586
+ /// let mut s: String<8> = String::try_from("foo")? ;
581
587
///
582
588
/// assert_eq!(s.remove(0), 'f');
583
589
/// assert_eq!(s.remove(1), 'o');
584
590
/// assert_eq!(s.remove(0), 'o');
591
+ /// # Ok::<(), heapless::vec::CapacityError>(())
585
592
/// ```
586
593
#[ inline]
587
594
pub fn remove ( & mut self , index : usize ) -> char {
@@ -619,7 +626,7 @@ impl<S: Storage> StringInner<S> {
619
626
/// assert!(s.is_empty());
620
627
/// assert_eq!(0, s.len());
621
628
/// assert_eq!(8, s.capacity());
622
- /// Ok::<(), () >(())
629
+ /// # Ok::<(), heapless::vec::CapacityError >(())
623
630
/// ```
624
631
#[ inline]
625
632
pub fn clear ( & mut self ) {
@@ -634,7 +641,7 @@ impl<const N: usize> Default for String<N> {
634
641
}
635
642
636
643
impl < ' a , const N : usize > TryFrom < & ' a str > for String < N > {
637
- type Error = ( ) ;
644
+ type Error = CapacityError ;
638
645
fn try_from ( s : & ' a str ) -> Result < Self , Self :: Error > {
639
646
let mut new = String :: new ( ) ;
640
647
new. push_str ( s) ?;
@@ -643,7 +650,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
643
650
}
644
651
645
652
impl < const N : usize > str:: FromStr for String < N > {
646
- type Err = ( ) ;
653
+ type Err = CapacityError ;
647
654
648
655
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
649
656
let mut new = String :: new ( ) ;
@@ -912,7 +919,7 @@ impl_try_from_num!(u64, 20);
912
919
913
920
#[ cfg( test) ]
914
921
mod tests {
915
- use crate :: { String , Vec } ;
922
+ use crate :: { vec :: CapacityError , String , Vec } ;
916
923
917
924
#[ test]
918
925
fn static_new ( ) {
@@ -980,7 +987,7 @@ mod tests {
980
987
assert ! ( s. len( ) == 3 ) ;
981
988
assert_eq ! ( s, "123" ) ;
982
989
983
- let _: ( ) = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
990
+ let _: CapacityError = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
984
991
}
985
992
986
993
#[ test]
@@ -991,7 +998,7 @@ mod tests {
991
998
assert ! ( s. len( ) == 3 ) ;
992
999
assert_eq ! ( s, "123" ) ;
993
1000
994
- let _: ( ) = String :: < 2 > :: from_str ( "123" ) . unwrap_err ( ) ;
1001
+ let _: CapacityError = String :: < 2 > :: from_str ( "123" ) . unwrap_err ( ) ;
995
1002
}
996
1003
997
1004
#[ test]
0 commit comments