How to send generic T to another thread? - multithreading

How to send generic T?
I try to send a generic T to another thread but I'm getting:
error[E0308]: mismatched types
--> src/main.rs:23:22
|
23 | t1.merge(Element(vec![3]));
| ^^^^^^^^^^^^^^^^ expected associated type, found struct `Element`
|
= note: expected associated type `<T as Join>::Item`
found struct `Element`
= help: consider constraining the associated type `<T as Join>::Item` to `Element`
Full code:
trait Join {
type Item;
fn merge(&mut self, other: Self::Item);
}
#[derive(Debug, Default)]
struct Element(Vec<u8>);
impl Join for Element {
type Item = Element;
fn merge(&mut self, mut other: Self::Item) {
self.0.append(&mut other.0);
}
}
fn work<T>()
where
T: Default + Join + Send + Sync + 'static,
{
let (sender, receiver) = std::sync::mpsc::channel::<(T)>();
std::thread::spawn(move || {
while let (mut t1) = receiver.recv().unwrap() {
t1.merge(Element(vec![3]));
}
});
loop {
let mut t1 = T::default();
sender.send(t1);
std::thread::sleep(std::time::Duration::from_secs(5));
}
}
fn main() {
// works!
let mut e = Element(vec![1]);
e.merge(Element(vec![2]));
// bad!
work::<Element>();
}
Playground link

When you use generics you let the caller decide which types must be used by your generic function.
This line in your example t1.merge(Element(vec![3])); is invalid because it assumes T = Element but the caller can chose from infinitely many possible types of T where T != Element which is why the compiler is complaining.
To make your function fully generic you have to do something like add a Default bound to <T as Join>::Item in the function signature and then change the offending line to t1.merge(<T as Join>::Item::default());.
Updated working commented example:
use std::fmt::Debug;
trait Join {
type Item;
fn merge(&mut self, other: Self::Item);
}
#[derive(Debug)]
struct Element(Vec<u8>);
// updated Default impl so we can observe merges
impl Default for Element {
fn default() -> Self {
Element(vec![1])
}
}
impl Join for Element {
type Item = Element;
fn merge(&mut self, mut other: Self::Item) {
self.0.append(&mut other.0);
}
}
fn work<T>() -> Result<(), Box<dyn std::error::Error>>
where
T: Default + Join + Send + Sync + Debug + 'static,
<T as Join>::Item: Default, // added Default bound here
{
let (sender, receiver) = std::sync::mpsc::channel::<T>();
std::thread::spawn(move || {
while let Ok(mut t1) = receiver.recv() {
// changed this to use Default impl
t1.merge(<T as Join>::Item::default());
// prints "Element([1, 1])" three times
println!("{:?}", t1);
}
});
let mut iterations = 3;
loop {
let t1 = T::default();
sender.send(t1)?;
std::thread::sleep(std::time::Duration::from_millis(100));
iterations -= 1;
if iterations == 0 {
break;
}
}
Ok(())
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// works!
let mut e = Element(vec![1]);
e.merge(Element(vec![2]));
// now also works!
work::<Element>()?;
Ok(())
}
playground

Related

How to fire async callback in rust

