clack_host/entry/entry_provider.rs
1use clack_common::entry::EntryDescriptor;
2use std::ptr::NonNull;
3use std::sync::Arc;
4
5/// A type which can provide an entry descriptor pointer for Clack hosts to load.
6///
7/// # Safety
8///
9/// Implementors *must* ensure the [`EntryDescriptor`] returned by the [`entry_pointer`](Self::entry_pointer) method
10/// remains valid for the entire lifetime of this type. That means all the function pointers it contains
11/// must remain valid to call.
12///
13/// Moreover, the returned [`EntryDescriptor`] must have a stable address. It must not move for the
14/// lifetime of this type.
15pub unsafe trait EntryProvider: Send + Sync + 'static {
16 /// Returns a pointer to the [`EntryDescriptor`] this provides.
17 ///
18 /// This pointer always valid for reads, as long as this type is alive.
19 /// This pointer is also stable, i.e. its address will never change for the entire duration of
20 /// this type's lifetime.
21 fn entry_pointer(&self) -> NonNull<EntryDescriptor>;
22}
23
24// SAFETY: The pointer is always valid since it comes from a 'static reference
25unsafe impl EntryProvider for &'static EntryDescriptor {
26 #[inline]
27 fn entry_pointer(&self) -> NonNull<EntryDescriptor> {
28 (*self).into()
29 }
30}
31
32// SAFETY: The pointer comes from this Arc, it is always valid as long as the Arc is alive
33unsafe impl EntryProvider for Arc<EntryDescriptor> {
34 #[inline]
35 fn entry_pointer(&self) -> NonNull<EntryDescriptor> {
36 self.as_ref().into()
37 }
38}
39
40// SAFETY: The pointer comes from this Box, it is always valid as long as the Box is alive
41unsafe impl EntryProvider for Box<EntryDescriptor> {
42 #[inline]
43 fn entry_pointer(&self) -> NonNull<EntryDescriptor> {
44 self.as_ref().into()
45 }
46}