|
| 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