I'm trying to implement a StateMachine in Rust, but I encountered some problems while trying to fire the callback of StateMachine in a spawn thread.
Here is my StateMachine struct. The state is a generic T because I want to use it in many different scenerios, and I use a Vec to store all the callbacks those registered into this StateMachine.
At the very begining, I didn't use the lifetime 'a, but it will run into some lifetime problems, so I add the lifetime 'a by this suggestion: Idiomatic callbacks in Rust
pub struct StateMachine<'a, T> where T:Clone+Eq+'a {
state: RwLock<T>,
listeners2: Vec<Arc<Mutex<ListenerCallback<'a, T>>>>,
}
pub type ListenerCallback<'a, T> = dyn FnMut(T) -> Result<()> + Send + Sync + 'a ;
When the state is changed, the StateMachine will fire all the callbacks, as follows.
pub async fn try_set(&mut self, new_state:T) -> Result<()> {
if (block_on(self.state.read()).deref().eq(&new_state)) {
return Ok(())
}
// todo change the state
// fire every listener in spawn
let mut fire_results = vec![];
for listener in &mut self.listeners2 {
let state = new_state.clone();
let fire_listener = listener.clone();
fire_results.push(tokio::spawn(async move {
let mut guard = fire_listener.lock().unwrap();
guard.deref_mut()(state);
}));
}
// if fire result return Err, return it
for fire_result in fire_results {
fire_result.await?;
}
Ok(())
}
But it will cause a compilation error.
error[E0521]: borrowed data escapes outside of associated function
--> src/taf/taf-core/src/execution/state_machine.rs:54:33
|
15 | impl<'a,T> StateMachine<'a,T> where T:Clone+Eq+Send {
| -- lifetime `'a` defined here
...
34 | pub async fn try_set(&mut self, new_state:T) -> Result<()> {
| --------- `self` is a reference that is only valid in the associated function body
...
54 | let fire_listener = listener.clone();
| ^^^^^^^^^^^^^^^^
| |
| `self` escapes the associated function body here
| argument requires that `'a` must outlive `'static`
##########################################################
The full code is coupled with a lot of business logic, so I rewrite 2 demos as follows, the problems is the same. The first demo fire callback synchronously and it works, the second demo try to fire callback asynchronously, it encounter the same problem: self escapes the associated function body here.
First demo(it works):
use std::alloc::alloc;
use std::ops::DerefMut;
use std::sync::{Arc, Mutex, RwLock};
use anyhow::Result;
use dashmap::DashMap;
struct StateMachine<'a,T> where T:Clone+Eq+'a {
state: T,
listeners: Vec<Box<Callback<'a, T>>>,
}
type Callback<'a, T> = dyn FnMut(T) -> Result<()> + Send + Sync + 'a;
impl<'a, T> StateMachine<'a,T> where T:Clone+Eq+'a {
pub fn new(init_state: T) -> Self {
StateMachine {
state: init_state,
listeners: vec![]
}
}
pub fn add_listener(&mut self, listener: Box<Callback<'a, T>>) -> Result<()> {
self.listeners.push(listener);
Ok(())
}
pub fn set(&mut self, new_state: T) -> Result<()> {
self.state = new_state.clone();
for listener in &mut self.listeners {
listener(new_state.clone());
}
Ok(())
}
}
#[derive(Clone, Eq, PartialEq, Hash)]
enum ExeState {
Waiting,
Running,
Finished,
Failed,
}
struct Execution<'a> {
exec_id: String,
pub state_machine: StateMachine<'a, ExeState>,
}
struct ExecManager<'a> {
all_jobs: Arc<RwLock<DashMap<String, Execution<'a>>>>,
finished_jobs: Arc<RwLock<Vec<String>>>,
}
impl<'a> ExecManager<'a> {
pub fn new() -> Self {
ExecManager {
all_jobs: Arc::new(RwLock::new(DashMap::new())),
finished_jobs: Arc::new(RwLock::new(vec![]))
}
}
fn add_job(&mut self, job_id: String) {
let mut execution = Execution {
exec_id: job_id.clone(),
state_machine: StateMachine::new(ExeState::Waiting)
};
// add listener
let callback_finished_jobs = self.finished_jobs.clone();
let callback_job_id = job_id.clone();
execution.state_machine.add_listener( Box::new(move |new_state| {
println!("listener fired!, job_id {}", callback_job_id.clone());
if new_state == ExeState::Finished || new_state == ExeState::Failed {
let mut guard = callback_finished_jobs.write().unwrap();
guard.deref_mut().push(callback_job_id.clone());
}
Ok(())
}));
let mut guard = self.all_jobs.write().unwrap();
guard.deref_mut().insert(job_id, execution);
}
fn mock_exec(&mut self, job_id: String) {
let mut guard = self.all_jobs.write().unwrap();
let mut exec = guard.deref_mut().get_mut(&job_id).unwrap();
exec.state_machine.set(ExeState::Finished);
}
}
#[test]
fn test() {
let mut manager = ExecManager::new();
manager.add_job(String::from("job_id1"));
manager.add_job(String::from("job_id2"));
manager.mock_exec(String::from("job_id1"));
manager.mock_exec(String::from("job_id2"));
}
Second demo:
use std::alloc::alloc;
use std::ops::DerefMut;
use std::sync::{Arc, Mutex, RwLock};
use anyhow::Result;
use dashmap::DashMap;
use petgraph::algo::astar;
struct StateMachine<'a,T> where T:Clone+Eq+Send+'a {
state: T,
listeners: Vec<Arc<Mutex<Box<Callback<'a, T>>>>>,
}
type Callback<'a, T> = dyn FnMut(T) -> Result<()> + Send + Sync + 'a;
impl<'a, T> StateMachine<'a,T> where T:Clone+Eq+Send+'a {
pub fn new(init_state: T) -> Self {
StateMachine {
state: init_state,
listeners: vec![]
}
}
pub fn add_listener(&mut self, listener: Box<Callback<'a, T>>) -> Result<()> {
self.listeners.push(Arc::new(Mutex::new(listener)));
Ok(())
}
pub fn set(&mut self, new_state: T) -> Result<()> {
self.state = new_state.clone();
for listener in &mut self.listeners {
let spawn_listener = listener.clone();
tokio::spawn(async move {
let mut guard = spawn_listener.lock().unwrap();
guard.deref_mut()(new_state.clone());
});
}
Ok(())
}
}
#[derive(Clone, Eq, PartialEq, Hash)]
enum ExeState {
Waiting,
Running,
Finished,
Failed,
}
struct Execution<'a> {
exec_id: String,
pub state_machine: StateMachine<'a, ExeState>,
}
struct ExecManager<'a> {
all_jobs: Arc<RwLock<DashMap<String, Execution<'a>>>>,
finished_jobs: Arc<RwLock<Vec<String>>>,
}
impl<'a> ExecManager<'a> {
pub fn new() -> Self {
ExecManager {
all_jobs: Arc::new(RwLock::new(DashMap::new())),
finished_jobs: Arc::new(RwLock::new(vec![]))
}
}
fn add_job(&mut self, job_id: String) {
let mut execution = Execution {
exec_id: job_id.clone(),
state_machine: StateMachine::new(ExeState::Waiting)
};
// add listener
let callback_finished_jobs = self.finished_jobs.clone();
let callback_job_id = job_id.clone();
execution.state_machine.add_listener( Box::new(move |new_state| {
println!("listener fired!, job_id {}", callback_job_id.clone());
if new_state == ExeState::Finished || new_state == ExeState::Failed {
let mut guard = callback_finished_jobs.write().unwrap();
guard.deref_mut().push(callback_job_id.clone());
}
Ok(())
}));
let mut guard = self.all_jobs.write().unwrap();
guard.deref_mut().insert(job_id, execution);
}
fn mock_exec(&mut self, job_id: String) {
let mut guard = self.all_jobs.write().unwrap();
let mut exec = guard.deref_mut().get_mut(&job_id).unwrap();
exec.state_machine.set(ExeState::Finished);
}
}
#[test]
fn test() {
let mut manager = ExecManager::new();
manager.add_job(String::from("job_id1"));
manager.add_job(String::from("job_id2"));
manager.mock_exec(String::from("job_id1"));
manager.mock_exec(String::from("job_id2"));
}
Compile error of second demo:
error[E0521]: borrowed data escapes outside of associated function
--> generic/src/callback2.rs:34:34
|
15 | impl<'a, T> StateMachine<'a,T> where T:Clone+Eq+Send+'a {
| -- lifetime `'a` defined here
...
29 | pub fn set(&mut self, new_state: T) -> Result<()> {
| --------- `self` is a reference that is only valid in the associated function body
...
34 | let spawn_listener = listener.clone();
| ^^^^^^^^^^^^^^^^
| |
| `self` escapes the associated function body here
| argument requires that `'a` must outlive `'static`
|
= note: requirement occurs because of the type `std::sync::Mutex<Box<dyn FnMut(T) -> Result<(), anyhow::Error> + Send + Sync>>`, which makes the generic argument `Box<dyn FnMut(T) -> Result<(), anyhow::Error> + Send + Sync>` invariant
= note: the struct `std::sync::Mutex<T>` is invariant over the parameter `T`
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
Tasks spawned with tokio::spawn() cannot use borrowed data (here, the data with lifetime 'a, whatever it may be). This is because there is not currently (and likely will never be) any way to guarantee that the borrowed data reliably outlives the spawned task.
You have two choices:
Fire the notifications without spawning. You can put the notification futures into a FuturesUnordered to run them all concurrently, but they will still all have to finish before try_set() does.
Remove the lifetime parameter; stop allowing callbacks that borrow data. Put 'static on your dyn types where necessary. Change the users of the StateMachine so they do not try to use borrowed data but use Arc instead, if necessary.
pub struct StateMachine<T> where T: Clone + Eq + 'static {
state: RwLock<T>,
listeners2: Vec<Arc<Mutex<ListenerCallback<T>>>>,
}
pub type ListenerCallback<T> = dyn FnMut(T) -> Result<()> + Send + Sync + 'static;

What is the idiomatic way to handle the actual data and not care about how the data is protected/wrapped?

This is valid code:
use std::rc::Rc;
use std::sync::{Arc, Mutex};
fn foo(n: i32) {
println!("n = {}", n)
}
fn main() {
let a = 1;
let b = Rc::new(2);
let c = Mutex::new(3);
let d = Arc::new(Mutex::new(4));
foo(a);
foo(*b);
foo(*(c.lock().unwrap()));
foo(*((*d).lock().unwrap()));
}
Are there any traits (or anything else) that I can implement so that the function calls could simply become:
foo(a);
foo(b);
foo(c);
foo(d);
What is the Rust idiomatic way for handling the actual data and not caring about how the data is protected/wrapped?
As others have pointed out, it is a bad idea to ignore the fallibility of Mutex::lock. However for the other cases, you can make your function accept owned values and references transparently by using Borrow:
use std::borrow::Borrow;
use std::rc::Rc;
fn foo (n: impl Borrow<i32>) {
println!("n = {}", n.borrow())
}
fn main() {
let a = 1;
let b = Rc::new (2);
let c = &3;
foo (a);
foo (b);
foo (c);
}
Playground
Here is an extremely literal answer to your specific question. I wouldn't use it.
use std::{
rc::Rc,
sync::{Arc, Mutex},
};
fn foo(n: impl DontCare<Output = i32>) {
let n = n.gimme_it();
println!("n = {}", n)
}
fn main() {
let a = 1;
let b = Rc::new(2);
let c = Mutex::new(3);
let d = Arc::new(Mutex::new(4));
foo(a);
foo(b);
foo(c);
foo(d);
}
trait DontCare {
type Output;
fn gimme_it(self) -> Self::Output;
}
impl DontCare for i32 {
type Output = Self;
fn gimme_it(self) -> Self::Output {
self
}
}
impl<T: DontCare> DontCare for Mutex<T> {
type Output = T::Output;
fn gimme_it(self) -> Self::Output {
self.into_inner()
.expect("Lets first assume unwrap() will not panic")
.gimme_it()
}
}
impl<T: DontCare> DontCare for Rc<T> {
type Output = T::Output;
fn gimme_it(self) -> Self::Output {
match Rc::try_unwrap(self) {
Ok(v) => v.gimme_it(),
_ => unreachable!("Lets first assume unwrap() will not panic"),
}
}
}
impl<T: DontCare> DontCare for Arc<T> {
type Output = T::Output;
fn gimme_it(self) -> Self::Output {
match Arc::try_unwrap(self) {
Ok(v) => v.gimme_it(),
_ => unreachable!("Lets first assume unwrap() will not panic"),
}
}
}
The function signatures you've specified take ownership of the value. That will be highly painful, especially paired with any type that doesn't implement Copy.
There are a number of code paths that panic implicitly. I'm not a fan of baking in panics — I reserve that for algorithmic failures, not data-driven ones.
See also:
Why is it discouraged to accept a reference to a String (&String), Vec (&Vec), or Box (&Box) as a function argument?
My answer is similar to Shepmaster's answer, but maybe a little more practical since it doesn't consume the argument to foo2 and it gives you a reference to the target instead of taking it out of the container.
I never have any luck implementing traits based on other traits so I didn't try to do that here.
use std::ops::Deref;
use std::rc::Rc;
use std::sync::{Arc, Mutex, MutexGuard};
fn foo(n: i32) {
println!("n = {}", n)
}
trait Holding<T> {
type Holder: Deref<Target = T>;
fn held(self) -> Self::Holder;
}
fn foo2<H: Holding<i32>>(x: H) {
let h = x.held();
println!("x = {}", *h);
}
impl<'a, T: 'a> Holding<T> for &'a T {
type Holder = &'a T;
fn held(self) -> Self::Holder {
self
}
}
impl<'a, T> Holding<T> for &'a Rc<T> {
type Holder = &'a T;
fn held(self) -> Self::Holder {
&**self
}
}
impl<'a, T> Holding<T> for &'a Mutex<T> {
type Holder = MutexGuard<'a, T>;
fn held(self) -> Self::Holder {
// this can panic
self.lock().unwrap()
}
}
impl<'a, T> Holding<T> for &'a Arc<Mutex<T>> {
type Holder = MutexGuard<'a, T>;
fn held(self) -> Self::Holder {
// this can panic
(*self).lock().unwrap()
}
}
fn main() {
let a = 1;
let b = Rc::new(2);
let c = Mutex::new(3);
let d = Arc::new(Mutex::new(4));
foo(a);
foo(*b);
foo(*(c.lock().unwrap()));
foo(*((*d).lock().unwrap()));
foo2(&a);
foo2(&b);
foo2(&c);
foo2(&d);
}

