@@ -12,6 +12,7 @@ use core::{
1212} ;
1313
1414use crate :: vec:: { OwnedVecStorage , Vec , VecInner , VecStorage , ViewVecStorage } ;
15+ use crate :: CapacityError ;
1516
1617mod drain;
1718pub use drain:: Drain ;
@@ -24,20 +25,28 @@ pub use drain::Drain;
2425#[ derive( Debug ) ]
2526pub enum FromUtf16Error {
2627 /// The capacity of the `String` is too small for the given operation.
27- Capacity ,
28+ Capacity ( CapacityError ) ,
2829 /// Error decoding UTF-16.
29- DecodeUtf16Error ( DecodeUtf16Error ) ,
30+ DecodeUtf16 ( DecodeUtf16Error ) ,
3031}
3132
3233impl fmt:: Display for FromUtf16Error {
3334 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
3435 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}" ) ,
3738 }
3839 }
3940}
4041
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+
4150/// Base struct for [`String`] and [`StringView`], generic over the [`VecStorage`].
4251///
4352/// In most cases you should use [`String`] or [`StringView`] directly. Only use this
@@ -164,10 +173,10 @@ impl<const N: usize> String<N> {
164173 for c in char:: decode_utf16 ( v. iter ( ) . cloned ( ) ) {
165174 match c {
166175 Ok ( c) => {
167- s. push ( c) . map_err ( |_| FromUtf16Error :: Capacity ) ?;
176+ s. push ( c) . map_err ( |_| CapacityError ) ?;
168177 }
169178 Err ( err) => {
170- return Err ( FromUtf16Error :: DecodeUtf16Error ( err) ) ;
179+ return Err ( FromUtf16Error :: DecodeUtf16 ( err) ) ;
171180 }
172181 }
173182 }
@@ -253,7 +262,7 @@ impl<const N: usize> String<N> {
253262 /// assert!(b.len() == 2);
254263 ///
255264 /// assert_eq!(&[b'a', b'b'], &b[..]);
256- /// # Ok::<(), () >(())
265+ /// # Ok::<(), heapless::CapacityError >(())
257266 /// ```
258267 #[ inline]
259268 pub fn into_bytes ( self ) -> Vec < u8 , N > {
@@ -360,7 +369,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
360369 ///
361370 /// let _s = s.as_str();
362371 /// // s.push('c'); // <- cannot borrow `s` as mutable because it is also borrowed as immutable
363- /// # Ok::<(), () >(())
372+ /// # Ok::<(), heapless::CapacityError >(())
364373 /// ```
365374 #[ inline]
366375 pub fn as_str ( & self ) -> & str {
@@ -379,7 +388,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
379388 /// let mut s: String<4> = String::try_from("ab")?;
380389 /// let s = s.as_mut_str();
381390 /// s.make_ascii_uppercase();
382- /// # Ok::<(), () >(())
391+ /// # Ok::<(), heapless::CapacityError >(())
383392 /// ```
384393 #[ inline]
385394 pub fn as_mut_str ( & mut self ) -> & mut str {
@@ -411,7 +420,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
411420 /// vec.reverse();
412421 /// }
413422 /// assert_eq!(s, "olleh");
414- /// # Ok::<(), () >(())
423+ /// # Ok::<(), heapless::CapacityError >(())
415424 /// ```
416425 pub unsafe fn as_mut_vec ( & mut self ) -> & mut VecInner < u8 , S > {
417426 & mut self . vec
@@ -433,11 +442,10 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
433442 /// assert_eq!("foobar", s);
434443 ///
435444 /// assert!(s.push_str("tender").is_err());
436- /// # Ok::<(), () >(())
445+ /// # Ok::<(), heapless::CapacityError >(())
437446 /// ```
438447 #[ 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 > {
441449 self . vec . extend_from_slice ( string. as_bytes ( ) )
442450 }
443451
@@ -476,13 +484,12 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
476484 /// assert!("abc123" == s.as_str());
477485 ///
478486 /// assert_eq!("abc123", s);
479- /// # Ok::<(), () >(())
487+ /// # Ok::<(), heapless::CapacityError >(())
480488 /// ```
481489 #[ 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 > {
484491 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 ) ,
486493 _ => self
487494 . vec
488495 . extend_from_slice ( c. encode_utf8 ( & mut [ 0 ; 4 ] ) . as_bytes ( ) ) ,
@@ -513,7 +520,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
513520 /// s.truncate(2);
514521 ///
515522 /// assert_eq!("he", s);
516- /// # Ok::<(), () >(())
523+ /// # Ok::<(), heapless::CapacityError >(())
517524 /// ```
518525 #[ inline]
519526 pub fn truncate ( & mut self , new_len : usize ) {
@@ -541,7 +548,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
541548 /// assert_eq!(s.pop(), Some('f'));
542549 ///
543550 /// assert_eq!(s.pop(), None);
544- /// Ok::<(), () >(())
551+ /// Ok::<(), heapless::CapacityError >(())
545552 /// ```
546553 pub fn pop ( & mut self ) -> Option < char > {
547554 let ch = self . chars ( ) . next_back ( ) ?;
@@ -615,7 +622,7 @@ impl<S: VecStorage<u8> + ?Sized> StringInner<S> {
615622 /// assert!(s.is_empty());
616623 /// assert_eq!(0, s.len());
617624 /// assert_eq!(8, s.capacity());
618- /// Ok::<(), () >(())
625+ /// Ok::<(), heapless::CapacityError >(())
619626 /// ```
620627 #[ inline]
621628 pub fn clear ( & mut self ) {
@@ -630,7 +637,8 @@ impl<const N: usize> Default for String<N> {
630637}
631638
632639impl < ' a , const N : usize > TryFrom < & ' a str > for String < N > {
633- type Error = ( ) ;
640+ type Error = CapacityError ;
641+
634642 fn try_from ( s : & ' a str ) -> Result < Self , Self :: Error > {
635643 let mut new = Self :: new ( ) ;
636644 new. push_str ( s) ?;
@@ -639,7 +647,7 @@ impl<'a, const N: usize> TryFrom<&'a str> for String<N> {
639647}
640648
641649impl < const N : usize > str:: FromStr for String < N > {
642- type Err = ( ) ;
650+ type Err = CapacityError ;
643651
644652 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
645653 let mut new = Self :: new ( ) ;
@@ -912,7 +920,7 @@ impl_try_from_num!(u64, 20);
912920
913921#[ cfg( test) ]
914922mod tests {
915- use crate :: { String , Vec } ;
923+ use crate :: { CapacityError , String , Vec } ;
916924
917925 #[ test]
918926 fn static_new ( ) {
@@ -980,7 +988,7 @@ mod tests {
980988 assert ! ( s. len( ) == 3 ) ;
981989 assert_eq ! ( s, "123" ) ;
982990
983- let _: ( ) = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
991+ let _: CapacityError = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
984992 }
985993
986994 #[ test]
@@ -991,7 +999,7 @@ mod tests {
991999 assert ! ( s. len( ) == 3 ) ;
9921000 assert_eq ! ( s, "123" ) ;
9931001
994- let _: ( ) = String :: < 2 > :: from_str ( "123" ) . unwrap_err ( ) ;
1002+ let _: CapacityError = String :: < 2 > :: from_str ( "123" ) . unwrap_err ( ) ;
9951003 }
9961004
9971005 #[ test]
0 commit comments