1
- use core:: {
2
- fmt,
3
- fmt:: Write ,
4
- hash,
5
- mem:: { self , MaybeUninit } ,
6
- ops, str,
7
- str:: Utf8Error ,
8
- } ;
1
+ use core:: { fmt, fmt:: Write , hash, ops, str} ;
9
2
10
3
use hash32;
11
4
@@ -16,13 +9,6 @@ pub struct String<const N: usize> {
16
9
vec : Vec < u8 , N > ,
17
10
}
18
11
19
- // impl<const N: usize> String<N> {
20
- // /// `String` `const` constructor; wrap the returned value in [`String`](../struct.String.html)
21
- // pub const fn new() -> Self {
22
- // Self { vec: Vec::new() }
23
- // }
24
- // }
25
-
26
12
impl < const N : usize > String < N > {
27
13
/// Constructs a new, empty `String` with a fixed capacity of `N`
28
14
///
@@ -44,65 +30,6 @@ impl<const N: usize> String<N> {
44
30
Self { vec : Vec :: new ( ) }
45
31
}
46
32
47
- /// Converts a vector of bytes into a `String`.
48
- ///
49
- /// A string slice ([`&str`]) is made of bytes ([`u8`]), and a vector of bytes
50
- /// ([`Vec<u8>`]) is made of bytes, so this function converts between the
51
- /// two. Not all byte slices are valid `String`s, however: `String`
52
- /// requires that it is valid UTF-8. `from_utf8()` checks to ensure that
53
- /// the bytes are valid UTF-8, and then does the conversion.
54
- ///
55
- /// See std::String for further information.
56
- ///
57
- /// # Examples
58
- ///
59
- /// Basic usage:
60
- ///
61
- /// ```
62
- /// use heapless::{String, Vec};
63
- ///
64
- /// let mut v: Vec<u8, 8> = Vec::new();
65
- /// v.push('a' as u8).unwrap();
66
- /// v.push('b' as u8).unwrap();
67
- ///
68
- /// let s = String::from_utf8(v).unwrap();
69
- /// assert!(s.len() == 2);
70
- /// ```
71
- ///
72
- /// Incorrect bytes:
73
- ///
74
- /// ```
75
- /// use heapless::{String, Vec};
76
- ///
77
- /// // some invalid bytes, in a vector
78
- ///
79
- /// let mut v: Vec<u8, 8> = Vec::new();
80
- /// v.push(0).unwrap();
81
- /// v.push(159).unwrap();
82
- /// v.push(146).unwrap();
83
- /// v.push(150).unwrap();
84
- /// assert!(String::from_utf8(v).is_err());
85
- /// ```
86
- #[ inline]
87
- pub fn from_utf8 ( vec : Vec < u8 , N > ) -> Result < String < N > , Utf8Error > {
88
- // validate input
89
- str:: from_utf8 ( & * vec) ?;
90
-
91
- Ok ( unsafe { String :: from_utf8_unchecked ( vec) } )
92
- }
93
-
94
- /// Converts a vector of bytes to a `String` without checking that the
95
- /// string contains valid UTF-8.
96
- ///
97
- /// See the safe version, `from_utf8`, for more details.
98
- #[ inline]
99
- pub unsafe fn from_utf8_unchecked ( mut vec : Vec < u8 , N > ) -> String < N > {
100
- // FIXME this may result in a memcpy at runtime
101
- let vec_ = mem:: replace ( & mut vec, MaybeUninit :: uninit ( ) . assume_init ( ) ) ;
102
- mem:: forget ( vec) ;
103
- String { vec : vec_ }
104
- }
105
-
106
33
/// Converts a `String` into a byte vector.
107
34
///
108
35
/// This consumes the `String`, so we do not need to copy its contents.
@@ -544,14 +471,6 @@ impl<const N: usize> PartialEq<String<N>> for &str {
544
471
545
472
impl < const N : usize > Eq for String < N > { }
546
473
547
- // impl<const N: usize, D: core::fmt::Display> From<D> for String<N> {
548
- // fn from(s: D) -> Self {
549
- // let mut new = String::new();
550
- // write!(&mut new, "{}", s).unwrap();
551
- // new
552
- // }
553
- // }
554
-
555
474
macro_rules! impl_from_num {
556
475
( $num: ty, $size: expr) => {
557
476
impl <const N : usize > From <$num> for String <N > {
@@ -647,52 +566,6 @@ mod tests {
647
566
let _: String < 4 > = String :: from ( "12345" ) ;
648
567
}
649
568
650
- #[ test]
651
- fn from_utf8 ( ) {
652
- let mut v: Vec < u8 , 8 > = Vec :: new ( ) ;
653
- v. push ( 'a' as u8 ) . unwrap ( ) ;
654
- v. push ( 'b' as u8 ) . unwrap ( ) ;
655
-
656
- let s = String :: from_utf8 ( v) . unwrap ( ) ;
657
- assert_eq ! ( s, "ab" ) ;
658
- }
659
-
660
- #[ test]
661
- fn from_utf8_uenc ( ) {
662
- let mut v: Vec < u8 , 8 > = Vec :: new ( ) ;
663
- v. push ( 240 ) . unwrap ( ) ;
664
- v. push ( 159 ) . unwrap ( ) ;
665
- v. push ( 146 ) . unwrap ( ) ;
666
- v. push ( 150 ) . unwrap ( ) ;
667
-
668
- assert ! ( String :: from_utf8( v) . is_ok( ) ) ;
669
- }
670
-
671
- #[ test]
672
- fn from_utf8_uenc_err ( ) {
673
- let mut v: Vec < u8 , 8 > = Vec :: new ( ) ;
674
- v. push ( 0 ) . unwrap ( ) ;
675
- v. push ( 159 ) . unwrap ( ) ;
676
- v. push ( 146 ) . unwrap ( ) ;
677
- v. push ( 150 ) . unwrap ( ) ;
678
-
679
- assert ! ( String :: from_utf8( v) . is_err( ) ) ;
680
- }
681
-
682
- #[ test]
683
- fn from_utf8_unchecked ( ) {
684
- let mut v: Vec < u8 , 8 > = Vec :: new ( ) ;
685
- v. push ( 104 ) . unwrap ( ) ;
686
- v. push ( 101 ) . unwrap ( ) ;
687
- v. push ( 108 ) . unwrap ( ) ;
688
- v. push ( 108 ) . unwrap ( ) ;
689
- v. push ( 111 ) . unwrap ( ) ;
690
-
691
- let s = unsafe { String :: from_utf8_unchecked ( v) } ;
692
-
693
- assert_eq ! ( s, "hello" ) ;
694
- }
695
-
696
569
#[ test]
697
570
fn from_num ( ) {
698
571
let v: String < 20 > = String :: from ( 18446744073709551615 as u64 ) ;
0 commit comments