I am trying to implement Deref
for an enum:
use std::rc::Rc;
use std::ops::Deref;
pub trait tObject {
fn name(&self) -> String;
fn span(&self) -> u32;
}
pub struct t1 {
pub name: String,
pub bd: Option<String>,
pub span: u32,
pub label: Option<String>
}
pub struct t2 {
pub name: String,
pub vrf: Option<String>,
pub span: u32,
pub label: Option<String>,
pub svi: u32
}
pub struct t3 {
pub name: String,
pub span: u32,
pub label: Option<String>
}
impl tObject for t1 {
fn name(&self) -> String {self.name.clone()}
fn span(&self) -> u32 {self.span.clone()}
}
impl tObject for t2 {
fn name(&self) -> String {self.name.clone()}
fn span(&self) -> u32 {self.span.clone()}
}
impl tObject for t3 {
fn name(&self) -> String {self.name.clone()}
fn span(&self) -> u32 {self.span.clone()}
}
pub enum TType {
t1(Rc<t1>),
t2(Rc<t2>),
t3(Rc<t3>)
}
impl Deref for TType {
type Target = tObject;
fn deref<'a>(&'a self) -> &'a tObject {
match *self {
TType::t1(ref x) => x as &t1,
TType::t2(ref x) => x as &t2,
TType::t3(ref x) => x as &t3
}
}
}
fn main() {
let mut t1s: Vec<Rc<t1>> = Vec::new();
let mut t2s: Vec<Rc<t2>> = Vec::new();
let mut t3s: Vec<Rc<t3>> = Vec::new();
let t_iter: Box<Iterator<Item=TType>> = Box::new(t1s.iter().map(|x| TType::t1(x.clone())).chain(
t2s.iter().map(|x| TType::t2(x.clone())).chain(
t3s.iter().map(|x| TType::t3(x.clone())))));
}
The compiler reports this error:
rustc 1.15.1 (021bd294c 2017-02-08)
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter 'a in generic type due to conflicting requirements
--> <anon>:53:5
|
53 | fn deref<'a>(&'a self) -> &'a tObject {
| _____^ starting here...
54 | | match *self {
55 | | TType::t1(ref x) => x as &t1,
56 | | TType::t2(ref x) => x as &t2,
57 | | TType::t3(ref x) => x as &t3
58 | | }
59 | | }
| |_____^ ...ending here
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 53:42...
--> <anon>:53:43
|
53 | fn deref<'a>(&'a self) -> &'a tObject {
| ___________________________________________^ starting here...
54 | | match *self {
55 | | TType::t1(ref x) => x as &t1,
56 | | TType::t2(ref x) => x as &t2,
57 | | TType::t3(ref x) => x as &t3
58 | | }
59 | | }
| |_____^ ...ending here
note: ...so that method type is compatible with trait (expected fn(&TType) -> &tObject + 'static, found fn(&TType) -> &tObject)
--> <anon>:53:5
|
53 | fn deref<'a>(&'a self) -> &'a tObject {
| _____^ starting here...
54 | | match *self {
55 | | TType::t1(ref x) => x as &t1,
56 | | TType::t2(ref x) => x as &t2,
57 | | TType::t3(ref x) => x as &t3
58 | | }
59 | | }
| |_____^ ...ending here
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that method type is compatible with trait (expected fn(&TType) -> &tObject + 'static, found fn(&TType) -> &tObject)
--> <anon>:53:5
|
53 | fn deref<'a>(&'a self) -> &'a tObject {
| _____^ starting here...
54 | | match *self {
55 | | TType::t1(ref x) => x as &t1,
56 | | TType::t2(ref x) => x as &t2,
57 | | TType::t3(ref x) => x as &t3
58 | | }
59 | | }
| |_____^ ...ending here
If I make the return type of deref
Self::Target
instead of tObject
, it compiles fine. I do not understand this behaviour.