Commit 04e248d064
Unsigned
Layout: unified · split
src/connection.rs +16 −5
| @@ -99,13 +99,15 @@ pub enum ClientMessage { | ||
| 99 | 99 | pub struct Connection { |
| 100 | 100 | #[allow(dead_code)] |
| 101 | 101 | receiver_task: Rc<Task<()>>, |
| 102 | client: Client, | |
| 102 | client: Option<Client>, | |
| 103 | 103 | pub runtime: Rc<Runtime>, |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | impl Connection { |
| 107 | 107 | pub fn client(&self) -> &Client { |
| 108 | &self.client | |
| 108 | self.client | |
| 109 | .as_ref() | |
| 110 | .expect("Connection client was already dropped") | |
| 109 | 111 | } |
| 110 | 112 | |
| 111 | 113 | pub async fn spawn<F>(&self, future: F) -> F::Output |
| @@ -141,7 +143,7 @@ impl Connection { | ||
| 141 | 143 | )); |
| 142 | 144 | |
| 143 | 145 | Self { |
| 144 | client: client.clone(), | |
| 146 | client: Some(client.clone()), | |
| 145 | 147 | runtime: runtime.into(), |
| 146 | 148 | receiver_task: receiver_task.into(), |
| 147 | 149 | } |
| @@ -180,7 +182,7 @@ impl Connection { | ||
| 180 | 182 | devices: Vec<OwnedDeviceId>, |
| 181 | 183 | auth_info: Option<InteractiveAuthInfo>, |
| 182 | 184 | ) -> MatrixResult<DeleteDevicesResponse> { |
| 183 | let client = self.client.clone(); | |
| 185 | let client = self.client().clone(); | |
| 184 | 186 | Ok(self |
| 185 | 187 | .spawn(async move { |
| 186 | 188 | if let Some(info) = auth_info { |
| @@ -216,7 +218,7 @@ impl Connection { | ||
| 216 | 218 | |
| 217 | 219 | /// Get the list of our own devices. |
| 218 | 220 | pub async fn devices(&self) -> MatrixResult<DevicesResponse> { |
| 219 | let client = self.client.clone(); | |
| 221 | let client = self.client().clone(); | |
| 220 | 222 | Ok(self.spawn(async move { client.devices().await }).await?) |
| 221 | 223 | } |
| 222 | 224 | |
| @@ -660,3 +662,12 @@ impl Connection { | ||
| 660 | 662 | } |
| 661 | 663 | } |
| 662 | 664 | } |
| 665 | ||
| 666 | impl Drop for Connection { | |
| 667 | fn drop(&mut self) { | |
| 668 | if let Some(client) = self.client.take() { | |
| 669 | let _guard = self.runtime.enter(); | |
| 670 | drop(client); | |
| 671 | } | |
| 672 | } | |
| 673 | } | |