How do I give an mpsc Receiver to a thread?
I am trying to give the mpsc::Receiver
object of a channel to a thread, it will only be called by a websocket client structure.
I've messed around with lifetimes, arcs, mutexes and I don't have enough knowledge about Rust to get it to work appropriately.
Thanks for any help you can give.
extern crate ws;
use ws::util::Token;
use ws::{connect, Handler, Handshake, Message, Result};
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
use std::thread;
fn main() {
let (tx1, rx1) = sync_channel(100);
let (tx2, rx2) = sync_channel(100);
// set up rx2 and tx1
let client = Client::new_thread(rx1, tx2);
client.join().unwrap();
}
pub struct Client {
pub out: ws::Sender,
tx: SyncSender<Message>,
rx: Receiver<Message>,
}
impl Client {
pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
thread::spawn(move || {
connect("ws://localhost:8000/ws/server/main", |out| Client {
out,
tx: tx.clone(),
rx: rx,
}).unwrap()
})
}
}
impl Handler for Client {
fn on_open(&mut self, _: Handshake) -> Result<()> {
self.out.timeout(250, Token(1))
}
fn on_message(&mut self, msg: Message) -> Result<()> {
self.tx.send(msg).unwrap();
Ok(())
}
fn on_timeout(&mut self, event: Token) -> Result<()> {
//let msg = self.rx.recv()
//self.out.send(msg)
self.out.timeout(250, event)
}
}
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> src/main.rs:31:21
|
26 | pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
| -- captured outer variable
...
31 | rx: rx,
| ^^ cannot move out of captured outer variable in an `FnMut` closure
websocket rust client
add a comment |
I am trying to give the mpsc::Receiver
object of a channel to a thread, it will only be called by a websocket client structure.
I've messed around with lifetimes, arcs, mutexes and I don't have enough knowledge about Rust to get it to work appropriately.
Thanks for any help you can give.
extern crate ws;
use ws::util::Token;
use ws::{connect, Handler, Handshake, Message, Result};
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
use std::thread;
fn main() {
let (tx1, rx1) = sync_channel(100);
let (tx2, rx2) = sync_channel(100);
// set up rx2 and tx1
let client = Client::new_thread(rx1, tx2);
client.join().unwrap();
}
pub struct Client {
pub out: ws::Sender,
tx: SyncSender<Message>,
rx: Receiver<Message>,
}
impl Client {
pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
thread::spawn(move || {
connect("ws://localhost:8000/ws/server/main", |out| Client {
out,
tx: tx.clone(),
rx: rx,
}).unwrap()
})
}
}
impl Handler for Client {
fn on_open(&mut self, _: Handshake) -> Result<()> {
self.out.timeout(250, Token(1))
}
fn on_message(&mut self, msg: Message) -> Result<()> {
self.tx.send(msg).unwrap();
Ok(())
}
fn on_timeout(&mut self, event: Token) -> Result<()> {
//let msg = self.rx.recv()
//self.out.send(msg)
self.out.timeout(250, event)
}
}
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> src/main.rs:31:21
|
26 | pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
| -- captured outer variable
...
31 | rx: rx,
| ^^ cannot move out of captured outer variable in an `FnMut` closure
websocket rust client
add a comment |
I am trying to give the mpsc::Receiver
object of a channel to a thread, it will only be called by a websocket client structure.
I've messed around with lifetimes, arcs, mutexes and I don't have enough knowledge about Rust to get it to work appropriately.
Thanks for any help you can give.
extern crate ws;
use ws::util::Token;
use ws::{connect, Handler, Handshake, Message, Result};
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
use std::thread;
fn main() {
let (tx1, rx1) = sync_channel(100);
let (tx2, rx2) = sync_channel(100);
// set up rx2 and tx1
let client = Client::new_thread(rx1, tx2);
client.join().unwrap();
}
pub struct Client {
pub out: ws::Sender,
tx: SyncSender<Message>,
rx: Receiver<Message>,
}
impl Client {
pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
thread::spawn(move || {
connect("ws://localhost:8000/ws/server/main", |out| Client {
out,
tx: tx.clone(),
rx: rx,
}).unwrap()
})
}
}
impl Handler for Client {
fn on_open(&mut self, _: Handshake) -> Result<()> {
self.out.timeout(250, Token(1))
}
fn on_message(&mut self, msg: Message) -> Result<()> {
self.tx.send(msg).unwrap();
Ok(())
}
fn on_timeout(&mut self, event: Token) -> Result<()> {
//let msg = self.rx.recv()
//self.out.send(msg)
self.out.timeout(250, event)
}
}
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> src/main.rs:31:21
|
26 | pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
| -- captured outer variable
...
31 | rx: rx,
| ^^ cannot move out of captured outer variable in an `FnMut` closure
websocket rust client
I am trying to give the mpsc::Receiver
object of a channel to a thread, it will only be called by a websocket client structure.
I've messed around with lifetimes, arcs, mutexes and I don't have enough knowledge about Rust to get it to work appropriately.
Thanks for any help you can give.
extern crate ws;
use ws::util::Token;
use ws::{connect, Handler, Handshake, Message, Result};
use std::sync::mpsc::{Receiver, SyncSender, sync_channel};
use std::thread;
fn main() {
let (tx1, rx1) = sync_channel(100);
let (tx2, rx2) = sync_channel(100);
// set up rx2 and tx1
let client = Client::new_thread(rx1, tx2);
client.join().unwrap();
}
pub struct Client {
pub out: ws::Sender,
tx: SyncSender<Message>,
rx: Receiver<Message>,
}
impl Client {
pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
thread::spawn(move || {
connect("ws://localhost:8000/ws/server/main", |out| Client {
out,
tx: tx.clone(),
rx: rx,
}).unwrap()
})
}
}
impl Handler for Client {
fn on_open(&mut self, _: Handshake) -> Result<()> {
self.out.timeout(250, Token(1))
}
fn on_message(&mut self, msg: Message) -> Result<()> {
self.tx.send(msg).unwrap();
Ok(())
}
fn on_timeout(&mut self, event: Token) -> Result<()> {
//let msg = self.rx.recv()
//self.out.send(msg)
self.out.timeout(250, event)
}
}
error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> src/main.rs:31:21
|
26 | pub fn new_thread(rx: Receiver<Message>, tx: SyncSender<Message>) -> thread::JoinHandle<()> {
| -- captured outer variable
...
31 | rx: rx,
| ^^ cannot move out of captured outer variable in an `FnMut` closure
websocket rust client
websocket rust client
edited Nov 27 '18 at 16:08
E_net4
12.4k73772
12.4k73772
asked Nov 27 '18 at 16:04
mjrtmjrt
113
113
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53503620%2fhow-do-i-give-an-mpsc-receiver-to-a-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53503620%2fhow-do-i-give-an-mpsc-receiver-to-a-thread%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown