Skip to content

Commit

Permalink
Add test for drop order
Browse files Browse the repository at this point in the history
  • Loading branch information
steffahn committed Oct 4, 2021
1 parent 738dea5 commit 00f5f82
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/self_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use std::panic::catch_unwind;
use std::process::Command;
use std::rc::Rc;
use std::str;
use std::sync::Mutex;

use crossbeam_utils::thread;

use impls::impls;

use once_cell::sync::Lazy;
use once_cell::unsync::OnceCell;

use self_cell::self_cell;
Expand Down Expand Up @@ -692,3 +694,43 @@ fn try_build_manual(path: &str) {
fn invalid_compile_manual() {
try_build_manual("tests/invalid_manual/wrong_covariance");
}

#[test]
fn drop_order() {
#[derive(Debug, PartialEq, Eq)]
enum Dropped {
Owner,
Dependent,
}

struct Owner;
struct Dependent<'a>(&'a Owner);

static DROPS: Lazy<Mutex<Vec<Dropped>>> = Lazy::new(<_>::default);

impl Drop for Owner {
fn drop(&mut self) {
DROPS.lock().unwrap().push(Dropped::Owner)
}
}
impl Drop for Dependent<'_> {
fn drop(&mut self) {
DROPS.lock().unwrap().push(Dropped::Dependent)
}
}

self_cell! { struct Foo {
owner: Owner,
#[covariant]
dependent: Dependent,
}
}

let foo = Foo::new(Owner, |o| Dependent(o));
DROPS.lock().unwrap().clear();
drop(foo);
assert_eq!(
&DROPS.lock().unwrap()[..],
&[Dropped::Dependent, Dropped::Owner]
);
}

0 comments on commit 00f5f82

Please # to comment.