Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help needed: how to use stick in tokio? #45

Closed
hkoosha opened this issue Feb 28, 2022 · 3 comments
Closed

Help needed: how to use stick in tokio? #45

hkoosha opened this issue Feb 28, 2022 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@hkoosha
Copy link

hkoosha commented Feb 28, 2022

Hi!
I want one thread to read stick events, another one to pick up the latest read event and send it off on a network socket. For networking purposes I'm using tokio, not pasts crate.

The issue is, I can't spawn a task with tokio as Controller has an Rc inside and rust is unhappy about it not being Send.

Here's the gist of the code:

let mut state = State::new(...);
let mut controller = get_ctl().await;
tokio::spawn(async move {  // => error here => future created by async block is not `Send`
    loop {
        let event = (&mut controller).await; // => also error => has type `Controller` which is not `Send`
        state.event(event);
    }
});

(Please forgive any stupid usage of rust / tokio / stick / async, I'm a newbie to both rust and async!)

Can you please guide me on how to achieve this? how should I send a controller through an async task? does this require fundamental changes to stick to use Arc?

@hkoosha
Copy link
Author

hkoosha commented Feb 28, 2022

I cloned the repo and by turning Rc in stick::Controller into Arc, I still had to implement Send manually to make rust happy. I'm definitely doing something wrong here.

@AldaronLau
Copy link
Member

@hkoosha Thanks for opening this issue! I just put together #46 which should do the work to make the futures Send (apparently a common issue people are running into with my crates, so good to fix). Luckily this change wasn't too much work, let me know if it works or if you have any questions about what I did.

Once a type is !Send, you can't fix that by wrapping it in another type because it still breaks the promise of it being !Send if you are moving the type to another thread. So there are two possible solutions: a) have a thread own that type and communicate via a channel, b) make the type Send - if you can prove it unsafe impl Send {}, otherwise it can be automatically inferred (in this change I just added it as a requirement of the trait, and needed to replace occurrences of Rc with Arc).

@AldaronLau AldaronLau added enhancement New feature or request question Further information is requested labels Mar 2, 2022
@hkoosha
Copy link
Author

hkoosha commented Mar 2, 2022

Awesome! many many thanks.
I will close this and will follow #46 :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants