Created
August 4, 2018 00:43
-
-
Save kiranandcode/e116c732986f876bd61669fbb44067b6 to your computer and use it in GitHub Desktop.
A quick test demonstrating how the dependent view ensures that all items are still dropped as expected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] extern crate dependent_view; | |
use std::rc::{Weak, Rc}; | |
use dependent_view::rc::DependentRc; | |
trait Testable { | |
fn get_id(&self) -> usize; | |
fn get_i_id(&self) -> usize; | |
} | |
trait Describable { | |
fn describe(&self); | |
} | |
macro_rules! gen_test_struct { | |
($name:tt, $id: tt) => { | |
pub struct $name { | |
id: usize, | |
i_id: usize, | |
scope: &'static str | |
} | |
impl $name { | |
pub fn new(i_id: usize, scope: &'static str) -> Self { | |
$name { | |
i_id: i_id, | |
id: $id, | |
scope: scope | |
} | |
} | |
} | |
impl Drop for $name { | |
fn drop(&mut self) { | |
println!("[{:?}] - i_id: {:?} of scope {:?} dropped", self.id, self.i_id, self.scope); | |
} | |
} | |
impl Testable for $name { | |
fn get_id(&self) -> usize { | |
self.id | |
} | |
fn get_i_id(&self) -> usize { | |
self.i_id | |
} | |
} | |
impl Describable for $name { | |
fn describe(&self) { | |
println!("Test struct[{:?}] id {:?}", self.id, self.i_id); | |
} | |
} | |
} | |
} | |
gen_test_struct!(Test1, 1); | |
gen_test_struct!(Test2, 2); | |
gen_test_struct!(Test3, 3); | |
fn main() { | |
let mut testables : Vec<Weak<Testable>> = Vec::new(); | |
let mut describables : Vec<Weak<Describable>> = Vec::new(); | |
macro_rules! populate_test_views { | |
($id:tt, $t1:tt, $t2:tt, $t3:tt, $scope:expr) => { | |
let mut t1 = DependentRc::new(Test1::new($id, $scope)); | |
testables.push(to_view!(t1)); | |
$t1.push(t1); | |
let mut t2 = DependentRc::new(Test2::new($id, $scope)); | |
testables.push(to_view!(t2)); | |
$t2.push(t2); | |
let mut t3 = DependentRc::new(Test3::new($id, $scope)); | |
testables.push(to_view!(t3)); | |
$t3.push(t3); | |
} | |
} | |
macro_rules! populate_describe_views { | |
($id:tt, $t1:tt, $t2:tt, $t3:tt, $scope:expr) => { | |
let mut t1 = DependentRc::new(Test1::new($id, $scope)); | |
describables.push(to_view!(t1)); | |
$t1.push(t1); | |
let mut t2 = DependentRc::new(Test2::new($id, $scope)); | |
describables.push(to_view!(t2)); | |
$t2.push(t2); | |
let mut t3 = DependentRc::new(Test3::new($id, $scope)); | |
describables.push(to_view!(t3)); | |
$t3.push(t3); | |
} | |
} | |
macro_rules! test_views { | |
($scope: expr) => { | |
for testable in testables.iter() { | |
if let Some(testable) = testable.upgrade() { | |
println!("Testing at {}, [{:?}]({:?})", $scope, testable.get_id(), testable.get_i_id()); | |
} else { | |
//println!("Could not promote test view at scope {}", $scope); | |
} | |
} | |
for describable in describables.iter() { | |
if let Some(describable) = describable.upgrade() { | |
describable.describe(); | |
} else { | |
//println!("Could not promote describe view at scope {}", $scope); | |
} | |
} | |
} | |
} | |
println!("Starting 1st scope"); | |
{ | |
let mut t1s = Vec::new(); | |
let mut t2s = Vec::new(); | |
let mut t3s = Vec::new(); | |
for i in 0..10 { | |
populate_test_views!(i, t1s, t2s, t3s, "1st scope"); | |
populate_describe_views!(i, t1s, t2s, t3s, "1st scope"); | |
} | |
test_views!("1st scope"); | |
println!("Starting 1st > 2nd scope"); | |
{ | |
let mut t1s = Vec::new(); | |
let mut t2s = Vec::new(); | |
let mut t3s = Vec::new(); | |
for i in 10..20 { | |
populate_test_views!(i, t1s, t2s, t3s, "1st > 2nd scope"); | |
populate_describe_views!(i, t1s, t2s, t3s, "1st > 2nd scope"); | |
} | |
test_views!("1st > 2nd scope"); | |
println!("Ending 1st > 2nd scope"); | |
} | |
for i in 20..30 { | |
populate_test_views!(i, t1s, t2s, t3s, "1st scope"); | |
} | |
test_views!("1st scope"); | |
println!("Starting 1st > 3nd scope"); | |
{ | |
let mut t1s = Vec::new(); | |
let mut t2s = Vec::new(); | |
let mut t3s = Vec::new(); | |
for i in 30..40 { | |
populate_describe_views!(i, t1s, t2s, t3s, "1st > 3nd scope"); | |
} | |
test_views!("1st > 3nd scope"); | |
println!("Starting 1st > 3nd > 4th scope"); | |
{ | |
for i in 30..40 { | |
populate_describe_views!(i, t1s, t2s, t3s, "1st > 3nd > 4th scope"); | |
populate_test_views!(i, t1s, t2s, t3s, "1st > 3nd > 4th scope"); | |
} | |
test_views!("1st > 3nd > 4th scope"); | |
println!("Ending 1st > 3nd > 4th scope"); | |
} | |
println!("Ending 1st > 3nd scope"); | |
} | |
for i in 30..40 { | |
populate_describe_views!(i, t1s, t2s, t3s, "1st scope"); | |
populate_test_views!(i, t1s, t2s, t3s, "1st scope"); | |
} | |
test_views!("1st scope"); | |
println!("Ending 1st scope"); | |
} | |
test_views!("no scope"); | |
println!("Hello, world!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment