diff options
Diffstat (limited to 'third_party/rust/maybe-uninit/tests')
-rw-r--r-- | third_party/rust/maybe-uninit/tests/doesnt_drop.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/third_party/rust/maybe-uninit/tests/doesnt_drop.rs b/third_party/rust/maybe-uninit/tests/doesnt_drop.rs new file mode 100644 index 0000000000..1d4b30fc46 --- /dev/null +++ b/third_party/rust/maybe-uninit/tests/doesnt_drop.rs @@ -0,0 +1,37 @@ +extern crate maybe_uninit; +use maybe_uninit::MaybeUninit; + +use std::cell::Cell; + +struct DecrementOnDrop<'a>(&'a Cell<usize>); + +impl<'a> DecrementOnDrop<'a> { + pub fn new(ref_:&'a Cell<usize>) -> Self { + ref_.set(1); + DecrementOnDrop(ref_) + } +} + +impl<'a> Clone for DecrementOnDrop<'a> { + fn clone(&self) -> Self { + self.0.set(self.0.get() + 1); + + DecrementOnDrop(self.0) + } +} + +impl<'a> Drop for DecrementOnDrop<'a>{ + fn drop(&mut self) { + self.0.set(self.0.get() - 1); + } +} + +#[test] +fn doesnt_drop(){ + let count = Cell::new(0); + let arc = DecrementOnDrop::new(&count); + let maybe = MaybeUninit::new(arc.clone()); + assert_eq!(count.get(), 2); + drop(maybe); + assert_eq!(count.get(), 2); +} |