@@ -13,7 +13,7 @@ use core::{
1313
1414use crate :: {
1515 storage:: { OwnedStorage , Storage , ViewStorage } ,
16- vec:: VecInner ,
16+ vec:: { CapacityError , VecInner } ,
1717 Vec ,
1818} ;
1919
@@ -28,20 +28,28 @@ pub use drain::Drain;
2828#[ derive( Debug ) ]
2929pub enum FromUtf16Error {
3030 /// The capacity of the `String` is too small for the given operation.
31- Capacity ,
31+ Capacity ( CapacityError ) ,
3232 /// Error decoding UTF-16.
3333 DecodeUtf16Error ( DecodeUtf16Error ) ,
3434}
3535
3636impl fmt:: Display for FromUtf16Error {
3737 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3838 match self {
39- Self :: Capacity => "insufficient capacity" . fmt ( f ) ,
39+ Self :: Capacity ( e ) => write ! ( f , "{}" , e ) ,
4040 Self :: DecodeUtf16Error ( e) => write ! ( f, "invalid UTF-16: {}" , e) ,
4141 }
4242 }
4343}
4444
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+
4553/// Base struct for [`String`] and [`StringView`], generic over the [`Storage`].
4654///
4755/// In most cases you should use [`String`] or [`StringView`] directly. Only use this
@@ -168,7 +176,7 @@ impl<const N: usize> String<N> {
168176 for c in char:: decode_utf16 ( v. iter ( ) . cloned ( ) ) {
169177 match c {
170178 Ok ( c) => {
171- s. push ( c) . map_err ( |_| FromUtf16Error :: Capacity ) ?;
179+ s. push ( c) ?;
172180 }
173181 Err ( err) => {
174182 return Err ( FromUtf16Error :: DecodeUtf16Error ( err) ) ;
@@ -257,7 +265,7 @@ impl<const N: usize> String<N> {
257265 /// assert!(b.len() == 2);
258266 ///
259267 /// assert_eq!(&[b'a', b'b'], &b[..]);
260- /// # Ok::<(), () >(())
268+ /// # Ok::<(), heapless::vec::CapacityError >(())
261269 /// ```
262270 #[ inline]
263271 pub fn into_bytes ( self ) -> Vec < u8 , N > {
@@ -364,7 +372,7 @@ impl<S: Storage> StringInner<S> {
364372 ///
365373 /// let _s = s.as_str();
366374 /// // s.push('c'); // <- cannot borrow `s` as mutable because it is also borrowed as immutable
367- /// # Ok::<(), () >(())
375+ /// # Ok::<(), heapless::vec::CapacityError >(())
368376 /// ```
369377 #[ inline]
370378 pub fn as_str ( & self ) -> & str {
@@ -383,7 +391,7 @@ impl<S: Storage> StringInner<S> {
383391 /// let mut s: String<4> = String::try_from("ab")?;
384392 /// let s = s.as_mut_str();
385393 /// s.make_ascii_uppercase();
386- /// # Ok::<(), () >(())
394+ /// # Ok::<(), heapless::vec::CapacityError >(())
387395 /// ```
388396 #[ inline]
389397 pub fn as_mut_str ( & mut self ) -> & mut str {
@@ -415,7 +423,7 @@ impl<S: Storage> StringInner<S> {
415423 /// vec.reverse();
416424 /// }
417425 /// assert_eq!(s, "olleh");
418- /// # Ok::<(), () >(())
426+ /// # Ok::<(), heapless::vec::CapacityError >(())
419427 /// ```
420428 pub unsafe fn as_mut_vec ( & mut self ) -> & mut VecInner < u8 , S > {
421429 & mut self . vec
@@ -437,11 +445,10 @@ impl<S: Storage> StringInner<S> {
437445 /// assert_eq!("foobar", s);
438446 ///
439447 /// assert!(s.push_str("tender").is_err());
440- /// # Ok::<(), () >(())
448+ /// # Ok::<(), heapless::vec::CapacityError >(())
441449 /// ```
442450 #[ 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 > {
445452 self . vec . extend_from_slice ( string. as_bytes ( ) )
446453 }
447454
@@ -480,13 +487,12 @@ impl<S: Storage> StringInner<S> {
480487 /// assert!("abc123" == s.as_str());
481488 ///
482489 /// assert_eq!("abc123", s);
483- /// # Ok::<(), () >(())
490+ /// # Ok::<(), heapless::vec::CapacityError >(())
484491 /// ```
485492 #[ 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 > {
488494 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 { } ) ,
490496 _ => self
491497 . vec
492498 . extend_from_slice ( c. encode_utf8 ( & mut [ 0 ; 4 ] ) . as_bytes ( ) ) ,
@@ -517,7 +523,7 @@ impl<S: Storage> StringInner<S> {
517523 /// s.truncate(2);
518524 ///
519525 /// assert_eq!("he", s);
520- /// # Ok::<(), () >(())
526+ /// # Ok::<(), heapless::vec::CapacityError >(())
521527 /// ```
522528 #[ inline]
523529 pub fn truncate ( & mut self , new_len : usize ) {
@@ -545,7 +551,7 @@ impl<S: Storage> StringInner<S> {
545551 /// assert_eq!(s.pop(), Some('f'));
546552 ///
547553 /// assert_eq!(s.pop(), None);
548- /// Ok::<(), () >(())
554+ /// # Ok::<(), heapless::vec::CapacityError >(())
549555 /// ```
550556 pub fn pop ( & mut self ) -> Option < char > {
551557 let ch = self . chars ( ) . next_back ( ) ?;
@@ -577,11 +583,12 @@ impl<S: Storage> StringInner<S> {
577583 /// ```
578584 /// use heapless::String;
579585 ///
580- /// let mut s: String<8> = String::try_from("foo").unwrap() ;
586+ /// let mut s: String<8> = String::try_from("foo")? ;
581587 ///
582588 /// assert_eq!(s.remove(0), 'f');
583589 /// assert_eq!(s.remove(1), 'o');
584590 /// assert_eq!(s.remove(0), 'o');
591+ /// # Ok::<(), heapless::vec::CapacityError>(())
585592 /// ```
586593 #[ inline]
587594 pub fn remove ( & mut self , index : usize ) -> char {
@@ -619,7 +626,7 @@ impl<S: Storage> StringInner<S> {
619626 /// assert!(s.is_empty());
620627 /// assert_eq!(0, s.len());
621628 /// assert_eq!(8, s.capacity());
622- /// Ok::<(), () >(())
629+ /// # Ok::<(), heapless::vec::CapacityError >(())
623630 /// ```
624631 #[ inline]
625632 pub fn clear ( & mut self ) {
@@ -634,7 +641,7 @@ impl<const N: usize> Default for String<N> {
634641}
635642
636643impl < ' a , const N : usize > TryFrom < & ' a str > for String < N > {
637- type Error = ( ) ;
644+ type Error = CapacityError ;
638645 fn try_from ( s : & ' a str ) -> Result < Self , Self :: Error > {
639646 let mut new = String :: new ( ) ;
640647 new. push_str ( s) ?;
@@ -643,7 +650,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
643650}
644651
645652impl < const N : usize > str:: FromStr for String < N > {
646- type Err = ( ) ;
653+ type Err = CapacityError ;
647654
648655 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
649656 let mut new = String :: new ( ) ;
@@ -912,7 +919,7 @@ impl_try_from_num!(u64, 20);
912919
913920#[ cfg( test) ]
914921mod tests {
915- use crate :: { String , Vec } ;
922+ use crate :: { vec :: CapacityError , String , Vec } ;
916923
917924 #[ test]
918925 fn static_new ( ) {
@@ -980,7 +987,7 @@ mod tests {
980987 assert ! ( s. len( ) == 3 ) ;
981988 assert_eq ! ( s, "123" ) ;
982989
983- let _: ( ) = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
990+ let _: CapacityError = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
984991 }
985992
986993 #[ test]
@@ -991,7 +998,7 @@ mod tests {
991998 assert ! ( s. len( ) == 3 ) ;
992999 assert_eq ! ( s, "123" ) ;
9931000
994- let _: ( ) = String :: < 2 > :: from_str ( "123" ) . unwrap_err ( ) ;
1001+ let _: CapacityError = String :: < 2 > :: from_str ( "123" ) . unwrap_err ( ) ;
9951002 }
9961003
9971004 #[ test]
0 commit comments