[英]Why do I need to implement MyTrait<T> instead of MyTrait<TypeA> and MyTrait<TypeB>?
[英]How do implement a struct that takes a trait MyTrait<A>? [duplicate]
我定义了一个特征如下:
trait Readable<E> {
fn read_u8(&mut self) -> Result<u8, E>;
fn read_u16be(&mut self) -> Result<u16, E>;
}
这个想法是用不同的后端来实现它,返回不同的错误类型。 我尝试在 function 中使用它:
fn f<E, R: Readable<E>>(r: &mut R) -> Result<u8, E> {
r.read_u8()
}
这编译。 我在 impl 中尝试了同样的方法:
struct FileFormat<R> {
r: R,
}
impl<E, R: Readable<E>> FileFormat<R> {
fn f(&mut self) -> Result<u8, E> {
self.r.read_u8()
}
}
这失败了:
14 | impl<E, R: Readable<E>> FileFormat<R> {
| ^ unconstrained type parameter
编译器建议rustc --explain E0207
但恐怕我无法理解其中包含的答案是否存在。
为什么前者编译而后者不编译? 为什么在这种情况下E
是不受约束的? 如何解决这个问题,以便实现能够采用任何Readable
?
让我们想象一下它确实 compile 。 这是一个带注释的示例,它表明它允许我们编写编译器无法进行类型检查的损坏代码:
trait Readable<E> {
fn read_u8(&mut self) -> Result<u8, E>;
fn read_u16be(&mut self) -> Result<u16, E>;
}
fn f<E, R: Readable<E>>(r: &mut R) -> Result<u8, E> {
r.read_u8()
}
struct SomeError;
struct SomeOtherError;
struct SomeReadable;
impl Readable<SomeError> for SomeReadable {
fn read_u8(&mut self) -> Result<u8, SomeError> {
todo!()
}
fn read_u16be(&mut self) -> Result<u16, SomeError> {
todo!()
}
}
impl Readable<SomeOtherError> for SomeReadable {
fn read_u8(&mut self) -> Result<u8, SomeOtherError> {
todo!()
}
fn read_u16be(&mut self) -> Result<u16, SomeOtherError> {
todo!()
}
}
struct FileFormat<R> {
r: R,
}
// let's pretend that this does compile
impl<E, R: Readable<E>> FileFormat<R> {
fn f(&mut self) -> Result<u8, E> {
self.r.read_u8()
}
}
// it will now allow us to write this code
// which is impossible to type check so
// it's obviously broken
fn example(mut fr: FileFormat<SomeReadable>) -> Result<u8, ???> {
// um, does this return Result<u8, SomeError>
// or does it return Result<u8, SomeOtherError>???
// it's impossible to know!
fr.f()
}
错误类型需要出现在FileFormat
类型中的某处。 修复就像将PhantomData
成员添加到FileFormat
一样简单,这样您就可以“确定”特定的错误类型:
use core::marker::PhantomData;
trait Readable<E> {
fn read_u8(&mut self) -> Result<u8, E>;
fn read_u16be(&mut self) -> Result<u16, E>;
}
fn f<E, R: Readable<E>>(r: &mut R) -> Result<u8, E> {
r.read_u8()
}
struct SomeError;
struct SomeOtherError;
struct SomeReadable;
impl Readable<SomeError> for SomeReadable {
fn read_u8(&mut self) -> Result<u8, SomeError> {
todo!()
}
fn read_u16be(&mut self) -> Result<u16, SomeError> {
todo!()
}
}
impl Readable<SomeOtherError> for SomeReadable {
fn read_u8(&mut self) -> Result<u8, SomeOtherError> {
todo!()
}
fn read_u16be(&mut self) -> Result<u16, SomeOtherError> {
todo!()
}
}
struct FileFormat<R, E> {
r: R,
e: PhantomData<E>,
}
// now compiles!
impl<E, R: Readable<E>> FileFormat<R, E> {
fn f(&mut self) -> Result<u8, E> {
self.r.read_u8()
}
}
// now works!
fn example(mut fr: FileFormat<SomeReadable, SomeError>) -> Result<u8, SomeError> {
fr.f()
}
// now also works!
fn other_example(mut fr: FileFormat<SomeReadable, SomeOtherError>) -> Result<u8, SomeOtherError> {
fr.f()
}
独立的通用 function 有效,因为我们在调用 function 时指定了错误类型:
trait Readable<E> {
fn read_u8(&mut self) -> Result<u8, E>;
fn read_u16be(&mut self) -> Result<u16, E>;
}
struct SomeError;
struct SomeOtherError;
struct SomeReadable;
impl Readable<SomeError> for SomeReadable {
fn read_u8(&mut self) -> Result<u8, SomeError> {
todo!()
}
fn read_u16be(&mut self) -> Result<u16, SomeError> {
todo!()
}
}
impl Readable<SomeOtherError> for SomeReadable {
fn read_u8(&mut self) -> Result<u8, SomeOtherError> {
todo!()
}
fn read_u16be(&mut self) -> Result<u16, SomeOtherError> {
todo!()
}
}
fn f<E, R: Readable<E>>(r: &mut R) -> Result<u8, E> {
r.read_u8()
}
fn example() {
let mut readable: SomeReadable = SomeReadable;
// error type clarified to be SomeError here
f::<SomeError, _>(&mut readable);
let mut readable: SomeReadable = SomeReadable;
// error type clarified to be SomeOtherError here
f::<SomeOtherError, _>(&mut readable);
}
实际上,这一切都归结为使您的类型对编译器可见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.