How to implement a macro that defines a new public type and returns an instance of that type?

I want to implement a struct using macro_rules! because the generics require a lot of boilerplate and trait hunting.
The struct in question has a hash table inside but the key and the value types are to be provided by the user. The code is as follows:
macro_rules! new_ytz {
($T: ty) => {
// define the struct
pub struct Ytz {
table: hashbrown::hash_map::HashMap<$T, $T>,
}
impl Ytz {
pub fn new() -> Self {
Ytz {
table: hashbrown::hash_map::HashMap::<$T, $T>::new(),
}
}
pub fn add(&mut self, item: &$T) {
if self.table.contains_key(item) {
*self.table.get_mut(item).unwrap() += *item;
} else {
self.table.insert(*item, *item);
}
}
pub fn largest(&self) -> $T {
let mut result = 0;
for v in self.table.values() {
if result < *v {
result = *v;
}
}
result
}
}
// construct an instance of the struct and return it
Ytz::new()
};
}
// driver
fn main() {
let mut y = new_ytz!(u64); // should construct the object and return Ytz::new()
y.add(&71);
y.add(&25);
y.add(&25);
y.add(&25);
y.add(&34);
println!("{}", y.largest());
}
This won't compile since it tries to paste the struct within the main function:
error: expected expression, found keyword `pub`
--> src/main.rs:4:9
|
4 | pub struct Ytz {
| ^^^ expected expression
...
40 | let mut y = new_ytz!(u64); // should construct the object and return Ytz::new()
| ------------- in this macro invocation
How can I work around it? How can I paste the struct outside the main function publicly, along with the impl block?
generics require a lot of boilerplate
use std::collections::HashMap;
use core::hash::Hash;
use std::ops::AddAssign;
struct YtzU64<T: Eq + Ord + Hash + Copy + AddAssign> {
table: HashMap<T, T>
}
impl<T: Eq + Ord + Hash + Copy + AddAssign> YtzU64<T> {
pub fn new() -> Self {
Self {
table: HashMap::new()
}
}
pub fn add(&mut self, item: &T) {
if let Some(item) = self.table.get_mut(item) {
*item += *item;
} else {
self.table.insert(*item, *item);
}
}
pub fn largest(&self) -> Option<T> {
let mut values = self.table.values();
let mut largest:Option<T> = values.next().map(|t| *t);
for v in values {
if largest < Some(*v) {
largest = Some(*v);
}
}
largest
}
}
fn main() {
let mut y = YtzU64::new();
y.add(&71);
y.add(&25);
y.add(&25);
y.add(&25);
y.add(&34);
println!("{}", y.largest().unwrap());
}
My translation of your macro requires less boilerplate than your macro. It has two fewer indents, 4 fewer lines (macro_rules!, pattern matching at the top, two close braces at the end). Note that I changed the api slightly, as largest now returns an Option, to match std::iter::Iterator::max(). Also note that your api design is limited to T:Copy. You would have to redesign it a little if you want to support T: ?Copy + Clone or T: ?Copy + ?Clone.
trait hunting
The compiler is your friend. Watch what happens when I remove one of the trait bounds
error[E0277]: the trait bound `T: std::hash::Hash` is not satisfied
...
Using a macro is an interesting exercise, but re-implementing generics using macros is not useful.

What is the type/trait produced by calling the `chars` method on a string literal?

Consider this piece of code:
struct Collector<T>
where
T: Iterator<Item = char>,
{
current: u32,
right_neighbour: u32,
counter: usize,
iterator: T,
}
impl<T: Iterator<Item = char>> Collector<T> {
fn next(&self) -> u32 {
self.iterator
.next()
.expect("failed to get next digit!")
.to_digit(10)
.expect("failed to prase char as digit!")
}
fn collect(&self) {
self.current = self.right_neighbour;
self.right_neighbour = self.next();
self.counter = self.counter + 1;
}
fn initialize<U>(iterator: U) -> Collector<U>
where
U: Iterator<Item = char>,
{
let mut collector = Collector {
current: 0,
right_neighbour: 0,
counter: 0,
iterator: iterator,
};
collector.collect();
collector
}
}
fn main() {
let numstr = "1111";
let mut collector = Collector::initialize(numstr.chars().cycle().peekable());
}
It produces a type mismatch error:
error[E0284]: type annotations required: cannot resolve `<_ as std::iter::Iterator>::Item == char`
--> src/main.rs:46:25
|
46 | let mut collector = Collector::initialize(numstr.chars().cycle().peekable());
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: required by `<Collector<T>>::initialize`
What is the type of numstr.chars().cycle().peekable()? The compiler tells me that its full type is:
std::iter::Peekable<std::iter::Cycle<std::str::Chars<'_>>>
I know that I can't use that type in the definition of my structs/functions, because it doesn't have an explicit lifetime...
How can I correctly write this code?
What is the type/trait produced by calling the chars method on a string literal?
The documentation for str::chars tells you exactly what type it is:
fn chars(&self) -> Chars
It produces a type mismatch error
Yes, because you haven't specified what the concrete type of T should be. You've introduced a completely separate generic type U and have no arguments or return types that reference T. The compiler has zero context to use to infer what T is.
How can I correctly write this code?
Remove the useless extra type parameter:
struct Collector<T>
where
T: Iterator<Item = char>,
{
current: u32,
right_neighbour: u32,
counter: usize,
iterator: T,
}
impl<T: Iterator<Item = char>> Collector<T> {
fn new(iterator: T) -> Collector<T> {
let mut collector = Collector {
current: 0,
right_neighbour: 0,
counter: 0,
iterator: iterator,
};
collector.collect();
collector
}
fn collect(&self) {
unimplemented!()
}
fn next(&self) -> u32 {
unimplemented!()
}
}
fn main() {
let numstr = "1111";
let mut collector = Collector::new(numstr.chars().cycle().peekable());
}
I've removed the implementations of next and collect because they have other, unrelated errors that I don't care to fix. I also renamed initialize to new, as new is the standard constructor name in the absence of multiple constructors.
Of note is that the usage of peekable here is completely useless. The generic type T doesn't know that it's possible to call peek because there's no appropriate trait bound for such.
because it doesn't have an explicit lifetime
You don't have to care about the lifetime, that will be covered by the generic. If your type needed to know that it was peekable, just put that in your struct:
struct Collector<T>
where
T: Iterator<Item = char>,
{
// ...
iterator: std::iter::Peekable<T>,
}
impl<T: Iterator<Item = char>> Collector<T> {
fn new(iterator: T) -> Collector<T> {
let mut collector = Collector {
// ...
iterator: iterator.peekable(),
};
// ...
}
// ...
}
fn main() {
// ...
let mut collector = Collector::new(numstr.chars().cycle());
}

How can I clone a FnBox closure when it cannot be put into an Arc?

I would like to send commands (closures) to different threads, with the closure capturing a non-Sync variable (therefore I cannot "share" the closure with an Arc, as explained in Can you clone a closure?).
The closure only captures elements which implement Clone, so I would feel that the closure could derive Clone as well.
#![feature(fnbox)]
use std::boxed::FnBox;
use std::sync::mpsc::{self, Sender};
use std::thread;
type Command = Box<FnBox(&mut SenderWrapper) + Send>;
struct SenderWrapper {
tx: Option<Sender<u64>>,
}
impl SenderWrapper {
fn new() -> SenderWrapper {
SenderWrapper { tx: None }
}
}
fn main() {
let (responses_tx, responses_rx) = mpsc::channel();
let closure: Command = Box::new(move |snd: &mut SenderWrapper| {
snd.tx = Some(responses_tx); // Captures tx, which is not Sync but is Clone
});
let mut commands = Vec::new();
for i in 0..2i32 {
let (commands_tx, commands_rx) = mpsc::channel();
commands.push(commands_tx);
thread::spawn(move || {
let mut wrapper = SenderWrapper::new();
let command: Command = commands_rx.recv().unwrap();
command.call_box((&mut wrapper,));
// Continue ...
});
}
for tx in commands.iter() {
commands[0].send(closure.clone()).unwrap(); // How can I make this clone() work?
}
// use answers ...
}
error[E0599]: no method named `clone` found for type `std::boxed::Box<for<'r> std::boxed::FnBox<(&'r mut SenderWrapper,), Output=()> + 'static>` in the current scope
--> src/main.rs:40:34
|
40 | commands[0].send(closure.clone()).unwrap();
| ^^^^^
|
= note: the method `clone` exists but the following trait bounds were not satisfied:
`std::boxed::Box<for<'r> std::boxed::FnBox<(&'r mut SenderWrapper,), Output=()>> : std::clone::Clone`
Is there any way in the current syntax where we can implement / derive traits for closures?
A dirty workaround for this would be to define (by hand) a structure containing the required environment, implement Clone, and define the FnOnce or Invoke trait.
Rust 1.35
Box<dyn FnOnce> is stable. Your code now works in stable Rust if you change it to only box the closure after cloning it:
use std::sync::mpsc::{self, Sender};
use std::thread;
type Command = Box<FnOnce(&mut SenderWrapper) + Send>;
struct SenderWrapper {
tx: Option<Sender<u64>>,
}
impl SenderWrapper {
fn new() -> SenderWrapper {
SenderWrapper { tx: None }
}
}
fn main() {
let (responses_tx, responses_rx) = mpsc::channel();
let closure = move |snd: &mut SenderWrapper| {
snd.tx = Some(responses_tx); // Captures tx, which is not Sync but is Clone
};
let mut commands = Vec::new();
for i in 0..2i32 {
let (commands_tx, commands_rx) = mpsc::channel();
commands.push(commands_tx);
thread::spawn(move || {
let mut wrapper = SenderWrapper::new();
let command: Command = commands_rx.recv().unwrap();
command(&mut wrapper);
// Continue ...
});
}
for tx in commands.iter() {
tx.send(Box::new(closure.clone())).unwrap(); // How can I make this clone() work?
}
// use answers ...
}
See also:
How to clone a struct storing a boxed trait object?
Rust 1.26
Closures now implement Clone
Prior
This doesn't answer your direct question, but is it feasible to just clone your variables before capturing them in the closure?
for tx in commands.iter() {
let my_resp_tx = responses_tx.clone();
let closure = Box::new(move |snd: &mut SenderWrapper| {
snd.tx = Some(my_resp_tx);
});
commands[0].send(closure).unwrap();
}
You can even extract this logic into a "factory" function.
Let's take a deeper look. First, we recognize that Box<FnBox> is a trait object, and cloning those is kind of difficult. Following the answers in How to clone a struct storing a boxed trait object?, and using a smaller case, we end up with:
#![feature(fnbox)]
use std::boxed::FnBox;
type Command = Box<MyFnBox<Output = ()> + Send>;
trait MyFnBox: FnBox(&mut u8) + CloneMyFnBox {}
trait CloneMyFnBox {
fn clone_boxed_trait_object(&self) -> Box<MyFnBox<Output = ()> + Send>;
}
impl<T> CloneMyFnBox for T
where
T: 'static + MyFnBox + Clone + Send,
{
fn clone_boxed_trait_object(&self) -> Box<MyFnBox<Output = ()> + Send> {
Box::new(self.clone())
}
}
impl Clone for Box<MyFnBox<Output = ()> + Send> {
fn clone(&self) -> Box<MyFnBox<Output = ()> + Send> {
self.clone_boxed_trait_object()
}
}
fn create() -> Command {
unimplemented!()
}
fn main() {
let c = create();
c.clone();
}
Of note is that we had to introduce a trait to implement cloning and another trait to combine the cloning trait with FnBox.
Then it's "just" a matter of implementing MyFnBox for all implementors of FnBox and enabling another nightly feature: #![clone_closures] (slated for stabilization in Rust 1.28):
#![feature(fnbox)]
#![feature(clone_closures)]
use std::boxed::FnBox;
use std::sync::mpsc::{self, Sender};
use std::thread;
struct SenderWrapper {
tx: Option<Sender<u64>>,
}
impl SenderWrapper {
fn new() -> SenderWrapper {
SenderWrapper { tx: None }
}
}
type Command = Box<MyFnBox<Output = ()> + Send>;
trait MyFnBox: FnBox(&mut SenderWrapper) + CloneMyFnBox {}
impl<T> MyFnBox for T
where
T: 'static + FnBox(&mut SenderWrapper) + Clone + Send,
{
}
trait CloneMyFnBox {
fn clone_boxed_trait_object(&self) -> Box<MyFnBox<Output = ()> + Send>;
}
impl<T> CloneMyFnBox for T
where
T: 'static + MyFnBox + Clone + Send,
{
fn clone_boxed_trait_object(&self) -> Box<MyFnBox<Output = ()> + Send> {
Box::new(self.clone())
}
}
impl Clone for Box<MyFnBox<Output = ()> + Send> {
fn clone(&self) -> Box<MyFnBox<Output = ()> + Send> {
self.clone_boxed_trait_object()
}
}
fn main() {
let (responses_tx, responses_rx) = mpsc::channel();
let closure: Command = Box::new(move |snd: &mut SenderWrapper| {
snd.tx = Some(responses_tx);
});
let mut commands = Vec::new();
for i in 0..2i32 {
let (commands_tx, commands_rx) = mpsc::channel();
commands.push(commands_tx);
thread::spawn(move || {
let mut wrapper = SenderWrapper::new();
let command: Command = commands_rx.recv().unwrap();
command.call_box((&mut wrapper,));
// Continue ...
});
}
for tx in commands.iter() {
commands[0].send(closure.clone()).unwrap(); // How can I make this clone() work?
}
// use answers ...
}

Resources