191e9d4abSLoGin #![no_std] 2840045afSLoGin #![allow(clippy::needless_return)] 391e9d4abSLoGin 491e9d4abSLoGin use system_error::SystemError; 591e9d4abSLoGin pub use unified_init_macros as macros; 691e9d4abSLoGin 791e9d4abSLoGin /// 统一初始化器 891e9d4abSLoGin #[derive(Debug)] 991e9d4abSLoGin pub struct UnifiedInitializer { 1091e9d4abSLoGin function: &'static UnifiedInitFunction, 1191e9d4abSLoGin name: &'static str, 1291e9d4abSLoGin } 1391e9d4abSLoGin 1491e9d4abSLoGin impl UnifiedInitializer { new( name: &'static str, function: &'static UnifiedInitFunction, ) -> UnifiedInitializer1591e9d4abSLoGin pub const fn new( 1691e9d4abSLoGin name: &'static str, 1791e9d4abSLoGin function: &'static UnifiedInitFunction, 1891e9d4abSLoGin ) -> UnifiedInitializer { 1991e9d4abSLoGin UnifiedInitializer { function, name } 2091e9d4abSLoGin } 2191e9d4abSLoGin 2291e9d4abSLoGin /// 调用初始化函数 call(&self) -> Result<(), SystemError>2391e9d4abSLoGin pub fn call(&self) -> Result<(), SystemError> { 2491e9d4abSLoGin (self.function)() 2591e9d4abSLoGin } 2691e9d4abSLoGin 2791e9d4abSLoGin /// 获取初始化函数的名称 name(&self) -> &'static str2891e9d4abSLoGin pub const fn name(&self) -> &'static str { 2991e9d4abSLoGin self.name 3091e9d4abSLoGin } 3191e9d4abSLoGin } 3291e9d4abSLoGin 3391e9d4abSLoGin pub type UnifiedInitFunction = fn() -> core::result::Result<(), SystemError>; 3491e9d4abSLoGin 3591e9d4abSLoGin /// 定义统一初始化器的分布式切片数组(私有) 3691e9d4abSLoGin #[macro_export] 3791e9d4abSLoGin macro_rules! define_unified_initializer_slice { 3891e9d4abSLoGin ($name:ident) => { 3991e9d4abSLoGin #[::linkme::distributed_slice] 4091e9d4abSLoGin static $name: [::unified_init::UnifiedInitializer] = [..]; 4191e9d4abSLoGin }; 4291e9d4abSLoGin () => { 43*0897bd8eSLoGin compile_error!( 4491e9d4abSLoGin "define_unified_initializer_slice! requires at least one argument: slice_name" 4591e9d4abSLoGin ); 4691e9d4abSLoGin }; 4791e9d4abSLoGin } 4891e9d4abSLoGin 4991e9d4abSLoGin /// 定义统一初始化器的分布式切片数组(公开) 5091e9d4abSLoGin #[macro_export] 5191e9d4abSLoGin macro_rules! define_public_unified_initializer_slice { 5291e9d4abSLoGin ($name:ident) => { 5391e9d4abSLoGin #[::linkme::distributed_slice] 5491e9d4abSLoGin pub static $name: [::unified_init::UnifiedInitializer] = [..]; 5591e9d4abSLoGin }; 5691e9d4abSLoGin () => { 57*0897bd8eSLoGin compile_error!( 5891e9d4abSLoGin "define_unified_initializer_slice! requires at least one argument: slice_name" 5991e9d4abSLoGin ); 6091e9d4abSLoGin }; 6191e9d4abSLoGin } 6291e9d4abSLoGin 6391e9d4abSLoGin /// 调用指定数组中的所有初始化器 6491e9d4abSLoGin #[macro_export] 6591e9d4abSLoGin macro_rules! unified_init { 6691e9d4abSLoGin ($initializer_slice:ident) => { 6791e9d4abSLoGin for initializer in $initializer_slice.iter() { 6891e9d4abSLoGin initializer.call().unwrap_or_else(|e| { 692eab6dd7S曾俊 log::error!("Failed to call initializer {}: {:?}", initializer.name(), e); 7091e9d4abSLoGin }); 7191e9d4abSLoGin } 7291e9d4abSLoGin }; 7391e9d4abSLoGin } 74