//! Item handling. use crate::{MovableRef, Ref}; /// Argument to [`crate::TypedSection::offset_of`] and related section lookup APIs. pub trait SectionItemLocation { /// Address of the item's storage in the link section (not the wrapper). fn item_ptr(&self) -> *const T; } impl SectionItemLocation for &T { fn item_ptr(&self) -> *const T { *self as *const T } } impl SectionItemLocation for &Ref { fn item_ptr(&self) -> *const T { Ref::as_ptr(self) } } impl SectionItemLocation for &MovableRef { fn item_ptr(&self) -> *const T { MovableRef::as_ptr(self) } } /// Element type for this section handle ([`crate::TypedSection`], etc.). pub trait SectionItemType { /// Item type stored or referenced in the section. type Item; } // Waiting on Rust 1.78 // #[diagnostic::on_unimplemented(message = "Incorrect section type for item")] /// Typed section compatibility for item `T`. pub trait SectionItemTyped { /// Item representation for this `T`. type Item; } #[cfg(test)] mod tests { use crate::item::SectionItemType; use core::marker::PhantomData; assert_type_eq!( as SectionItemType>::Item, u32); assert_type_eq!( as SectionItemType>::Item, &'static u32 ); macro_rules! assert_type_eq { ($lhs:ty, $rhs:ty) => { const _: () = { struct __AssertTypeEq(PhantomData, PhantomData); trait __AssertTypeEqT { const CHECK: bool = true; } impl __AssertTypeEqT for __AssertTypeEq {} _ = <__AssertTypeEq<$lhs, $rhs> as __AssertTypeEqT>::CHECK; }; }; } pub(crate) use assert_type_eq; }