| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 |
|
| 12 | use core::cell::UnsafeCell;
|
| 13 |
|
| 14 | |
| 15 | |
| 16 | |
| 17 | pub struct ListArcField<T, const ID: u64 = 0> {
|
| 18 | value: UnsafeCell<T>,
|
| 19 | }
|
| 20 |
|
| 21 | |
| 22 | unsafe impl<T: Send + Sync, const ID: u64> Send for ListArcField<T, ID> {}
|
| 23 | |
| 24 | unsafe impl<T: Send + Sync, const ID: u64> Sync for ListArcField<T, ID> {}
|
| 25 |
|
| 26 | impl<T, const ID: u64> ListArcField<T, ID> {
|
| 27 | |
| 28 | pub fn new(value: T) -> Self {
|
| 29 | Self {
|
| 30 | value: UnsafeCell::new(value),
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | |
| 35 | |
| 36 | |
| 37 | pub fn get_mut(&mut self) -> &mut T {
|
| 38 | self.value.get_mut()
|
| 39 | }
|
| 40 |
|
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | pub unsafe fn assert_ref(&self) -> &T {
|
| 48 | |
| 49 | |
| 50 | unsafe { &*self.value.get() }
|
| 51 | }
|
| 52 |
|
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | #[expect(clippy::mut_from_ref)]
|
| 60 | pub unsafe fn assert_mut(&self) -> &mut T {
|
| 61 | |
| 62 | |
| 63 | unsafe { &mut *self.value.get() }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | |
| 68 | #[macro_export]
|
| 69 | #[doc(hidden)]
|
| 70 | macro_rules! define_list_arc_field_getter {
|
| 71 | ($pub:vis fn $name:ident(&self $(<$id:tt>)?) -> &$typ:ty { $field:ident }
|
| 72 | $($rest:tt)*
|
| 73 | ) => {
|
| 74 | $pub fn $name<'a>(self: &'a $crate::list::ListArc<Self $(, $id)?>) -> &'a $typ {
|
| 75 | let field = &(&**self).$field;
|
| 76 | |
| 77 | unsafe { $crate::list::ListArcField::<$typ $(, $id)?>::assert_ref(field) }
|
| 78 | }
|
| 79 |
|
| 80 | $crate::list::define_list_arc_field_getter!($($rest)*);
|
| 81 | };
|
| 82 |
|
| 83 | ($pub:vis fn $name:ident(&mut self $(<$id:tt>)?) -> &mut $typ:ty { $field:ident }
|
| 84 | $($rest:tt)*
|
| 85 | ) => {
|
| 86 | $pub fn $name<'a>(self: &'a mut $crate::list::ListArc<Self $(, $id)?>) -> &'a mut $typ {
|
| 87 | let field = &(&**self).$field;
|
| 88 | |
| 89 | unsafe { $crate::list::ListArcField::<$typ $(, $id)?>::assert_mut(field) }
|
| 90 | }
|
| 91 |
|
| 92 | $crate::list::define_list_arc_field_getter!($($rest)*);
|
| 93 | };
|
| 94 |
|
| 95 | () => {};
|
| 96 | }
|
| 97 | pub use define_list_arc_field_getter;
|