pub unsafe trait AsLink { }
Expand description
A type that can be pointer-cast to and from a Link
.
Most types can avoid this trait and work instead with the Linked
wrapper type. However, if you want more control over the layout of your
type (i.e. are working with a DST), you may need to implement this trait
directly.
§Safety
Types implementing this trait must be marked #[repr(C)]
and have a Link
as their first field.
§Examples
use seize::{AsLink, Collector, Link};
#[repr(C)]
struct Bytes {
// safety invariant: Link must be the first field
link: Link,
values: [*mut u8; 0],
}
// Safety: Bytes is repr(C) and has Link as it's first field
unsafe impl AsLink for Bytes {}
// Deallocate an `Bytes`.
unsafe fn dealloc(ptr: *mut Bytes, collector: &Collector) {
collector.retire(ptr, |link| {
// safety `ptr` is of type *mut Bytes
let link: *mut Bytes = Link::cast(link);
// ..
});
}