こんにちは、私はかなり簡単なテストケース で寿命が問題になりました。コンパイラが私に言っていることを理解できないようです。 以下のコードは、Rustプレイグラウンドにペーストします。寿命のエラー解決方法がわかりません
コードの考え方は、Element
がパイプラインにまとめられているということです。データは最終的に1つの要素を次の要素に渡します。各段は前の段からの入力を受けて次の段にデータを送る(rx, tx)
対を有する。 をSync
とマークすることができるので、Element
は1度にデータの一部を処理します。
エラーは以下のとおりです。
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> <anon>:56:18
|
56 | for e in self.elements {
| ^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the lifetime 'a as defined on the body at 53:53...
--> <anon>:53:54
|
53 | pub fn run(&self) -> Vec<thread::JoinHandle<()>> {
| ^
note: ...so that expression is assignable (expected std::vec::Vec<Box<&Element>>, found std::vec::Vec<Box<&'a Element + 'a>>)
--> <anon>:56:18
|
56 | for e in self.elements {
| ^^^^^^^^^^^^^
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `[[email protected]<anon>:62:41: 64:15 e:Box<&Element>, sender:std::sync::Arc<std::sync::Mutex<std::sync::mpsc::SyncSender<(std::string::String, std::string::String)>>>, receiver:std::sync::Arc<std::sync::Mutex<std::sync::mpsc::Receiver<(std::string::String, std::string::String)>>>]` will meet its required lifetime bounds
--> <anon>:62:27
|
62 | handles.push(thread::spawn(move || {
| ^^^^^^^^^^^^^
私がそのように彼らはPipeline
として一緒に出回っ 固執コンパイラに伝えるべきではありませんElement
sは &'a Element
のように定義されているようで混乱していた最初のエラー?
Vec<thread::JoinHandle<()>>
は生涯に依存していると言われています。'a
?私はそれをどのように伝達するかわかりません。
私は最初の2つを修正した後、3番目の方が意味をなされることを望みます。現時点で私はそれが何を伝えているのか分かりません。前のエラーまたは警告にコンテキストを追加コンパイラによって報告さ
use std::sync::{Arc, Mutex};
use std::thread;
use std::result::Result;
use std::sync::mpsc::{SyncSender, Receiver, sync_channel};
pub trait Element: Send + Sync {
fn run(&self, output: Arc<Mutex<SyncSender<i32>>>,
input: Arc<Mutex<Receiver<i32>>>);
}
pub struct TestElement {}
impl TestElement {
pub fn new() -> Self {
TestElement {}
}
}
impl Element for TestElement {
fn run(&self, output: Arc<Mutex<SyncSender<i32>>>,
input: Arc<Mutex<Receiver<i32>>>) {
println!("Hello");
}
}
pub struct Pipeline<'a> {
elements: Vec<Box<&'a Element>>,
}
impl<'a> Pipeline<'a> {
pub fn new(name: String) -> Self {
Pipeline {
elements: Vec::new(),
}
}
pub fn run(&self) -> Vec<thread::JoinHandle<()>> {
let mut handles = Vec::with_capacity(self.elements.len());
for e in self.elements {
let channel = sync_channel::<i32>(1000);
let sender = Arc::new(Mutex::new(channel.0)).clone();
let receiver = Arc::new(Mutex::new(channel.1)).clone();
handles.push(thread::spawn(move || {
e.run(sender, receiver);
}));
}
handles
}
}
fn main() {
let mut test_element = TestElement::new();
let mut pipeline = Pipeline::new("example pipeline".to_string());
let handles = pipeline.run();
}
あなたはさらに、あなたのコードを簡素化できますか?それはまだかなり大きなコードであり、答えが簡単で、小さなコードを扱うときだけ理解しやすくなります。ありがとう:) –
私は少しサイズを縮小しようとしました。私はより多くを減らすことができますが、それはいくつかのエラーを取り除くだろうと私は彼らが別のエラーであるかどうか私は知らないかどうかは、1つの修正に関連するかどうかは不明でした。どう思いますか ? –
なぜあなたは 'Box <&'a Element>'を使用していますか? 'Box'はすでにヒープが割り当てられており、基本的にはポインタにすぎないので、' Box 'で十分です。これはまたあなたの生涯の問題を解決するでしょう(しかし他の問題については光を照らす)。また、生成されたスレッドは他のものより長く存続する可能性があるので、 'Pipeline :: run()'で生成されたスレッドは 'Pipeline'オブジェクトより長く存続します。これは答えではありませんが、正しい方向への微妙な動きです。 –