[英]Rust: Implement trait "From" for associated type (Error)
这是我上一个问题的后续问题: Rust: Read and map lines from stdin and processing different error types
我创建了以下结构和函数来从 stdin 读取行并将它们解析为整数并且它可以工作:
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
}
现在,我想我会将u32
替换为类型参数 T 以允许任何类型的数字类型。 为此,我假设我需要将 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
所以基本上我想表达的东西是这样的:“我想为我的InputError
实现From<T::Err>
InputError
,每个T
也实现了FromStr
。这是否可能,如果是,如何?
所以基本上我想表达的东西是这样的:“我想为我的
InputError
实现From<T::Err>
InputError
,每个T
也实现了FromStr
。这是否可能,如果是,如何?
这不是错误所说的。
特征FromStr
有一个关联类型Err
。 错误是说此关联的错误类型无法转换为InputError
。
首先,让我们通过去掉类型参数来简化:
fn get_integer_lines() -> Result<Vec<u32>, InputError> {
let stdin = io::stdin();
let my_values = stdin
.lock()
.lines()
.map(|line| Ok(line?.parse()?))
.collect();
my_values
}
这有效!
相关的Err
类型为u32
的FromStr
实现ParseIntError
和您正确实施From<ParseIntError> for InputError
。
这对T
不起作用的原因是因为T
的FromStr::Err
类型可以是任何类型。 通过使用类型参数,您告诉编译器您希望此函数适用于任何可能的类型T
,但它仅适用于可以将FromStr::Err
转换为InputError
类型的类型。
错误消息给了你一个提示:
= help: consider adding a `where InputError: std::convert::From<<T as std::str::FromStr>::Err>` bound
所以让我们这样做:
fn get_integer_lines<T>() -> Result<Vec<T>, InputError>
where
T: str::FromStr,
InputError: From<<T as str::FromStr>::Err>,
{
let stdin = io::stdin();
let my_values = stdin
.lock()
.lines()
.map(|line| Ok(line?.parse()?))
.collect();
my_values
}
这告诉编译器您希望该函数适用于所有可能的T
前提是:
T
实现FromStr
和,T
的FromStr
实现的关联Err
类型可以转换为InputError
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.