Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何复制锈流_Rust_Rust Tokio - Fatal编程技术网

Rust 如何复制锈流

Rust 如何复制锈流,rust,rust-tokio,Rust,Rust Tokio,我有一个类似于以下内容的程序: struct MyEvent { /* some fields */ } struct MyStruct { /* some fields */ } struct MyStreamer { /* holds some state */ } impl MyStreamer { pub fn stream_objects<'a, 'b: 'a>( &'a self, event_stream: Pin<

我有一个类似于以下内容的程序:

struct MyEvent { /* some fields */ }
struct MyStruct { /* some fields */ }
struct MyStreamer { /* holds some state */ }

impl MyStreamer {
    pub fn stream_objects<'a, 'b: 'a>(
        &'a self,
        event_stream: Pin<Box<dyn Stream<Item = MyEvent> + Send + 'b>>,
    ) -> Pin<Box<dyn Stream<Item = Arc<MyStruct>> + Send + 'a>> { /* implementation */ }
}
    |
155 |         struct_stream: Pin<Box<dyn Stream<Item = Arc<MyStruct>> + Send + 'b>>,
    |                               ----------------------------------------------- this data with lifetime `'b`...
...
165 |         let _handle = tokio::spawn(async move { struct_stream.map(Ok).forward(s).await });
    |                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...is captured here...
    |
note: ...and is required to live as long as `'static` here
   --> *file name here*
    |
165 |         let _handle = tokio::spawn(async move { struct_stream.map(Ok).forward(s).await });
    |                       ^^^^^^^^^^^^
在这一点上,我被告知如下:

struct MyEvent { /* some fields */ }
struct MyStruct { /* some fields */ }
struct MyStreamer { /* holds some state */ }

impl MyStreamer {
    pub fn stream_objects<'a, 'b: 'a>(
        &'a self,
        event_stream: Pin<Box<dyn Stream<Item = MyEvent> + Send + 'b>>,
    ) -> Pin<Box<dyn Stream<Item = Arc<MyStruct>> + Send + 'a>> { /* implementation */ }
}
    |
155 |         struct_stream: Pin<Box<dyn Stream<Item = Arc<MyStruct>> + Send + 'b>>,
    |                               ----------------------------------------------- this data with lifetime `'b`...
...
165 |         let _handle = tokio::spawn(async move { struct_stream.map(Ok).forward(s).await });
    |                                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...is captured here...
    |
note: ...and is required to live as long as `'static` here
   --> *file name here*
    |
165 |         let _handle = tokio::spawn(async move { struct_stream.map(Ok).forward(s).await });
    |                       ^^^^^^^^^^^^
|
155 | struct_stream:Pin我认为这是

编译器是正确的,您可能应该考虑生命周期,因为扇出是正常的


tokio::spawn
需要
'static
,并且您已经为
结构流
指定了
'b
生存期。也许将
struct\u流
包装成
Arc/Rc

我认为“复制”这个词不适合您所寻找的内容。也许是“拆分”。@Elazar我曾考虑使用“拆分”,但觉得这意味着我希望流的某些元素转到流1,而另一些元素转到流2。@RaduSzasz两个相同的流,如
a.next()
b.next()
将是相同的,或者
a
b
都共享一个底层流并调用
。一个上的next()
将推进这两个流(类似于
Iterator::by_ref
)?@Aplet123
a.next()
b.next()
应返回相同的流。假设
struct\u stream=stream::iter!(vec![1,2,3].into_iter())
let(s1,s2)=复制_流(struct_流)
。然后
s1.next()
返回
1
。同上,
s2.next()
返回
1
。感谢您的回答!我的问题是关于“我有一条流,我想有两条流。我该怎么做?”请随意建议一个更好的措辞,以清楚地表达出来。我通常会在我的问题中包括我已经尝试过的东西,这样很明显,当我遇到这个问题时,我并没有立即在StackOverflow上发表文章,而且大家都知道我到目前为止所走的道路。我不指望我收到的答案能确定我的寿命(除非这是解决这个问题的最佳方式)。@RaduSzasz,我想我在评论的后半部分做出了回应。我认为(我不是最好的rustacean,但大部分使用tokio)你所做的(使用扇出)是正确的方法,因为你使用的生命周期不正确,我建议解决生命周期不匹配的一种方法是使用ref计数包装
struct\u stream
。您的方法不起作用,因为它是错误的,但因为代码不正确。