Messages in this thread | | | Date | Wed, 03 Apr 2024 10:42:06 +0000 | From | Benno Lossin <> | Subject | Re: [PATCH 4/9] rust: list: add macro for implementing ListItem |
| |
On 02.04.24 14:17, Alice Ryhl wrote: > +/// Declares that this type has a `ListLinks<ID>` field at a fixed offset. > +/// > +/// This trait is only used to help implement `ListItem` safely. If `ListItem` is implemented > +/// manually, then this trait is not needed. > +/// > +/// # Safety > +/// > +/// All values of this type must have a `ListLinks<ID>` field at the given offset. > +pub unsafe trait HasListLinks<const ID: u64 = 0> { > + /// The offset of the `ListLinks` field. > + const OFFSET: usize; > + > + /// Returns a pointer to the [`ListLinks<T, ID>`] field. > + /// > + /// # Safety > + /// > + /// The provided pointer must point at a valid struct of type `Self`. > + /// > + /// [`ListLinks<T, ID>`]: ListLinks > + // We don't really need this method, but it's necessary for the implementation of > + // `impl_has_work!` to be correct. > + #[inline] > + unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut ListLinks<ID> { > + // SAFETY: The caller promises that the pointer is valid.
The caller only provides a valid pointer, this also needs the requirement from the trait that `OFFSET` is correct.
-- Cheers, Benno
> + unsafe { (ptr as *mut u8).add(Self::OFFSET) as *mut ListLinks<ID> } > + } > +}
| |