From 066d76cc8e419073f8a51695fe8c2e421560dc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 31 Oct 2019 09:17:13 +0100 Subject: [PATCH] nio: Make the callback futures Sync/Send. --- examples/login.rs | 9 ++++----- src/async_client.rs | 32 ++++++++++++++++---------------- src/base_client.rs | 13 ++++++------- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/examples/login.rs b/examples/login.rs index 31a35d13..91b4e71e 100644 --- a/examples/login.rs +++ b/examples/login.rs @@ -3,8 +3,8 @@ use std::{env, process::exit}; use std::pin::Pin; use std::future::Future; +use std::sync::{Arc, Mutex}; use std::rc::Rc; -use std::cell::RefCell; use matrix_nio::{ self, @@ -16,8 +16,8 @@ use matrix_nio::{ AsyncClient, AsyncClientConfig, SyncSettings, Room }; -async fn async_helper(room: Rc>, event: Rc) { - let room = room.borrow(); +async fn async_helper(room: Arc>, event: Arc) { + let room = room.lock().unwrap(); if let RoomEvent::RoomMessage(MessageEvent { content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), sender, @@ -27,10 +27,9 @@ async fn async_helper(room: Rc>, event: Rc) { let user = room.members.get(&sender.to_string()).unwrap(); println!("{}: {}", user.display_name.as_ref().unwrap_or(&sender.to_string()), msg_body); } - } -fn async_callback(room: Rc>, event: Rc) -> Pin>> { +fn async_callback(room: Arc>, event: Arc) -> Pin + Send + Sync >> { Box::pin(async_helper(room, event)) } diff --git a/src/async_client.rs b/src/async_client.rs index 601ff020..96b50123 100644 --- a/src/async_client.rs +++ b/src/async_client.rs @@ -2,7 +2,7 @@ use std::convert::{TryFrom, TryInto}; use std::future::Future; use std::pin::Pin; use std::rc::Rc; -use std::cell::RefCell; +use std::sync::{Arc, Mutex}; use http::Method as HttpMethod; use http::Response as HttpResponse; @@ -22,7 +22,7 @@ use crate::error::{Error, InnerError}; use crate::session::Session; type RoomEventCallback = Box::; -type RoomEventCallbackF = Box::>, Rc) -> Pin>>>; +type RoomEventCallbackF = Box::>, Arc) -> Pin + Send + Sync>> + Send + Sync>; pub struct AsyncClient { /// The URL of the homeserver to connect to. @@ -31,8 +31,8 @@ pub struct AsyncClient { http_client: reqwest::Client, /// User session data. base_client: BaseClient, - /// Event callbacks - event_callbacks: Vec, + // /// Event callbacks + // event_callbacks: Vec, /// Event futures event_futures: Vec, } @@ -166,18 +166,18 @@ impl AsyncClient { homeserver, http_client, base_client: BaseClient::new(session), - event_callbacks: Vec::new(), + // event_callbacks: Vec::new(), event_futures: Vec::new(), }) } - pub fn add_event_callback( - &mut self, - event_type: EventType, - callback: RoomEventCallback, - ) { - self.event_callbacks.push(callback); - } + // pub fn add_event_callback( + // &mut self, + // event_type: EventType, + // callback: RoomEventCallback, + // ) { + // self.event_callbacks.push(callback); + // } pub fn add_event_future( &mut self, @@ -245,12 +245,12 @@ impl AsyncClient { let room = self.base_client.joined_rooms.get(&room_id).unwrap(); - for mut cb in &mut self.event_callbacks { - cb(&room.borrow(), &event); - } + // for mut cb in &mut self.event_callbacks { + // cb(&room.lock().unwrap(), &event); + // } for mut cb in &mut self.event_futures { - cb(room.clone(), Rc::new(event.clone())).await; + cb(room.clone(), Arc::new(event.clone())).await; } } } diff --git a/src/base_client.rs b/src/base_client.rs index cb6c9d66..9b7be64d 100644 --- a/src/base_client.rs +++ b/src/base_client.rs @@ -4,8 +4,7 @@ use crate::api::r0 as api; use crate::events::collections::all::{RoomEvent, StateEvent}; use crate::events::room::member::{MemberEvent, MembershipState}; use crate::session::Session; -use std::rc::Rc; -use std::cell::RefCell; +use std::sync::{Arc, Mutex}; pub type Token = String; pub type RoomId = String; @@ -150,7 +149,7 @@ pub struct Client { /// The current sync token that should be used for the next sync call. pub sync_token: Option, /// A map of the rooms our user is joined in. - pub joined_rooms: HashMap>>, + pub joined_rooms: HashMap>>, } impl Client { @@ -186,11 +185,11 @@ impl Client { self.session = Some(session); } - fn get_or_create_room(&mut self, room_id: &RoomId) -> &mut Rc> { + fn get_or_create_room(&mut self, room_id: &RoomId) -> &mut Arc> { self.joined_rooms .entry(room_id.to_string()) .or_insert( - Rc::new(RefCell::new(Room::new( + Arc::new(Mutex::new(Room::new( room_id, &self .session @@ -210,7 +209,7 @@ impl Client { /// Returns true if the membership list of the room changed, false /// otherwise. pub fn receive_joined_timeline_event(&mut self, room_id: &RoomId, event: &RoomEvent) -> bool { - let mut room = self.get_or_create_room(room_id).borrow_mut(); + let mut room = self.get_or_create_room(room_id).lock().unwrap(); room.receive_timeline_event(event) } @@ -223,7 +222,7 @@ impl Client { /// Returns true if the membership list of the room changed, false /// otherwise. pub fn receive_joined_state_event(&mut self, room_id: &RoomId, event: &StateEvent) -> bool { - let mut room = self.get_or_create_room(room_id).borrow_mut(); + let mut room = self.get_or_create_room(room_id).lock().unwrap(); room.receive_state_event(event) } }