cds/arraystring/traits/
from_str.rs

1use crate::{
2    arraystring::{errors::InsufficientCapacityError, ArrayString},
3    len::LengthType,
4    mem::SpareMemoryPolicy,
5};
6
7impl<L, SM, const C: usize> core::str::FromStr for ArrayString<C, L, SM>
8where
9    L: LengthType,
10    SM: SpareMemoryPolicy<u8>,
11{
12    type Err = InsufficientCapacityError;
13
14    #[inline]
15    fn from_str(s: &str) -> Result<Self, Self::Err> {
16        Self::try_from(s)
17    }
18}
19
20#[cfg(test)]
21mod testing {
22    use crate as cds;
23    use cds::{
24        arraystring::{errors::InsufficientCapacityError, ArrayString},
25        len::U8,
26    };
27    use core::str::FromStr;
28
29    #[test]
30    fn test_from_str() {
31        type AS = ArrayString<4, U8>;
32
33        let s = AS::from_str("cds").unwrap();
34        assert_eq!(s, "cds");
35
36        let s = AS::from_str("").unwrap();
37        assert_eq!(s, "");
38
39        assert!(matches!(AS::from_str("abcdef"), Err(e) if e == InsufficientCapacityError));
40    }
41}