Re: [PATCH 8/9] rust: list: support heterogeneous lists
From: Benno Lossin
Date: Thu Apr 04 2024 - 11:36:12 EST
On 02.04.24 14:17, Alice Ryhl wrote:
> @@ -180,6 +184,41 @@ unsafe fn from_fields(me: *mut ListLinksFields) -> *mut Self {
> }
> }
>
> +/// Similar to [`ListLinks`], but also contains a pointer to the full value.
> +///
> +/// This type can be used instead of [`ListLinks`] to support lists with trait objects.
> +#[repr(C)]
> +pub struct ListLinksSelfPtr<T: ?Sized, const ID: u64 = 0> {
> + /// The `ListLinks` field inside this value.
> + ///
> + /// This is public so that it can be used with `impl_has_list_links!`.
> + pub inner: ListLinks<ID>,
> + self_ptr: UnsafeCell<MaybeUninit<*const T>>,
> +}
> +
> +unsafe impl<T: ?Sized + Send, const ID: u64> Send for ListLinksSelfPtr<T, ID> {}
> +unsafe impl<T: ?Sized + Sync, const ID: u64> Sync for ListLinksSelfPtr<T, ID> {}
Missing SAFETY comments.
> +
> +impl<T: ?Sized, const ID: u64> ListLinksSelfPtr<T, ID> {
> + /// The offset from the [`ListLinks`] to the self pointer field.
> + pub const LIST_LINKS_SELF_PTR_OFFSET: usize = core::mem::offset_of!(Self, self_ptr);
> +
> + /// Creates a new initializer for this type.
> + pub fn new() -> impl PinInit<Self> {
> + // INVARIANT: Pin-init initializers can't be used on an existing `Arc`, so this value will
> + // not be constructed in an `Arc` that already has a `ListArc`.
> + Self {
> + inner: ListLinks {
> + inner: Opaque::new(ListLinksFields {
> + prev: ptr::null_mut(),
> + next: ptr::null_mut(),
> + }),
> + },
Why don't you use `inner <- ListLinks::new(),`?
> + self_ptr: UnsafeCell::new(MaybeUninit::zeroed()),
> + }
> + }
> +}
> +
> impl<T: ?Sized + ListItem<ID>, const ID: u64> List<T, ID> {
> /// Creates a new empty list.
> pub const fn new() -> Self {
[...]
> @@ -94,5 +137,45 @@ unsafe fn post_remove(me: *mut ListLinks<$num>) -> *const Self {
> }
> }
> };
> +
> + (
> + impl$({$($generics:tt)*})? ListItem<$num:tt> for $t:ty {
> + using ListLinksSelfPtr;
> + } $($rest:tt)*
> + ) => {
> + unsafe impl$(<$($generics)*>)? ListItem<$num> for $t {
> + unsafe fn prepare_to_insert(me: *const Self) -> *mut ListLinks<$num> {
> + let links_field = unsafe { Self::view_links(me) };
> +
> + let spoff = ListLinksSelfPtr::<Self, $num>::LIST_LINKS_SELF_PTR_OFFSET;
> + let self_ptr = unsafe { (links_field as *const u8).add(spoff)
> + as *const ::core::cell::UnsafeCell<*const Self> };
> + let cell_inner = ::core::cell::UnsafeCell::raw_get(self_ptr);
> +
> + unsafe { ::core::ptr::write(cell_inner, me) };
> + links_field
> + }
> +
> + unsafe fn view_links(me: *const Self) -> *mut ListLinks<$num> {
> + unsafe {
> + <Self as HasListLinks<$num>>::raw_get_list_links(me.cast_mut())
> + }
> + }
> +
> + unsafe fn view_value(links_field: *mut ListLinks<$num>) -> *const Self {
> + let spoff = ListLinksSelfPtr::<Self, $num>::LIST_LINKS_SELF_PTR_OFFSET;
> + let self_ptr = unsafe { (links_field as *const u8).add(spoff)
> + as *const ::core::cell::UnsafeCell<*const Self> };
> + let cell_inner = ::core::cell::UnsafeCell::raw_get(self_ptr);
> + unsafe {
> + ::core::ptr::read(cell_inner)
> + }
> + }
> +
> + unsafe fn post_remove(me: *mut ListLinks<$num>) -> *const Self {
> + unsafe { Self::view_value(me) }
> + }
> + }
> + };
The paths in this macro should use `$crate::...` to prevent import
errors.
--
Cheers,
Benno