This is a follow-up question on my previous question: Rust: Read and map lines from stdin and handling different error types
I have created the following struct and function to read lines from stdin and parse them into integers and it works:
use std::io::BufRead;
use std::{io, num, str};
#[derive(Debug)]
enum InputError {
IOError(io::Error),
ParseIntError(num::ParseIntError),
}
impl From<io::Error> for InputError {
fn from(e: io::Error) -> InputError {
return InputError::IOError(e);
}
}
impl From<num::ParseIntError> for InputError {
fn from(e: num::ParseIntError) -> InputError {
return InputError::ParseIntError(e);
}
}
pub fn get_integer_lines<T>() -> Result<Vec<T>, InputError>
where
T: str::FromStr,
{
let stdin = io::stdin();
let my_values: Result<Vec<_>, InputError> = stdin
.lock()
.lines()
.map(|line| -> Result<T, InputError> { Ok(line?.parse::<T>()?) })
.collect();
my_values
}
Now, I thought that I would replace u32
for a type parameter T to allow for any kind of numeric type. To do this I assume that I need to restrict T to types implementing the FromStr trait and then somehow implement the From trait to allow conversion from FromStr::Err into my "InputError".
Following the error I first got
error[E0277]: `?` couldn't convert the error to `InputError`
--> src/lib.rs:30:69
|
30 | .map(|line| -> Result<T, InputError> { Ok(line?.parse::<T>()?) })
| ^ the trait `std::convert::From<<T as std::str::FromStr>::Err>` is not implemented for `InputError`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: consider adding a `where InputError: std::convert::From<<T as std::str::FromStr>::Err>` bound
= note: required by `std::convert::From::from`
I tried something like this:
impl std::convert::From<<T as std::str::FromStr>::Err> for InputError {
fn from(e: <T as std::str::FromStr>::Err) -> InputError {
return InputError::ParseIntError(e)
}
}
But that instead results in:
error[E0412]: cannot find type `T` in this scope
--> src/lib.rs:22:26
|
22 | impl std::convert::From<<T as std::str::FromStr>::Err> for InputError {
| ^ not found in this scope
So basically what I want to express is something along the lines of:
"I want to implement the trait From<T::Err>
for my InputError
for every T
which also implements FromStr
. Is this even possible and if so, how?