Move const_drop_in_place test from ptr to manually_drop module

This commit is contained in:
ltdk
2025-10-19 02:44:41 -04:00
parent 97b7170eef
commit 31a2c565bd
2 changed files with 39 additions and 39 deletions
+38
View File
@@ -27,3 +27,41 @@ fn drop(&mut self) {
drop(x);
drop(y);
}
#[test]
fn const_drop_in_place() {
const COUNTER: usize = {
use core::cell::Cell;
let counter = Cell::new(0);
// only exists to make `Drop` indirect impl
#[allow(dead_code)]
struct Test<'a>(Dropped<'a>);
struct Dropped<'a>(&'a Cell<usize>);
impl const Drop for Dropped<'_> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
let mut one = ManuallyDrop::new(Test(Dropped(&counter)));
let mut two = ManuallyDrop::new(Test(Dropped(&counter)));
let mut three = ManuallyDrop::new(Test(Dropped(&counter)));
assert!(counter.get() == 0);
unsafe {
ManuallyDrop::drop(&mut one);
}
assert!(counter.get() == 1);
unsafe {
ManuallyDrop::drop(&mut two);
}
assert!(counter.get() == 2);
unsafe {
ManuallyDrop::drop(&mut three);
}
counter.get()
};
assert_eq!(COUNTER, 3);
}
+1 -39
View File
@@ -1,6 +1,6 @@
use core::cell::RefCell;
use core::marker::Freeze;
use core::mem::{ManuallyDrop, MaybeUninit};
use core::mem::MaybeUninit;
use core::num::NonZero;
use core::ptr;
use core::ptr::*;
@@ -1045,41 +1045,3 @@ struct PtrMutDefaultTest {
let default = PtrMutDefaultTest::default();
assert!(default.ptr.is_null());
}
#[test]
fn test_const_drop_in_place() {
const COUNTER: usize = {
use core::cell::Cell;
let counter = Cell::new(0);
// only exists to make `Drop` indirect impl
#[allow(dead_code)]
struct Test<'a>(Dropped<'a>);
struct Dropped<'a>(&'a Cell<usize>);
impl const Drop for Dropped<'_> {
fn drop(&mut self) {
self.0.set(self.0.get() + 1);
}
}
let mut one = ManuallyDrop::new(Test(Dropped(&counter)));
let mut two = ManuallyDrop::new(Test(Dropped(&counter)));
let mut three = ManuallyDrop::new(Test(Dropped(&counter)));
assert!(counter.get() == 0);
unsafe {
ManuallyDrop::drop(&mut one);
}
assert!(counter.get() == 1);
unsafe {
ManuallyDrop::drop(&mut two);
}
assert!(counter.get() == 2);
unsafe {
ManuallyDrop::drop(&mut three);
}
counter.get()
};
assert_eq!(COUNTER, 3);
}