1515extern crate alloc;
1616
1717use alloc:: boxed:: Box ;
18+ use core:: convert:: Infallible ;
1819use core:: error:: Error ;
20+ use core:: num:: { ParseFloatError , ParseIntError } ;
1921
20- use crate :: { PrimitiveError , PrimitiveInteger , PrimitiveNumber } ;
22+ use crate :: {
23+ PrimitiveError , PrimitiveFloat , PrimitiveInteger , PrimitiveNumber , PrimitiveSigned ,
24+ PrimitiveUnsigned ,
25+ } ;
2126
2227fn check_result < ' a , T : PrimitiveNumber , E : PrimitiveError > ( r : Result < T , E > , ok : T ) {
2328 // Cloning and equating results requires `E: Clone + PartialEq`
@@ -37,22 +42,70 @@ fn check_result<'a, T: PrimitiveNumber, E: PrimitiveError>(r: Result<T, E>, ok:
3742
3843#[ test]
3944fn parse ( ) {
45+ // `PrimitiveNumber` is not specific about the `FromStr` error type,
46+ // only constraining that it implements `PrimitiveError`.
4047 fn check < T : PrimitiveNumber > ( s : & str , ok : T ) {
4148 check_result ( s. parse ( ) , ok) ;
4249 }
4350 check ( "0" , 0u32 ) ;
4451}
4552
53+ #[ test]
54+ fn parse_float ( ) {
55+ // `PrimitiveFloat` is specific about the `FromStr` error type.
56+ fn check < T : PrimitiveFloat > ( s : & str , ok : T ) {
57+ let r: Result < T , ParseFloatError > = s. parse ( ) ;
58+ assert_eq ! ( r, Ok ( ok) ) ;
59+ }
60+ check ( "0" , 0f32 ) ;
61+ }
62+
63+ #[ test]
64+ fn parse_int ( ) {
65+ // `PrimitiveInteger` is specific about the `FromStr` error type.
66+ fn check < T : PrimitiveInteger > ( s : & str , ok : T ) {
67+ let r: Result < T , ParseIntError > = s. parse ( ) ;
68+ assert_eq ! ( r, Ok ( ok) ) ;
69+ }
70+ check ( "0" , 0u32 ) ;
71+ }
72+
4673#[ test]
4774fn try_from ( ) {
75+ // `PrimitiveInteger` is not specific about the `TryFrom` error type,
76+ // only constraining that it implements `PrimitiveError`.
4877 fn check < T : PrimitiveInteger > ( x : i32 , ok : T ) {
4978 check_result ( T :: try_from ( x) , ok) ;
5079 }
5180 check ( 0i32 , 0u32 ) ;
5281}
5382
83+ #[ test]
84+ fn try_from_signed ( ) {
85+ // `PrimitiveSigned` is specific that `TryFrom<i8>` is infallible.
86+ // (implied by `From<i8>`, but we still need an explicit constraint)
87+ fn check < T : PrimitiveSigned > ( x : i8 , ok : T ) {
88+ let r: Result < T , Infallible > = T :: try_from ( x) ;
89+ assert_eq ! ( r, Ok ( ok) ) ;
90+ }
91+ check ( 0i8 , 0i32 ) ;
92+ }
93+
94+ #[ test]
95+ fn try_from_unsigned ( ) {
96+ // `PrimitiveUnsigned` is specific that `TryFrom<u8>` is infallible.
97+ // (implied by `From<u8>`, but we still need an explicit constraint)
98+ fn check < T : PrimitiveUnsigned > ( x : u8 , ok : T ) {
99+ let r: Result < T , Infallible > = T :: try_from ( x) ;
100+ assert_eq ! ( r, Ok ( ok) ) ;
101+ }
102+ check ( 0u8 , 0u32 ) ;
103+ }
104+
54105#[ test]
55106fn try_into ( ) {
107+ // `PrimitiveInteger` is not specific about the `TryInto` error type,
108+ // only constraining that it implements `PrimitiveError`.
56109 fn check < T : PrimitiveInteger > ( x : T , ok : u32 ) {
57110 check_result ( x. try_into ( ) , ok) ;
58111 }
0 commit comments