这是我之前问题的后续问题:Rust:读取并映射来自标准输入的行并处理不同的错误类型
我创建了以下结构体和函数,用于从标准输入读取行并将它们解析为整数,目前它能正常工作。
现在,我想把类型参数T替换为
我尝试了类似这样的东西:
但是这会导致以下结果:
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
}
现在,我想把类型参数T替换为
u32
,以允许使用任何一种数字类型。为了做到这一点,我假设需要将T限制为实现FromStr特性的类型,然后以某种方式实现From特性,以允许从FromStr :: Err转换为我的“InputError”。根据错误信息,我首先得到了以下提示: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`
我尝试了类似这样的东西:
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)
}
}
但是这会导致以下结果:
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
基本上我想表达的意思是:
我想为每个实现FromStr
的T
类型,实现InputError
的特性From<T::Err>
。这是否可能,如果可能,怎么做呢?