diff options
Diffstat (limited to 'tests/ui/resource-destruct.rs')
-rw-r--r-- | tests/ui/resource-destruct.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/ui/resource-destruct.rs b/tests/ui/resource-destruct.rs new file mode 100644 index 000000000..c4756a21a --- /dev/null +++ b/tests/ui/resource-destruct.rs @@ -0,0 +1,31 @@ +// run-pass + +#![allow(non_camel_case_types)] +use std::cell::Cell; + +struct shrinky_pointer<'a> { + i: &'a Cell<isize>, +} + +impl<'a> Drop for shrinky_pointer<'a> { + fn drop(&mut self) { + println!("Hello!"); self.i.set(self.i.get() - 1); + } +} + +impl<'a> shrinky_pointer<'a> { + pub fn look_at(&self) -> isize { return self.i.get(); } +} + +fn shrinky_pointer(i: &Cell<isize>) -> shrinky_pointer { + shrinky_pointer { + i: i + } +} + +pub fn main() { + let my_total = &Cell::new(10); + { let pt = shrinky_pointer(my_total); assert_eq!(pt.look_at(), 10); } + println!("my_total = {}", my_total.get()); + assert_eq!(my_total.get(), 9); +} |