grammers icon indicating copy to clipboard operation
grammers copied to clipboard

Send for Client

Open nrot opened this issue 3 years ago • 2 comments

Implement Send for Client. For used with tokio::spawn. Becuse used Mutex for Client data, this can be implement. I don`t sure how to do this rigth. Minimal code:

use grammers_client::{Client, Config, InitParams};
use grammers_session::Session;
use tokio::sync::mpsc;

const API_ID: i32 = 0;
const API_HASH: &str = "...";

#[tokio::main]
async fn main() {
    let config = Config {
        api_hash: String::from(API_HASH),
        api_id: API_ID,
        params: InitParams::default(),
        session: Session::new(),
    };
    let client = Client::connect(config).await.unwrap();
    let (txu, rxu) = mpsc::unbounded_channel();
    tokio::spawn(async {
        let c = client.clone();
        loop {
            match client.next_update().await {
                Ok(Some(u)) => {
                    if let Err(e) = txu.send(u) {
                        eprintln!("{}", e);
                        return;
                    }
                }
                Ok(_) => {}
                Err(_) => {}
            }
        }
    });
}

Cargo.toml

[dependencies]
grammers-client = {git = "https://github.com/Lonami/grammers/", branch="master"}
grammers-session = {git = "https://github.com/Lonami/grammers/", branch="master"}
tokio = {version="1.17", features=["rt", "rt-multi-thread", "sync", "macros", "signal", "time"]}

Build error

Checking grammers_test v0.1.0 (grammers_test)
error: generator cannot be sent between threads safely
   --> src\main.rs:18:5
    |
18  |     tokio::spawn(async {
    |     ^^^^^^^^^^^^ future created by async block is not `Send`
    |
    = help: within `impl Future<Output = [async output]>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, MessageBox>`
note: required by a bound in `tokio::spawn`
   --> .cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.17.0\src\task\spawn.rs:127:21
    |
127 |         T: Future + Send + 'static,
    |                     ^^^^ required by this bound in `tokio::spawn`

error: generator cannot be sent between threads safely
   --> src\main.rs:18:5
    |
18  |     tokio::spawn(async {
    |     ^^^^^^^^^^^^ future created by async block is not `Send`
    |
    = help: within `impl Future<Output = [async output]>`, the trait `Send` is not implemented for `std::sync::MutexGuard<'_, ChatHashCache>`
note: required by a bound in `tokio::spawn`
   --> .cargo\registry\src\github.com-1ecc6299db9ec823\tokio-1.17.0\src\task\spawn.rs:127:21
    |
127 |         T: Future + Send + 'static,
    |                     ^^^^ required by this bound in `tokio::spawn`

error: could not compile `grammers_test` due to 2 previous errors

nrot avatar Mar 19 '22 17:03 nrot

I am having the same problem

x1qqq avatar Aug 08 '22 22:08 x1qqq

Try to clone the client before the spawn, and use the original value inside the spawn (as done in the examples):

@https://github.com/Lonami/grammers/blob/acb8c593a1b52c8f9ac5284ed4bbd7c96a00544e/lib/grammers-client/examples/dialogs.rs#L102-L103

Does that work?

Lonami avatar Aug 09 '22 08:08 Lonami

next_update's Future was not Send but https://github.com/Lonami/grammers/commit/4043c9618e29c14ef51857ed16c29a445cae09ac fixes this.

Lonami avatar Sep 28 '22 18:09 Lonami