@@ -95,12 +95,6 @@ impl<'e> IntoLisp<'e> for &'e GlobalRef {
9595 }
9696}
9797
98- impl < ' e > IntoLisp < ' e > for & ' e OnceGlobalRef {
99- #[ inline( always) ]
100- fn into_lisp ( self , env : & ' e Env ) -> Result < Value < ' e > > {
101- Ok ( self . bind ( env) )
102- }
103- }
10498
10599impl < ' e > Value < ' e > {
106100 /// Creates a new [`GlobalRef`] for this value.
@@ -112,10 +106,50 @@ impl<'e> Value<'e> {
112106 }
113107}
114108
109+ /// Declares global references. These will be initialized when the module is loaded.
110+ #[ doc( hidden) ]
111+ #[ macro_export]
112+ macro_rules! global_refs {
113+ ( $( $name: ident) * ) => {
114+ $(
115+ #[ allow( non_upper_case_globals) ]
116+ pub static $name: & ' static $crate:: OnceGlobalRef = {
117+ static X : $crate:: OnceGlobalRef = $crate:: OnceGlobalRef :: new( ) ;
118+ & X
119+ } ;
120+ ) *
121+ } ;
122+ ( $registrator_name: ident ( $init_method: ident) =>
123+ $(
124+ $name: ident $( => $lisp_name: expr ) ?
125+ ) *
126+ ) => {
127+ $crate:: global_refs! {
128+ $( $name) *
129+ }
130+
131+ #[ $crate:: deps:: ctor:: ctor]
132+ fn $registrator_name( ) {
133+ $crate:: init:: __PRE_INIT__. try_lock( )
134+ . expect( "Failed to acquire a write lock on the list of initializers" )
135+ . push( :: std:: boxed:: Box :: new( |env| {
136+ $(
137+ #[ allow( unused_variables) ]
138+ let name = $crate:: deps:: emacs_macros:: lisp_name!( $name) ;
139+ $( let name = $lisp_name; ) ?
140+ $crate:: OnceGlobalRef :: $init_method( & $name, env, name) ?;
141+ ) *
142+ Ok ( ( ) )
143+ } ) ) ;
144+ }
145+ } ;
146+ }
147+
115148/// A [`GlobalRef`] that can be initialized once. This is useful for long-lived values that should
116- /// be initialized when the module is loaded, such as frequently-used symbols.
149+ /// be initialized when the dynamic module is loaded. A typical use case is specifying
150+ /// frequently-used symbols, which can be done with the help of the macro [`use_symbols!`].
117151///
118- /// [`GlobalRef `]: struct.GlobalRef.html
152+ /// [`use_symbols `]: crate::use_symbols
119153#[ derive( Debug ) ]
120154#[ repr( transparent) ]
121155pub struct OnceGlobalRef {
@@ -158,6 +192,13 @@ impl OnceGlobalRef {
158192 }
159193}
160194
195+ impl < ' e > IntoLisp < ' e > for & ' e OnceGlobalRef {
196+ #[ inline( always) ]
197+ fn into_lisp ( self , env : & ' e Env ) -> Result < Value < ' e > > {
198+ Ok ( self . bind ( env) )
199+ }
200+ }
201+
161202impl Deref for OnceGlobalRef {
162203 type Target = GlobalRef ;
163204
0 commit comments