| 1 | |
| 2 |
|
| 3 | |
| 4 |
|
| 5 | |
| 6 | |
| 7 | |
| 8 | pub trait Module: Sized + Sync + Send {
|
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | fn init(module: &'static ThisModule) -> crate::error::Result<Self>;
|
| 16 | }
|
| 17 |
|
| 18 | |
| 19 | pub trait InPlaceModule: Sync + Send {
|
| 20 | |
| 21 | |
| 22 | |
| 23 | fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, crate::error::Error>;
|
| 24 | }
|
| 25 |
|
| 26 | impl<T: Module> InPlaceModule for T {
|
| 27 | fn init(module: &'static ThisModule) -> impl pin_init::PinInit<Self, crate::error::Error> {
|
| 28 | let initer = move |slot: *mut Self| {
|
| 29 | let m = <Self as Module>::init(module)?;
|
| 30 |
|
| 31 | |
| 32 | unsafe { slot.write(m) };
|
| 33 | Ok(())
|
| 34 | };
|
| 35 |
|
| 36 | |
| 37 | unsafe { pin_init::pin_init_from_closure(initer) }
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | |
| 42 | pub trait ModuleMetadata {
|
| 43 | |
| 44 | const NAME: &'static crate::str::CStr;
|
| 45 |
|
| 46 | |
| 47 | const THIS_MODULE: ThisModule;
|
| 48 | }
|
| 49 |
|
| 50 | |
| 51 | #[inline]
|
| 52 | pub const fn this_module<M: ModuleMetadata>() -> &'static ThisModule {
|
| 53 | &M::THIS_MODULE
|
| 54 | }
|
| 55 |
|
| 56 | |
| 57 | |
| 58 | |
| 59 | pub struct ThisModule(*mut crate::bindings::module);
|
| 60 |
|
| 61 | |
| 62 | unsafe impl Sync for ThisModule {}
|
| 63 |
|
| 64 | impl ThisModule {
|
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | pub const unsafe fn from_ptr(ptr: *mut crate::bindings::module) -> ThisModule {
|
| 71 | ThisModule(ptr)
|
| 72 | }
|
| 73 |
|
| 74 | |
| 75 | |
| 76 | |
| 77 | pub const fn as_ptr(&self) -> *mut crate::bindings::module {
|
| 78 | self.0
|
| 79 | }
|
| 80 | }
|