Hello. I am trying to develop a Matrix bot, and it uses event handler pattern to determine what must be done for a certain event. The library provides Client
, which does basic actions like sending message to a room, and I am making a wrapper class that does more specialised actions (such as detecting if a message is a bot command). I am trying to pass self
to this closure function so it can access functions I've made for the wrapper class, but I have no idea how to pass this whilst satisfying the lifetime check.
pub struct MatrixBot{
client: Client,
path: PathBuf,
config: Arc<Mutex<BotConfig>> //BotConfig is just a simple struct that contains config, as the name suggests
}
impl MatrixBot{
pub async fn new(path: PathBuf) -> MatrixBot { //Constructor
let client = loader::restore_login(&path).await;
let config = Arc::new(Mutex::new(loader::restore_config(&path)));
return MatrixBot{
client,
path,
config
};
}
pub async fn initialise(&self) -> () { //Actually starts the bot
let response = self.client.sync_once(SyncSettings::default()).await.expect("Syncing failed.");
self.client.add_event_handler(|event: OriginalSyncRoomMessageEvent, room: Room| async {
let debug = self.config.lock().expect("Cannot obtain lock due to poisioning.").debug; //Error occurs here
self.send_debug("Received message.");
});
let settings = SyncSettings::default().token(response.next_batch);
self.client.sync(settings).await.expect("Syncing failed.");
}
pub async fn send_debug(&self, message: &str) {...}
}
This is the following message:
error[E0521]: borrowed data escapes outside of associated function
--> src/matrix_bot/matrix_bot.rs:91:3
|
89 | pub async fn initialise(&self) -> () {
| -----
| |
| `self` is a reference that is only valid in the associated function body
| let's call the lifetime of this reference `'1`
90 | let response = self.client.sync_once(SyncSettings::default()).await.expect("Syncing failed.");
91 | / self.client.add_event_handler(|event: OriginalSyncRoomMessageEvent, room: Room| async {
92 | | let debug = self.config.lock().expect("Cannot obtain lock due to poisioning.").debug;
93 | |
94 | | });
| | ^
| | |
| |__________`self` escapes the associated function body here
| argument requires that `'1` must outlive `'static`
I assumed that if designed like this, self.client
and anything associated with it would never outlive self
itself, but it seems that is not the case. How would I be able to access self
in this particular situation?