diff --git a/src/lib.rs b/src/lib.rs index b898e08..9cc75e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -618,7 +618,7 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream // Create a struct which the client will use to communicate let client_recv_queue_wrapper = format_ident!("__{}RecvQueueWrapper", name); let client_struct = quote! { - #[derive(Clone)] + #[derive(::std::clone::Clone)] struct #client_recv_queue_wrapper { recv_queue: ::std::sync::Arc<::tokio::sync::Mutex<::tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>>>, } @@ -665,8 +665,8 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream let connection_task = ::tokio::spawn(connection.run()); Ok(Self::new(send_queue, recv_queue, ::std::option::Option::Some(::std::sync::Arc::new(connection_task)), ready_notify)) } - pub fn close(self) { - if let ::std::option::Option::Some(task) = self.connection_task { + pub fn close(&mut self) { + if let ::std::option::Option::Some(task) = self.connection_task.take() { task.abort(); } } @@ -707,6 +707,11 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream } #(#client_impl)* } + impl ::std::ops::Drop for #client_struct_name { + fn drop(&mut self) { + self.close(); + } + } }; let expanded = quote! {