Skip to content

Commit

Permalink
fix(dropper): fix issue with atomic & weak refs
Browse files Browse the repository at this point in the history
Signed-off-by: vados <[email protected]>
  • Loading branch information
t3hmrman committed Sep 21, 2024
1 parent ec6e5bb commit c012f8b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ proc-macro2 = "1.0.69"
quote = "1.0.33"
syn = "2.0.38"
tokio = "1.33.0"
uuid = "1.10.0"
2 changes: 0 additions & 2 deletions crates/async-dropper-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ fn gen_impl(DeriveInput { ident, .. }: &DeriveInput) -> proc_macro2::TokenStream
let shared_default_name = make_shared_default_name(ident);
quote::quote!(
#[automatically_derived]
#[async_trait]
impl Drop for #ident {
fn drop(&mut self) {
// We consider a self that is completely equivalent to it's default version to be dropped
Expand Down Expand Up @@ -225,7 +224,6 @@ fn gen_impl(DeriveInput { ident, .. }: &DeriveInput) -> proc_macro2::TokenStream
let shared_default_name = make_shared_default_name(ident);
quote::quote!(
#[automatically_derived]
#[async_trait]
impl Drop for #ident {
fn drop(&mut self) {
// We consider a self that is completely equivalent to it's default version to be dropped
Expand Down
2 changes: 2 additions & 0 deletions crates/async-dropper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ async-dropper-derive = { path = "../async-dropper-derive", version = "0.3.2" }
async-dropper-simple = { path = "../async-dropper-simple", version = "0.2.7" }

[dev-dependencies]
anyhow = { workspace = true }
uuid = { workspace = true }
tokio = { workspace = true, features = [
"time",
"macros",
Expand Down
2 changes: 1 addition & 1 deletion crates/async-dropper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub trait ResetDefault {
///
/// Normally, implementing only `async_drop(&mut self)` is necessary.
///
/// An implementation of `reset(&mut self)` is generated, and can be overriden where necessary,
/// An implementation of `reset(&mut self)` is generated, and can be overriden where necessary,
/// with the primary requirement that `reset` returns the `T` being reset to a state where it is *identical*
/// to a `T::default()`.
#[async_trait::async_trait]
Expand Down
40 changes: 40 additions & 0 deletions crates/async-dropper/tests/gh_issue_21.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::sync::atomic::AtomicBool;
use std::sync::Weak;

use anyhow::Result;
use async_trait::async_trait;
use uuid::Uuid;

use async_dropper::{AsyncDrop, AsyncDropError};

/// Ensure that async-dropper works with atomic structures and weak refs
///
/// see: https://github.com/t3hmrman/async-dropper/issues/21
#[tokio::test]
async fn gh_issue_21() -> Result<()> {
#[derive(Default)]
pub struct State(String);

#[derive(Default, PartialEq, Eq, AsyncDrop)]
pub struct Supervisor {
pub socket_bot_id: Uuid,
pub room_id: String,
pub private_room: AtomicBool,
pub state: Weak<State>,
}

#[async_trait]
impl AsyncDrop for Supervisor {
async fn async_drop(&mut self) -> Result<(), AsyncDropError> {
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
Ok(())
}
}

let mut supervisor = Supervisor::default();
supervisor.room_id = "test".into();

drop(supervisor);

Ok(())
}

0 comments on commit c012f8b

Please sign in to comment.