@@ -39,11 +39,12 @@ impl<const N: usize> String<N> {
39
39
/// ```
40
40
/// use heapless::String;
41
41
///
42
- /// let s: String<4> = String::from ("ab");
42
+ /// let s: String<4> = String::try_from ("ab")? ;
43
43
/// let b = s.into_bytes();
44
44
/// assert!(b.len() == 2);
45
45
///
46
46
/// assert_eq!(&['a' as u8, 'b' as u8], &b[..]);
47
+ /// # Ok::<(), ()>(())
47
48
/// ```
48
49
#[ inline]
49
50
pub fn into_bytes ( self ) -> Vec < u8 , N > {
@@ -59,11 +60,12 @@ impl<const N: usize> String<N> {
59
60
/// ```
60
61
/// use heapless::String;
61
62
///
62
- /// let mut s: String<4> = String::from ("ab");
63
+ /// let mut s: String<4> = String::try_from ("ab")? ;
63
64
/// assert!(s.as_str() == "ab");
64
65
///
65
66
/// let _s = s.as_str();
66
67
/// // s.push('c'); // <- cannot borrow `s` as mutable because it is also borrowed as immutable
68
+ /// # Ok::<(), ()>(())
67
69
/// ```
68
70
#[ inline]
69
71
pub fn as_str ( & self ) -> & str {
@@ -79,9 +81,10 @@ impl<const N: usize> String<N> {
79
81
/// ```
80
82
/// use heapless::String;
81
83
///
82
- /// let mut s: String<4> = String::from ("ab");
84
+ /// let mut s: String<4> = String::try_from ("ab")? ;
83
85
/// let s = s.as_mut_str();
84
86
/// s.make_ascii_uppercase();
87
+ /// # Ok::<(), ()>(())
85
88
/// ```
86
89
#[ inline]
87
90
pub fn as_mut_str ( & mut self ) -> & mut str {
@@ -102,7 +105,9 @@ impl<const N: usize> String<N> {
102
105
/// Basic usage:
103
106
///
104
107
/// ```
105
- /// let mut s = String::from("hello");
108
+ /// use heapless::String;
109
+ ///
110
+ /// let mut s: String<8> = String::try_from("hello")?;
106
111
///
107
112
/// unsafe {
108
113
/// let vec = s.as_mut_vec();
@@ -111,6 +116,7 @@ impl<const N: usize> String<N> {
111
116
/// vec.reverse();
112
117
/// }
113
118
/// assert_eq!(s, "olleh");
119
+ /// # Ok::<(), ()>(())
114
120
/// ```
115
121
pub unsafe fn as_mut_vec ( & mut self ) -> & mut Vec < u8 , N > {
116
122
& mut self . vec
@@ -125,13 +131,14 @@ impl<const N: usize> String<N> {
125
131
/// ```
126
132
/// use heapless::String;
127
133
///
128
- /// let mut s: String<8> = String::from ("foo");
134
+ /// let mut s: String<8> = String::try_from ("foo")? ;
129
135
///
130
136
/// assert!(s.push_str("bar").is_ok());
131
137
///
132
138
/// assert_eq!("foobar", s);
133
139
///
134
140
/// assert!(s.push_str("tender").is_err());
141
+ /// # Ok::<(), ()>(())
135
142
/// ```
136
143
#[ inline]
137
144
pub fn push_str ( & mut self , string : & str ) -> Result < ( ) , ( ) > {
@@ -166,7 +173,7 @@ impl<const N: usize> String<N> {
166
173
/// ```
167
174
/// use heapless::String;
168
175
///
169
- /// let mut s: String<8> = String::from ("abc");
176
+ /// let mut s: String<8> = String::try_from ("abc")? ;
170
177
///
171
178
/// s.push('1').unwrap();
172
179
/// s.push('2').unwrap();
@@ -175,6 +182,7 @@ impl<const N: usize> String<N> {
175
182
/// assert!("abc123" == s.as_str());
176
183
///
177
184
/// assert_eq!("abc123", s);
185
+ /// # Ok::<(), ()>(())
178
186
/// ```
179
187
#[ inline]
180
188
pub fn push ( & mut self , c : char ) -> Result < ( ) , ( ) > {
@@ -207,11 +215,12 @@ impl<const N: usize> String<N> {
207
215
/// ```
208
216
/// use heapless::String;
209
217
///
210
- /// let mut s: String<8> = String::from ("hello");
218
+ /// let mut s: String<8> = String::try_from ("hello")? ;
211
219
///
212
220
/// s.truncate(2);
213
221
///
214
222
/// assert_eq!("he", s);
223
+ /// # Ok::<(), ()>(())
215
224
/// ```
216
225
#[ inline]
217
226
pub fn truncate ( & mut self , new_len : usize ) {
@@ -234,13 +243,14 @@ impl<const N: usize> String<N> {
234
243
/// ```
235
244
/// use heapless::String;
236
245
///
237
- /// let mut s: String<8> = String::from ("foo");
246
+ /// let mut s: String<8> = String::try_from ("foo")? ;
238
247
///
239
248
/// assert_eq!(s.pop(), Some('o'));
240
249
/// assert_eq!(s.pop(), Some('o'));
241
250
/// assert_eq!(s.pop(), Some('f'));
242
251
///
243
252
/// assert_eq!(s.pop(), None);
253
+ /// Ok::<(), ()>(())
244
254
/// ```
245
255
pub fn pop ( & mut self ) -> Option < char > {
246
256
let ch = self . chars ( ) . rev ( ) . next ( ) ?;
@@ -267,13 +277,14 @@ impl<const N: usize> String<N> {
267
277
/// ```
268
278
/// use heapless::String;
269
279
///
270
- /// let mut s: String<8> = String::from ("foo");
280
+ /// let mut s: String<8> = String::try_from ("foo")? ;
271
281
///
272
282
/// s.clear();
273
283
///
274
284
/// assert!(s.is_empty());
275
285
/// assert_eq!(0, s.len());
276
286
/// assert_eq!(8, s.capacity());
287
+ /// Ok::<(), ()>(())
277
288
/// ```
278
289
#[ inline]
279
290
pub fn clear ( & mut self ) {
@@ -287,11 +298,12 @@ impl<const N: usize> Default for String<N> {
287
298
}
288
299
}
289
300
290
- impl < ' a , const N : usize > From < & ' a str > for String < N > {
291
- fn from ( s : & ' a str ) -> Self {
301
+ impl < ' a , const N : usize > TryFrom < & ' a str > for String < N > {
302
+ type Error = ( ) ;
303
+ fn try_from ( s : & ' a str ) -> Result < Self , Self :: Error > {
292
304
let mut new = String :: new ( ) ;
293
- new. push_str ( s) . unwrap ( ) ;
294
- new
305
+ new. push_str ( s) ? ;
306
+ Ok ( new)
295
307
}
296
308
}
297
309
@@ -474,31 +486,33 @@ impl<const N: usize> Ord for String<N> {
474
486
}
475
487
}
476
488
477
- macro_rules! impl_from_num {
489
+ macro_rules! impl_try_from_num {
478
490
( $num: ty, $size: expr) => {
479
- impl <const N : usize > From <$num> for String <N > {
480
- fn from( s: $num) -> Self {
491
+ impl <const N : usize > core:: convert:: TryFrom <$num> for String <N > {
492
+ type Error = ( ) ;
493
+ fn try_from( s: $num) -> Result <Self , Self :: Error > {
481
494
let mut new = String :: new( ) ;
482
- write!( & mut new, "{}" , s) . unwrap ( ) ;
483
- new
495
+ write!( & mut new, "{}" , s) . map_err ( |_| ( ) ) ? ;
496
+ Ok ( new)
484
497
}
485
498
}
486
499
} ;
487
500
}
488
501
489
- impl_from_num ! ( i8 , 4 ) ;
490
- impl_from_num ! ( i16 , 6 ) ;
491
- impl_from_num ! ( i32 , 11 ) ;
492
- impl_from_num ! ( i64 , 20 ) ;
502
+ impl_try_from_num ! ( i8 , 4 ) ;
503
+ impl_try_from_num ! ( i16 , 6 ) ;
504
+ impl_try_from_num ! ( i32 , 11 ) ;
505
+ impl_try_from_num ! ( i64 , 20 ) ;
493
506
494
- impl_from_num ! ( u8 , 3 ) ;
495
- impl_from_num ! ( u16 , 5 ) ;
496
- impl_from_num ! ( u32 , 10 ) ;
497
- impl_from_num ! ( u64 , 20 ) ;
507
+ impl_try_from_num ! ( u8 , 3 ) ;
508
+ impl_try_from_num ! ( u16 , 5 ) ;
509
+ impl_try_from_num ! ( u32 , 10 ) ;
510
+ impl_try_from_num ! ( u64 , 20 ) ;
498
511
499
512
#[ cfg( test) ]
500
513
mod tests {
501
514
use crate :: { String , Vec } ;
515
+ use core:: convert:: TryFrom ;
502
516
503
517
#[ test]
504
518
fn static_new ( ) {
@@ -507,7 +521,7 @@ mod tests {
507
521
508
522
#[ test]
509
523
fn clone ( ) {
510
- let s1: String < 20 > = String :: from ( "abcd" ) ;
524
+ let s1: String < 20 > = String :: try_from ( "abcd" ) . unwrap ( ) ;
511
525
let mut s2 = s1. clone ( ) ;
512
526
s2. push_str ( " efgh" ) . unwrap ( ) ;
513
527
@@ -517,16 +531,16 @@ mod tests {
517
531
518
532
#[ test]
519
533
fn cmp ( ) {
520
- let s1: String < 4 > = String :: from ( "abcd" ) ;
521
- let s2: String < 4 > = String :: from ( "zzzz" ) ;
534
+ let s1: String < 4 > = String :: try_from ( "abcd" ) . unwrap ( ) ;
535
+ let s2: String < 4 > = String :: try_from ( "zzzz" ) . unwrap ( ) ;
522
536
523
537
assert ! ( s1 < s2) ;
524
538
}
525
539
526
540
#[ test]
527
541
fn cmp_heterogenous_size ( ) {
528
- let s1: String < 4 > = String :: from ( "abcd" ) ;
529
- let s2: String < 8 > = String :: from ( "zzzz" ) ;
542
+ let s1: String < 4 > = String :: try_from ( "abcd" ) . unwrap ( ) ;
543
+ let s2: String < 8 > = String :: try_from ( "zzzz" ) . unwrap ( ) ;
530
544
531
545
assert ! ( s1 < s2) ;
532
546
}
@@ -535,7 +549,7 @@ mod tests {
535
549
fn debug ( ) {
536
550
use core:: fmt:: Write ;
537
551
538
- let s: String < 8 > = String :: from ( "abcd" ) ;
552
+ let s: String < 8 > = String :: try_from ( "abcd" ) . unwrap ( ) ;
539
553
let mut std_s = std:: string:: String :: new ( ) ;
540
554
write ! ( std_s, "{:?}" , s) . unwrap ( ) ;
541
555
assert_eq ! ( "\" abcd\" " , std_s) ;
@@ -545,7 +559,7 @@ mod tests {
545
559
fn display ( ) {
546
560
use core:: fmt:: Write ;
547
561
548
- let s: String < 8 > = String :: from ( "abcd" ) ;
562
+ let s: String < 8 > = String :: try_from ( "abcd" ) . unwrap ( ) ;
549
563
let mut std_s = std:: string:: String :: new ( ) ;
550
564
write ! ( std_s, "{}" , s) . unwrap ( ) ;
551
565
assert_eq ! ( "abcd" , std_s) ;
@@ -561,10 +575,13 @@ mod tests {
561
575
}
562
576
563
577
#[ test]
564
- fn from ( ) {
565
- let s: String < 4 > = String :: from ( "123" ) ;
578
+ fn try_from ( ) {
579
+ let s: String < 4 > = String :: try_from ( "123" ) . unwrap ( ) ;
566
580
assert ! ( s. len( ) == 3 ) ;
567
581
assert_eq ! ( s, "123" ) ;
582
+
583
+ let e: ( ) = String :: < 2 > :: try_from ( "123" ) . unwrap_err ( ) ;
584
+ assert_eq ! ( e, ( ) ) ;
568
585
}
569
586
570
587
#[ test]
@@ -596,26 +613,29 @@ mod tests {
596
613
#[ test]
597
614
#[ should_panic]
598
615
fn from_panic ( ) {
599
- let _: String < 4 > = String :: from ( "12345" ) ;
616
+ let _: String < 4 > = String :: try_from ( "12345" ) . unwrap ( ) ;
600
617
}
601
618
602
619
#[ test]
603
- fn from_num ( ) {
604
- let v: String < 20 > = String :: from ( 18446744073709551615 as u64 ) ;
620
+ fn try_from_num ( ) {
621
+ let v: String < 20 > = String :: try_from ( 18446744073709551615 as u64 ) . unwrap ( ) ;
605
622
assert_eq ! ( v, "18446744073709551615" ) ;
623
+
624
+ let e: ( ) = String :: < 2 > :: try_from ( 18446744073709551615 as u64 ) . unwrap_err ( ) ;
625
+ assert_eq ! ( e, ( ) ) ;
606
626
}
607
627
608
628
#[ test]
609
629
fn into_bytes ( ) {
610
- let s: String < 4 > = String :: from ( "ab" ) ;
630
+ let s: String < 4 > = String :: try_from ( "ab" ) . unwrap ( ) ;
611
631
let b: Vec < u8 , 4 > = s. into_bytes ( ) ;
612
632
assert_eq ! ( b. len( ) , 2 ) ;
613
633
assert_eq ! ( & [ 'a' as u8 , 'b' as u8 ] , & b[ ..] ) ;
614
634
}
615
635
616
636
#[ test]
617
637
fn as_str ( ) {
618
- let s: String < 4 > = String :: from ( "ab" ) ;
638
+ let s: String < 4 > = String :: try_from ( "ab" ) . unwrap ( ) ;
619
639
620
640
assert_eq ! ( s. as_str( ) , "ab" ) ;
621
641
// should be moved to fail test
@@ -625,15 +645,15 @@ mod tests {
625
645
626
646
#[ test]
627
647
fn as_mut_str ( ) {
628
- let mut s: String < 4 > = String :: from ( "ab" ) ;
648
+ let mut s: String < 4 > = String :: try_from ( "ab" ) . unwrap ( ) ;
629
649
let s = s. as_mut_str ( ) ;
630
650
s. make_ascii_uppercase ( ) ;
631
651
assert_eq ! ( s, "AB" ) ;
632
652
}
633
653
634
654
#[ test]
635
655
fn push_str ( ) {
636
- let mut s: String < 8 > = String :: from ( "foo" ) ;
656
+ let mut s: String < 8 > = String :: try_from ( "foo" ) . unwrap ( ) ;
637
657
assert ! ( s. push_str( "bar" ) . is_ok( ) ) ;
638
658
assert_eq ! ( "foobar" , s) ;
639
659
assert_eq ! ( s, "foobar" ) ;
@@ -644,7 +664,7 @@ mod tests {
644
664
645
665
#[ test]
646
666
fn push ( ) {
647
- let mut s: String < 6 > = String :: from ( "abc" ) ;
667
+ let mut s: String < 6 > = String :: try_from ( "abc" ) . unwrap ( ) ;
648
668
assert ! ( s. push( '1' ) . is_ok( ) ) ;
649
669
assert ! ( s. push( '2' ) . is_ok( ) ) ;
650
670
assert ! ( s. push( '3' ) . is_ok( ) ) ;
@@ -654,13 +674,13 @@ mod tests {
654
674
655
675
#[ test]
656
676
fn as_bytes ( ) {
657
- let s: String < 8 > = String :: from ( "hello" ) ;
677
+ let s: String < 8 > = String :: try_from ( "hello" ) . unwrap ( ) ;
658
678
assert_eq ! ( & [ 104 , 101 , 108 , 108 , 111 ] , s. as_bytes( ) ) ;
659
679
}
660
680
661
681
#[ test]
662
682
fn truncate ( ) {
663
- let mut s: String < 8 > = String :: from ( "hello" ) ;
683
+ let mut s: String < 8 > = String :: try_from ( "hello" ) . unwrap ( ) ;
664
684
s. truncate ( 6 ) ;
665
685
assert_eq ! ( s. len( ) , 5 ) ;
666
686
s. truncate ( 2 ) ;
@@ -671,7 +691,7 @@ mod tests {
671
691
672
692
#[ test]
673
693
fn pop ( ) {
674
- let mut s: String < 8 > = String :: from ( "foo" ) ;
694
+ let mut s: String < 8 > = String :: try_from ( "foo" ) . unwrap ( ) ;
675
695
assert_eq ! ( s. pop( ) , Some ( 'o' ) ) ;
676
696
assert_eq ! ( s. pop( ) , Some ( 'o' ) ) ;
677
697
assert_eq ! ( s. pop( ) , Some ( 'f' ) ) ;
@@ -680,7 +700,7 @@ mod tests {
680
700
681
701
#[ test]
682
702
fn pop_uenc ( ) {
683
- let mut s: String < 8 > = String :: from ( "é" ) ;
703
+ let mut s: String < 8 > = String :: try_from ( "é" ) . unwrap ( ) ;
684
704
assert_eq ! ( s. len( ) , 3 ) ;
685
705
match s. pop ( ) {
686
706
Some ( c) => {
@@ -702,7 +722,7 @@ mod tests {
702
722
703
723
#[ test]
704
724
fn clear ( ) {
705
- let mut s: String < 8 > = String :: from ( "foo" ) ;
725
+ let mut s: String < 8 > = String :: try_from ( "foo" ) . unwrap ( ) ;
706
726
s. clear ( ) ;
707
727
assert ! ( s. is_empty( ) ) ;
708
728
assert_eq ! ( 0 , s. len( ) ) ;
0 commit comments