fdf_component/macros.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! Implementation of the [`driver_register`] macro for registering driver implementations
//! with the driver host.
use crate::server::DriverServer;
use crate::Driver;
/// These re-exports are for the use of the macro and so should not be surfaced in documentation.
#[doc(hidden)]
pub use fdf_sys::{
fdf_dispatcher_get_current_dispatcher, DriverRegistration,
DriverRegistration_driver_registration_v1, DRIVER_REGISTRATION_VERSION_1,
};
/// Called by the macro [`driver_register`] to create the driver registration struct
/// without exposing any of the internal implementation as a public API.
#[doc(hidden)]
pub const fn make_driver_registration<T: Driver>() -> DriverRegistration {
DriverRegistration {
version: DRIVER_REGISTRATION_VERSION_1 as u64,
v1: DriverRegistration_driver_registration_v1 {
initialize: Some(DriverServer::<T>::initialize),
destroy: Some(DriverServer::<T>::destroy),
},
}
}
/// Macro for declaring a driver's implementation of the [`Driver`] trait.
///
/// # Example
///
/// ```
/// use fdf_server::{driver_register, Driver, DriverContext};
/// use tracing::info;
/// use zx::Status;
///
/// #[derive(Default)]
/// struct TestDriver;
///
/// impl Driver for TestDriver {
/// async fn start(context: DriverContext) -> Result<Self, Status> {
/// info!("driver starting!");
/// // implement binding the node client, creating children, etc. here.
/// Ok(Self)
/// }
/// async fn stop(&self) {
/// info!("driver stop message");
/// }
/// }
///
/// driver_register!(TestDriver);
/// ```
#[macro_export]
macro_rules! driver_register {
($ty:ty) => {
#[no_mangle]
#[link_section = ".driver_registration"]
#[allow(non_upper_case_globals)]
pub static __fuchsia_driver_registration__: $crate::macros::DriverRegistration =
$crate::macros::make_driver_registration::<$ty>();
};
}