I'm experimenting with futures 0.3 for the first time, and getting trait or lifetime issues.
use futures::executor::ThreadPool;
use futures::future::*;
use futures::StreamExt;
const BUFSIZE: usize = 140;
#[derive(Clone)]
struct Packet {
channel: usize,
seq: usize,
total: usize,
buffer: [u8; BUFSIZE],
}
fn to_packets<T>(channel: usize, msg: T) -> Vec<Packet> {
// Trivial implemmentation
vec![Packet {
channel: 0,
seq: 0,
total: 0,
buffer: [0u8; BUFSIZE],
}]
}
pub struct SMConnection;
impl SMConnection {
fn push<T: 'static>(&mut self, channel: &'static usize, msg: T)
where
T: Send + Sync + Clone + Serialize,
{
let threadpool = ThreadPool::new().unwrap();
let future = async {
move || {
to_packets(*channel, msg)
.iter()
.for_each(|_packet| { /* do something */ })
};
};
threadpool.spawn_ok(future);
}
}
This errors with
error[E0597]: `channel` does not live long enough
--> src\network.rs:82:27
|
81 | let future = async {
| ______________________-_____-
| |______________________|
| ||
82 | || let channel = channel.clone();
| || ^^^^^^^ borrowed value does not live long enough
83 | || move || to_packets(channel, msg).iter().for_each(|_| { });
84 | || };
| || -
| ||_________|
| |__________value captured here by generator
| argument requires that `channel` is borrowed for `'static`
85 | threadpool.spawn_ok(future);
86 | }
| - `channel` dropped here while still borrowed
I'm passing channel
and msg
by value so I would not expect there to be a lifetime issue. Following the compiler advice to give 'static
bounds to the arguments still leaves me stuck.
I've tried combinations of clone
and Arc
What have I missed here?
EDIT For reference: Cargo.toml
[dependencies]
serde="^1"
serde_derive="^1"
bincode = "1.1.4"
romio = "0.3.0-alpha.9"
futures-preview = { version = "=0.3.0-alpha.18", features = ["async-await", "nightly"] }
Compiler: rustc 1.39.0-nightly (9b91b9c10 2019-08-26)