我想用一个容器来管理tokio::oneshot::Sender
s。我正在使用Vec
,但似乎保存的值Vec
是引用,我需要使用self
,而不是引用来调用它:
use bytes::BytesMut;
use tokio::sync::oneshot;
#[derive(Clone)]
pub enum ChannelData {
Video { timestamp: u32, data: BytesMut },
Audio { timestamp: u32, data: BytesMut },
MetaData {},
}
pub type PlayerPublisher = oneshot::Sender<ChannelData>;
pub struct Channel {
player_producers: Vec<PlayerPublisher>, // consumers who subscribe this channel.
}
impl Channel {
fn new() -> Self {
Self {
player_producers: Vec::new(),
}
}
async fn transmit(&mut self) {
let b = BytesMut::new();
let data = ChannelData::Video {
timestamp: 234,
data: b,
};
for i in self.player_producers {
i.send(data);
}
}
}
错误:
error[E0507]: cannot move out of `self.player_producers` which is behind a mutable reference
--> src/lib.rs:31:18
|
31 | for i in self.player_producers {
| ^^^^^^^^^^^^^^^^^^^^^
| |
| move occurs because `self.player_producers` has type `Vec<tokio::sync::oneshot::Sender<ChannelData>>`, which does not implement the `Copy` trait
| help: consider iterating over a slice of the `Vec<_>`'s content: `&self.player_producers`
error[E0382]: use of moved value: `data`
--> src/lib.rs:32:20
|
26 | let data = ChannelData::Video {
| ---- move occurs because `data` has type `ChannelData`, which does not implement the `Copy` trait
...
32 | i.send(data);
| ^^^^ value moved here, in previous iteration of loop
我怎样才能实现我的目标?
pub fn send(mut self, t: T) -> Result<(), T> {
let inner = self.inner.take().unwrap();
inner.value.with_mut(|ptr| unsafe {
*ptr = Some(t);
});
if !inner.complete() {
unsafe {
return Err(inner.consume_value().unwrap());
}
}
Ok(())
}