锈质性状寿命 [英] Rust trait field lifetime
问题描述
我认为这很明显我很想念,但是可以了.
I think this is something obvious I'm missing, but here goes..
use std::io;
pub trait Source<'a, T> {
fn push(&self, t: T) -> io::Result<()>;
fn link(&mut self, sink: &dyn Sink<'a, T>) -> io::Result<()>;
}
pub trait Sink<'a, T> {
fn push(&self, t: T) -> io::Result<()>;
fn link(&mut self, source: &dyn Source<T>) -> io::Result<()>;
}
pub struct SyncSource<'a, T> {
sink: Option<&'a dyn Sink<'a, T>>,
}
impl<'a, T> SyncSource<'a, T> {
pub fn new() -> SyncSource<'a, T> {
SyncSource {
sink: None,
}
}
}
impl<'a, T> Source<'a, T> for SyncSource<'a, T> {
fn push(&self, t: T) -> io::Result<()> {
match self.sink {
Some(sink) => sink.push(t),
None => Err(io::Error::new(io::ErrorKind::NotConnected, "no sink")),
}
}
fn link(&mut self, sink: &dyn Sink<'a, T>) -> io::Result<()> {
self.sink = Some(sink);
Ok(())
}
}
pub struct SyncSink<'a, T> {
source: Option<&'a dyn Source<'a, T>>,
}
impl<'a, T> SyncSink<'a, T> {
pub fn new() -> SyncSink<'a, T> {
SyncSink {
source: None,
}
}
}
impl<'a, T> Sink<'a, T> for SyncSink<'a, T> {
fn push(&self, t: T) -> io::Result<()> {
match self.source {
Some(source) => source.push(t),
None => Err(io::Error::new(io::ErrorKind::NotConnected, "no source")),
}
}
fn link(&mut self, source: &dyn Source<T>) -> io::Result<()> {
self.source = Some(source);
Ok(())
}
}
我阅读了有关终生的rustlang书,但无法真正理解这里出了什么问题.我正在尝试建立一个基本的管道和过滤器体系结构.一个源知道它的接收器,一个接收器知道它的源,因此我想存储对对象的引用.显然,这里有一个终身问题.
I read the rustlang book chapter about lifetimes but could not really understand what's wrong here. What I'm trying to do is build a basic pipe and filter architecture. A source knows its sink and a sink knows its source, thus I want to store references to the objects. Obviously, there's a lifetime issue here.
我首先想到了引入生存期'a
,以说源/接收器将一直存在,只要它链接到的对象即可.这是行不通的.现在,我想我可能需要一生的'b',它要比'a寿命长,然后以某种方式将其扔进去,但是正如您所看到的,这就是我感到困惑的地方.
I first thought about introducing the lifetime 'a
to say the source/sink shall live as long as the object it is linked to. This does not work. Now I'm thinking I may need a lifetime 'b which outlives 'a and somehow throw that in the mix, but as you can see, this is where I am confused.
推荐答案
您快到了:
use std::io;
pub trait Source<'a, T> {
fn push(&self, t: T) -> io::Result<()>;
// Make sure the references themselves have the 'a lifetime marker
fn link(&'a mut self, sink: &'a dyn Sink<'a, T>) -> io::Result<()>;
}
pub trait Sink<'a, T> {
fn push(&self, t: T) -> io::Result<()>;
// Make sure the references themselves have the 'a lifetime marker
fn link(&'a mut self, source: &'a dyn Source<'a, T>) -> io::Result<()>;
}
pub struct SyncSource<'a, T> {
sink: Option<&'a dyn Sink<'a, T>>,
}
impl<'a, T> SyncSource<'a, T> {
pub fn new() -> SyncSource<'a, T> {
SyncSource {
sink: None,
}
}
}
impl<'a, T> Source<'a, T> for SyncSource<'a, T> {
fn push(&self, t: T) -> io::Result<()> {
match self.sink {
Some(sink) => sink.push(t),
None => Err(io::Error::new(io::ErrorKind::NotConnected, "no sink")),
}
}
// Now match the lifetime definitions that is defined in the trait
fn link(&'a mut self, sink: &'a dyn Sink<'a, T>) -> io::Result<()> {
self.sink = Some(sink);
Ok(())
}
}
pub struct SyncSink<'a, T> {
source: Option<&'a dyn Source<'a, T>>,
}
impl<'a, T> SyncSink<'a, T> {
pub fn new() -> SyncSink<'a, T> {
SyncSink {
source: None,
}
}
}
impl<'a, T> Sink<'a, T> for SyncSink<'a, T> {
fn push(&self, t: T) -> io::Result<()> {
match self.source {
Some(source) => source.push(t),
None => Err(io::Error::new(io::ErrorKind::NotConnected, "no source")),
}
}
// Now match the lifetime definitions that is defined in the trait
fn link(&'a mut self, source: &'a dyn Source<'a, T>) -> io::Result<()> {
self.source = Some(source);
Ok(())
}
}
这篇关于锈质性状寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!