@@ -12,6 +12,7 @@ use core::{
12
12
} ;
13
13
14
14
use crate :: vec:: { OwnedVecStorage , Vec , VecInner , VecStorage , ViewVecStorage } ;
15
+ use crate :: CapacityError ;
15
16
16
17
mod drain;
17
18
pub use drain:: Drain ;
@@ -24,20 +25,28 @@ pub use drain::Drain;
24
25
#[ derive( Debug ) ]
25
26
pub enum FromUtf16Error {
26
27
/// The capacity of the `String` is too small for the given operation.
27
- Capacity ,
28
+ Capacity ( CapacityError ) ,
28
29
/// Error decoding UTF-16.
29
- DecodeUtf16Error ( DecodeUtf16Error ) ,
30
+ DecodeUtf16 ( DecodeUtf16Error ) ,
30
31
}
31
32
32
33
impl fmt:: Display for FromUtf16Error {
33
34
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
34
35
match self {
35
- Self :: Capacity => "insufficient capacity" . fmt ( f ) ,
36
- Self :: DecodeUtf16Error ( e ) => write ! ( f, "invalid UTF-16: {}" , e ) ,
36
+ Self :: Capacity ( err ) => write ! ( f , "{err}" ) ,
37
+ Self :: DecodeUtf16 ( err ) => write ! ( f, "invalid UTF-16: {err}" ) ,
37
38
}
38
39
}
39
40
}
40
41
42
+ impl core:: error:: Error for FromUtf16Error { }
43
+
44
+ impl From < CapacityError > for FromUtf16Error {
45
+ fn from ( e : CapacityError ) -> Self {
46
+ Self :: Capacity ( e)
47
+ }
48
+ }
49
+
41
50
/// Base struct for [`String`] and [`StringView`], generic over the [`VecStorage`].
42
51
///
43
52
/// In most cases you should use [`String`] or [`StringView`] directly. Only use this
@@ -164,10 +173,10 @@ impl<const N: usize> String<N> {
164
173
for c in char:: decode_utf16 ( v. iter ( ) . cloned ( ) ) {
165
174
match c {
166
175
Ok ( c) => {
167
- s. push ( c) . map_err ( |_| FromUtf16Error :: Capacity ) ?;
176
+ s. push ( c) . map_err ( |_| CapacityError ) ?;
168
177
}
169
178
Err ( err) => {
170
- return Err ( FromUtf16Error :: DecodeUtf16Error ( err) ) ;
179
+ return Err ( FromUtf16Error :: DecodeUtf16 ( err) ) ;
171
180
}
172
181
}
173
182
}
@@ -253,7 +262,7 @@ impl<const N: usize> String<N> {
253
262
/// assert!(b.len() == 2);
254
263
///
255
264
/// assert_eq!(&[b'a', b'b'], &b[..]);
256
- /// # Ok::<(), () >(())
265
+ /// # Ok::<(), heapless::CapacityError >(())
257
266
/// ```
258
267
#[ inline]
259
268
pub fn into_bytes ( self ) -> Vec < u8 , N > {
@@ -360,7 +369,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
360
369
///
361
370
/// let _s = s.as_str();
362
371
/// // s.push('c'); // <- cannot borrow `s` as mutable because it is also borrowed as immutable
363
- /// # Ok::<(), () >(())
372
+ /// # Ok::<(), heapless::CapacityError >(())
364
373
/// ```
365
374
#[ inline]
366
375
pub fn as_str ( & self ) -> & str {
@@ -379,7 +388,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
379
388
/// let mut s: String<4> = String::try_from("ab")?;
380
389
/// let s = s.as_mut_str();
381
390
/// s.make_ascii_uppercase();
382
- /// # Ok::<(), () >(())
391
+ /// # Ok::<(), heapless::CapacityError >(())
383
392
/// ```
384
393
#[ inline]
385
394
pub fn as_mut_str ( & mut self ) -> & mut str {
@@ -411,7 +420,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
411
420
/// vec.reverse();
412
421
/// }
413
422
/// assert_eq!(s, "olleh");
414
- /// # Ok::<(), () >(())
423
+ /// # Ok::<(), heapless::CapacityError >(())
415
424
/// ```
416
425
pub unsafe fn as_mut_vec ( & mut self ) -> & mut VecInner < u8 , S > {
417
426
& mut self . vec
@@ -433,11 +442,10 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
433
442
/// assert_eq!("foobar", s);
434
443
///
435
444
/// assert!(s.push_str("tender").is_err());
436
- /// # Ok::<(), () >(())
445
+ /// # Ok::<(), heapless::CapacityError >(())
437
446
/// ```
438
447
#[ inline]
439
- #[ allow( clippy:: result_unit_err) ]
440
- pub fn push_str ( & mut self , string : & str ) -> Result < ( ) , ( ) > {
448
+ pub fn push_str ( & mut self , string : & str ) -> Result < ( ) , CapacityError > {
441
449
self . vec . extend_from_slice ( string. as_bytes ( ) )
442
450
}
443
451
@@ -476,13 +484,12 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
476
484
/// assert!("abc123" == s.as_str());
477
485
///
478
486
/// assert_eq!("abc123", s);
479
- /// # Ok::<(), () >(())
487
+ /// # Ok::<(), heapless::CapacityError >(())
480
488
/// ```
481
489
#[ inline]
482
- #[ allow( clippy:: result_unit_err) ]
483
- pub fn push ( & mut self , c : char ) -> Result < ( ) , ( ) > {
490
+ pub fn push ( & mut self , c : char ) -> Result < ( ) , CapacityError > {
484
491
match c. len_utf8 ( ) {
485
- 1 => self . vec . push ( c as u8 ) . map_err ( |_| { } ) ,
492
+ 1 => self . vec . push ( c as u8 ) . map_err ( |_| CapacityError ) ,
486
493
_ => self
487
494
. vec
488
495
. extend_from_slice ( c. encode_utf8 ( & mut [ 0 ; 4 ] ) . as_bytes ( ) ) ,
@@ -513,7 +520,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
513
520
/// s.truncate(2);
514
521
///
515
522
/// assert_eq!("he", s);
516
- /// # Ok::<(), () >(())
523
+ /// # Ok::<(), heapless::CapacityError >(())
517
524
/// ```
518
525
#[ inline]
519
526
pub fn truncate ( & mut self , new_len : usize ) {
@@ -541,7 +548,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
541
548
/// assert_eq!(s.pop(), Some('f'));
542
549
///
543
550
/// assert_eq!(s.pop(), None);
544
- /// Ok::<(), () >(())
551
+ /// Ok::<(), heapless::CapacityError >(())
545
552
/// ```
546
553
pub fn pop ( & mut self ) -> Option < char > {
547
554
let ch = self . chars ( ) . next_back ( ) ?;
@@ -615,7 +622,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
615
622
/// assert!(s.is_empty());
616
623
/// assert_eq!(0, s.len());
617
624
/// assert_eq!(8, s.capacity());
618
- /// Ok::<(), () >(())
625
+ /// Ok::<(), heapless::CapacityError >(())
619
626
/// ```
620
627
#[ inline]
621
628
pub fn clear ( & mut self ) {
@@ -630,7 +637,8 @@ impl<const N: usize> Default for String<N> {
630
637
}
631
638
632
639
impl < ' a , const N : usize > TryFrom < & ' a str > for String < N > {
633
- type Error = ( ) ;
640
+ type Error = CapacityError ;
641
+
634
642
fn try_from ( s : & ' a str ) -> Result < Self , Self :: Error > {
635
643
let mut new = Self :: new ( ) ;
636
644
new. push_str ( s) ?;
@@ -639,7 +647,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
639
647
}
640
648
641
649
impl < const N : usize > str:: FromStr for String < N > {
642
- type Err = ( ) ;
650
+ type Err = CapacityError ;
643
651
644
652
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
645
653
let mut new = Self :: new ( ) ;
@@ -912,7 +920,7 @@ impl_try_from_num!(u64, 20);
912
920
913
921
#[ cfg( test) ]
914
922
mod tests {
915
- use crate :: { String , Vec } ;
923
+ use crate :: { CapacityError , String , Vec } ;
916
924
917
925
#[ test]
918
926
fn static_new ( ) {
@@ -980,7 +988,7 @@ mod tests {
980
988
assert ! ( s. len( ) == 3 ) ;
981
989
assert_eq ! ( s, "123" ) ;
982
990
983
- let _: ( ) = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
991
+ let _: CapacityError = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
984
992
}
985
993
986
994
#[ test]
@@ -991,7 +999,7 @@ mod tests {
991
999
assert ! ( s. len( ) == 3 ) ;
992
1000
assert_eq ! ( s, "123" ) ;
993
1001
994
- let _: ( ) = String :: < 2 > :: from_str ( "123" ) . unwrap_err ( ) ;
1002
+ let _: CapacityError = String :: < 2 > :: from_str ( "123" ) . unwrap_err ( ) ;
995
1003
}
996
1004
997
1005
#[ test]
0 commit comments