Skip to content

Commit 838a163

Browse files
authored
Merge pull request #3 from Starry-Mix-THU/main
2 parents ac0a74b + 7ea8a5c commit 838a163

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

src/lib.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,42 @@ where
341341
/// assert_eq!(iter.next(), None);
342342
/// ```
343343
#[inline]
344-
pub fn ids(&self) -> Iter<CAP> {
344+
pub fn ids(&self) -> Iter<'_, CAP> {
345345
self.id_bitmap.into_iter()
346346
}
347347
}
348+
349+
impl<T, const CAP: usize> Default for FlattenObjects<T, CAP>
350+
where
351+
BitsImpl<{ CAP }>: Bits,
352+
{
353+
fn default() -> Self {
354+
Self::new()
355+
}
356+
}
357+
358+
impl<T: Clone, const CAP: usize> Clone for FlattenObjects<T, CAP>
359+
where
360+
BitsImpl<{ CAP }>: Bits,
361+
{
362+
fn clone(&self) -> Self {
363+
let mut cloned = Self::new();
364+
cloned.count = self.count;
365+
cloned.id_bitmap = self.id_bitmap;
366+
for id in &self.id_bitmap {
367+
cloned.objects[id].write(unsafe { self.objects[id].assume_init_ref() }.clone());
368+
}
369+
cloned
370+
}
371+
}
372+
373+
impl<T, const CAP: usize> Drop for FlattenObjects<T, CAP>
374+
where
375+
BitsImpl<{ CAP }>: Bits,
376+
{
377+
fn drop(&mut self) {
378+
for id in &self.id_bitmap {
379+
unsafe { self.objects[id].assume_init_drop() };
380+
}
381+
}
382+
}

tests/test_clone_and_drop.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use std::sync::Arc;
2+
3+
use flatten_objects::FlattenObjects;
4+
5+
mod sealed {
6+
use core::fmt;
7+
8+
const INNER: u8 = 0x1f;
9+
10+
pub struct Object(u8);
11+
12+
impl Default for Object {
13+
fn default() -> Self {
14+
Self(INNER)
15+
}
16+
}
17+
18+
impl fmt::Debug for Object {
19+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20+
write!(f, "Object")
21+
}
22+
}
23+
24+
impl Clone for Object {
25+
fn clone(&self) -> Self {
26+
assert_eq!(self.0, INNER, "Object::clone");
27+
Self(self.0)
28+
}
29+
}
30+
31+
impl Drop for Object {
32+
fn drop(&mut self) {
33+
assert_eq!(self.0, INNER, "Object::drop");
34+
}
35+
}
36+
}
37+
38+
pub use sealed::Object;
39+
40+
#[test]
41+
fn test_object() {
42+
let mut objects = FlattenObjects::<Object, 32>::new();
43+
44+
objects.add(Object::default()).unwrap();
45+
objects.add_at(10, Object::default()).unwrap();
46+
47+
let mut cloned = objects.clone();
48+
cloned.remove(0).unwrap();
49+
}
50+
51+
#[test]
52+
fn test_arc() {
53+
let src = Arc::new(());
54+
55+
let mut objects = FlattenObjects::<Arc<()>, 32>::new();
56+
57+
objects.add(src.clone()).unwrap();
58+
objects.add_at(10, src.clone()).unwrap();
59+
assert_eq!(Arc::strong_count(&src), 3);
60+
61+
let mut cloned = objects.clone();
62+
assert_eq!(Arc::strong_count(&src), 5);
63+
64+
cloned.remove(0).unwrap();
65+
assert_eq!(Arc::strong_count(&src), 4);
66+
67+
drop(cloned);
68+
assert_eq!(Arc::strong_count(&src), 3);
69+
70+
drop(objects);
71+
assert_eq!(Arc::strong_count(&src), 1);
72+
}

0 commit comments

Comments
 (0)