implement Drop for client struct
This commit is contained in:
parent
b5870e62fe
commit
bffb41e8a1
11
src/lib.rs
11
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
|
// Create a struct which the client will use to communicate
|
||||||
let client_recv_queue_wrapper = format_ident!("__{}RecvQueueWrapper", name);
|
let client_recv_queue_wrapper = format_ident!("__{}RecvQueueWrapper", name);
|
||||||
let client_struct = quote! {
|
let client_struct = quote! {
|
||||||
#[derive(Clone)]
|
#[derive(::std::clone::Clone)]
|
||||||
struct #client_recv_queue_wrapper {
|
struct #client_recv_queue_wrapper {
|
||||||
recv_queue: ::std::sync::Arc<::tokio::sync::Mutex<::tokio::sync::mpsc::Receiver<(u64, #answer_enum_name)>>>,
|
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());
|
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))
|
Ok(Self::new(send_queue, recv_queue, ::std::option::Option::Some(::std::sync::Arc::new(connection_task)), ready_notify))
|
||||||
}
|
}
|
||||||
pub fn close(self) {
|
pub fn close(&mut self) {
|
||||||
if let ::std::option::Option::Some(task) = self.connection_task {
|
if let ::std::option::Option::Some(task) = self.connection_task.take() {
|
||||||
task.abort();
|
task.abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -707,6 +707,11 @@ fn derive_protocol(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream
|
|||||||
}
|
}
|
||||||
#(#client_impl)*
|
#(#client_impl)*
|
||||||
}
|
}
|
||||||
|
impl ::std::ops::Drop for #client_struct_name {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let expanded = quote! {
|
let expanded = quote! {
|
||||||
|
Loading…
Reference in New Issue
Block a user