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 543190b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/self_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#![deny(private_in_public)]

use std::cell::RefCell;
use std::fmt::Debug;
use std::fs;
use std::marker::PhantomData;
Expand Down Expand Up @@ -692,3 +693,38 @@ 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(Rc<RefCell<Vec<Dropped>>>);
struct Dependent<'a>(&'a Owner, Rc<RefCell<Vec<Dropped>>>);

impl Drop for Owner {
fn drop(&mut self) {
self.0.borrow_mut().push(Dropped::Owner)
}
}
impl Drop for Dependent<'_> {
fn drop(&mut self) {
self.1.borrow_mut().push(Dropped::Dependent)
}
}

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

let drops: Rc<RefCell<Vec<Dropped>>> = <_>::default();
let foo = Foo::new(Owner(drops.clone()), |o| Dependent(o, drops.clone()));
drop(foo);
assert_eq!(&drops.borrow()[..], &[Dropped::Dependent, Dropped::Owner]);
}

0 comments on commit 543190b

Please sign in to comment.