dioxus/packages/generational-box/tests/reference_counting.rs
Evan Almloff baf5a95cae
Fix cloned rsx double drop (#2839)
* Implement Rc generational pointers

* Clean up error handling a bit

* start adding tests

* Simplify a few types

* Separate Rc and normal generational boxes

* Add tests for rc drop and read

* Move over the pointer instead of the data in ReadOnlySignal props

* Fix panic on ref drop and release mode builds

* final rc signal cleanup

* use rc generational boxes for event handlers as well

* add a regression test for read only signal cloning

* merge main

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
2024-10-14 09:23:09 -07:00

61 lines
2.1 KiB
Rust

use generational_box::{Storage, SyncStorage, UnsyncStorage};
#[test]
fn reference_counting() {
fn reference_counting<S: Storage<String>>() {
let data = String::from("hello world");
let reference;
{
let outer_owner = S::owner();
{
// create an owner
let owner = S::owner();
// insert data into the store
let original = owner.insert_rc(data);
reference = outer_owner.insert_reference(original).unwrap();
// The reference should point to the value immediately
assert_eq!(&*reference.read(), "hello world");
// Original is dropped
}
// The reference should still point to the value
assert_eq!(&*reference.read(), "hello world");
}
// Now that all references are dropped, the value should be dropped
assert!(reference.try_read().is_err());
}
reference_counting::<UnsyncStorage>();
reference_counting::<SyncStorage>();
}
#[test]
fn move_reference_in_place() {
fn move_reference_in_place<S: Storage<String>>() {
let data1 = String::from("hello world");
let data2 = String::from("hello world 2");
// create an owner
let original_owner = S::owner();
// insert data into the store
let original = original_owner.insert_rc(data1.clone());
let mut reference = original_owner.insert_reference(original).unwrap();
// The reference should point to the original value
assert_eq!(&*reference.read(), &data1);
let new_owner = S::owner();
// Move the reference in place
let new = new_owner.insert_rc(data2.clone());
reference.point_to(new).unwrap();
// The reference should point to the new value
assert_eq!(&*reference.read(), &data2);
// make sure both got dropped
drop(original_owner);
drop(new_owner);
assert!(original.try_read().is_err());
assert!(new.try_read().is_err());
}
move_reference_in_place::<UnsyncStorage>();
move_reference_in_place::<SyncStorage>();
}