Commit 04e248d064
Unsigned
Layout: unified · split
src/connection.rs +16 −5
| @@ -99,13 +99,15 @@ pub enum ClientMessage { | |||
| 99 | pub struct Connection { | 99 | pub struct Connection { |
| 100 | #[allow(dead_code)] | 100 | #[allow(dead_code)] |
| 101 | receiver_task: Rc<Task<()>>, | 101 | receiver_task: Rc<Task<()>>, |
| 102 | client: Client, | 102 | client: Option<Client>, |
| 103 | pub runtime: Rc<Runtime>, | 103 | pub runtime: Rc<Runtime>, |
| 104 | } | 104 | } |
| 105 | 105 | ||
| 106 | impl Connection { | 106 | impl Connection { |
| 107 | pub fn client(&self) -> &Client { | 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 | pub async fn spawn<F>(&self, future: F) -> F::Output | 113 | pub async fn spawn<F>(&self, future: F) -> F::Output |
| @@ -141,7 +143,7 @@ impl Connection { | |||
| 141 | )); | 143 | )); |
| 142 | 144 | ||
| 143 | Self { | 145 | Self { |
| 144 | client: client.clone(), | 146 | client: Some(client.clone()), |
| 145 | runtime: runtime.into(), | 147 | runtime: runtime.into(), |
| 146 | receiver_task: receiver_task.into(), | 148 | receiver_task: receiver_task.into(), |
| 147 | } | 149 | } |
| @@ -180,7 +182,7 @@ impl Connection { | |||
| 180 | devices: Vec<OwnedDeviceId>, | 182 | devices: Vec<OwnedDeviceId>, |
| 181 | auth_info: Option<InteractiveAuthInfo>, | 183 | auth_info: Option<InteractiveAuthInfo>, |
| 182 | ) -> MatrixResult<DeleteDevicesResponse> { | 184 | ) -> MatrixResult<DeleteDevicesResponse> { |
| 183 | let client = self.client.clone(); | 185 | let client = self.client().clone(); |
| 184 | Ok(self | 186 | Ok(self |
| 185 | .spawn(async move { | 187 | .spawn(async move { |
| 186 | if let Some(info) = auth_info { | 188 | if let Some(info) = auth_info { |
| @@ -216,7 +218,7 @@ impl Connection { | |||
| 216 | 218 | ||
| 217 | /// Get the list of our own devices. | 219 | /// Get the list of our own devices. |
| 218 | pub async fn devices(&self) -> MatrixResult<DevicesResponse> { | 220 | pub async fn devices(&self) -> MatrixResult<DevicesResponse> { |
| 219 | let client = self.client.clone(); | 221 | let client = self.client().clone(); |
| 220 | Ok(self.spawn(async move { client.devices().await }).await?) | 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 | } | ||