【发布时间】:2017-06-24 23:00:56
【问题描述】:
在 Rust 1.15 中,我创建了一个 trait 来抽象读取和解析文件格式。我正在尝试创建一个内部具有此通用特征的结构。
我有这个特点:
use std::io::Read;
trait MyReader<R: Read> {
fn new(R) -> Self;
fn into_inner(self) -> R;
fn get_next(&mut self) -> Option<u32>;
fn do_thingie(&mut self);
}
我想创建一个结构,它引用了实现它的东西。
struct MyIterThing<'a, T: MyReader<R>+'a> {
inner: &'a mut T,
}
给出以下错误:
error[E0412]: type name `R` is undefined or not in scope
--> <anon>:11:36
|
11 | struct MyIterThing<'a, T: MyReader<R>+'a> {
| ^ undefined or not in scope
|
= help: no candidates by the name of `R` found in your project; maybe you misspelled the name or forgot to import an external crate?
T: MyReader+'a,我得到了错误:"error[E0243]: wrong number of type arguments: expected 1, found 0",T: MyReader<R: Read>+'a 给出了一个低级别的语法错误,它不希望出现:。
这也不起作用:
error[E0392]: parameter `R` is never used
--> <anon>:11:24
|
11 | struct MyIterThing<'a, R: Read, T: MyReader<R>+'a> {
| ^ unused type parameter
|
= help: consider removing `R` or using a marker such as `std::marker::PhantomData`
如何创建我的MyIterThing 结构?
【问题讨论】:
-
好吧,我很抱歉。推送评论后立即注意到。 :x 你有没有听从
PhantomData的建议? -
@E_net4 我不太确定该怎么做?