Skip to content

Commit 995c6f0

Browse files
authored
Merge pull request #693 from tlyu/trait-obj-tryfrom
fix: use trait objects for try_from_into and from_str
2 parents 9aeca3f + c3e7b83 commit 995c6f0

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

exercises/conversions/from_str.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Additionally, upon implementing FromStr, you can use the `parse` method
33
// on strings to generate an object of the implementor type.
44
// You can read more about it at https://doc.rust-lang.org/std/str/trait.FromStr.html
5+
use std::error;
56
use std::str::FromStr;
67

78
#[derive(Debug)]
@@ -23,7 +24,7 @@ struct Person {
2324
// If everything goes well, then return a Result of a Person object
2425

2526
impl FromStr for Person {
26-
type Err = String;
27+
type Err = Box<dyn error::Error>;
2728
fn from_str(s: &str) -> Result<Person, Self::Err> {
2829
}
2930
}

exercises/conversions/try_from_into.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// instead of the target type itself.
44
// You can read more about it at https://doc.rust-lang.org/std/convert/trait.TryFrom.html
55
use std::convert::{TryFrom, TryInto};
6+
use std::error;
67

78
#[derive(Debug, PartialEq)]
89
struct Color {
@@ -24,19 +25,19 @@ struct Color {
2425

2526
// Tuple implementation
2627
impl TryFrom<(i16, i16, i16)> for Color {
27-
type Error = String;
28+
type Error = Box<dyn error::Error>;
2829
fn try_from(tuple: (i16, i16, i16)) -> Result<Self, Self::Error> {}
2930
}
3031

3132
// Array implementation
3233
impl TryFrom<[i16; 3]> for Color {
33-
type Error = String;
34+
type Error = Box<dyn error::Error>;
3435
fn try_from(arr: [i16; 3]) -> Result<Self, Self::Error> {}
3536
}
3637

3738
// Slice implementation
3839
impl TryFrom<&[i16]> for Color {
39-
type Error = String;
40+
type Error = Box<dyn error::Error>;
4041
fn try_from(slice: &[i16]) -> Result<Self, Self::Error> {}
4142
}
4243

@@ -76,41 +77,43 @@ mod tests {
7677
}
7778
#[test]
7879
fn test_tuple_correct() {
79-
let c: Result<Color, String> = (183, 65, 14).try_into();
80+
let c: Result<Color, _> = (183, 65, 14).try_into();
81+
assert!(c.is_ok());
8082
assert_eq!(
81-
c,
82-
Ok(Color {
83+
c.unwrap(),
84+
Color {
8385
red: 183,
8486
green: 65,
8587
blue: 14
86-
})
88+
}
8789
);
8890
}
8991
#[test]
9092
fn test_array_out_of_range_positive() {
91-
let c: Result<Color, String> = [1000, 10000, 256].try_into();
93+
let c: Result<Color, _> = [1000, 10000, 256].try_into();
9294
assert!(c.is_err());
9395
}
9496
#[test]
9597
fn test_array_out_of_range_negative() {
96-
let c: Result<Color, String> = [-10, -256, -1].try_into();
98+
let c: Result<Color, _> = [-10, -256, -1].try_into();
9799
assert!(c.is_err());
98100
}
99101
#[test]
100102
fn test_array_sum() {
101-
let c: Result<Color, String> = [-1, 255, 255].try_into();
103+
let c: Result<Color, _> = [-1, 255, 255].try_into();
102104
assert!(c.is_err());
103105
}
104106
#[test]
105107
fn test_array_correct() {
106-
let c: Result<Color, String> = [183, 65, 14].try_into();
108+
let c: Result<Color, _> = [183, 65, 14].try_into();
109+
assert!(c.is_ok());
107110
assert_eq!(
108-
c,
109-
Ok(Color {
111+
c.unwrap(),
112+
Color {
110113
red: 183,
111114
green: 65,
112115
blue: 14
113-
})
116+
}
114117
);
115118
}
116119
#[test]
@@ -131,14 +134,15 @@ mod tests {
131134
#[test]
132135
fn test_slice_correct() {
133136
let v = vec![183, 65, 14];
134-
let c: Result<Color, String> = Color::try_from(&v[..]);
137+
let c: Result<Color, _> = Color::try_from(&v[..]);
138+
assert!(c.is_ok());
135139
assert_eq!(
136-
c,
137-
Ok(Color {
140+
c.unwrap(),
141+
Color {
138142
red: 183,
139143
green: 65,
140144
blue: 14
141-
})
145+
}
142146
);
143147
}
144148
#[test]

info.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,5 +884,5 @@ path = "exercises/conversions/from_str.rs"
884884
mode = "test"
885885
hint = """
886886
The implementation of FromStr should return an Ok with a Person object,
887-
or an Err with a string if the string is not valid.
887+
or an Err with an error if the string is not valid.
888888
This is almost like the `try_from_into` exercise."""

0 commit comments

Comments
 (0)