2020
2121use crate :: { reflect:: prelude:: * , type_traits:: prelude:: * , visitor:: prelude:: * , SafeLock } ;
2222use fxhash:: FxHashMap ;
23- use parking_lot:: Mutex ;
23+ use parking_lot:: { Mutex , MutexGuard } ;
2424use std:: {
2525 any:: { type_name, Any , TypeId } ,
2626 fmt:: { Debug , Display , Formatter } ,
8787
8888#[ derive( Debug , TypeUuidProvider ) ]
8989#[ type_uuid( id = "87d9ef74-09a9-4228-a2d1-df270b50fddb" ) ]
90- pub struct DynTypeWrapper ( Box < dyn DynType > ) ;
90+ pub struct DynTypeWrapper ( pub Box < dyn DynType > ) ;
9191
9292impl Clone for DynTypeWrapper {
9393 fn clone ( & self ) -> Self {
@@ -197,7 +197,7 @@ impl Reflect for DynTypeWrapper {
197197}
198198
199199#[ derive( Default , Reflect , Clone , Debug ) ]
200- pub struct DynTypeContainer ( Option < DynTypeWrapper > ) ;
200+ pub struct DynTypeContainer ( pub Option < DynTypeWrapper > ) ;
201201
202202impl DynTypeContainer {
203203 pub fn value_ref ( & self ) -> Option < & dyn DynType > {
@@ -245,24 +245,36 @@ impl Visit for DynTypeContainer {
245245 }
246246}
247247
248- pub type DynTypeConstructor = Box < dyn Fn ( ) -> Box < dyn DynType > > ;
248+ pub type DynTypeConstructor = Box < dyn Fn ( ) -> Box < dyn DynType > + Send + ' static > ;
249249
250+ pub struct DynTypeConstructorDefinition {
251+ pub name : String ,
252+ pub constructor : DynTypeConstructor ,
253+ }
254+
255+ #[ derive( Default ) ]
250256pub struct DynTypeConstructorContainer {
251- map : Mutex < FxHashMap < Uuid , DynTypeConstructor > > ,
257+ map : Mutex < FxHashMap < Uuid , DynTypeConstructorDefinition > > ,
252258}
253259
254260impl DynTypeConstructorContainer {
255261 pub fn try_create ( & self , type_uuid : & Uuid ) -> Option < Box < dyn DynType > > {
256- self . map . safe_lock ( ) . get ( type_uuid) . map ( |c| ( * c) ( ) )
262+ self . map
263+ . safe_lock ( )
264+ . get ( type_uuid)
265+ . map ( |c| ( * c. constructor ) ( ) )
257266 }
258267
259- pub fn add < T > ( & self ) -> & Self
268+ pub fn add < T > ( & self , name : & str ) -> & Self
260269 where
261270 T : TypeUuidProvider + Default + DynType ,
262271 {
263272 let old = self . map . safe_lock ( ) . insert (
264273 <T as TypeUuidProvider >:: type_uuid ( ) ,
265- Box :: new ( || Box :: new ( T :: default ( ) ) ) ,
274+ DynTypeConstructorDefinition {
275+ name : name. to_string ( ) ,
276+ constructor : Box :: new ( || Box :: new ( T :: default ( ) ) ) ,
277+ } ,
266278 ) ;
267279
268280 assert ! ( old. is_none( ) ) ;
@@ -273,7 +285,7 @@ impl DynTypeConstructorContainer {
273285 pub fn add_custom (
274286 & self ,
275287 type_uuid : Uuid ,
276- constructor : DynTypeConstructor ,
288+ constructor : DynTypeConstructorDefinition ,
277289 ) -> Result < ( ) , String > {
278290 let mut map = self . map . safe_lock ( ) ;
279291 let old = map. insert ( type_uuid, constructor) ;
@@ -282,4 +294,8 @@ impl DynTypeConstructorContainer {
282294
283295 Ok ( ( ) )
284296 }
297+
298+ pub fn inner ( & self ) -> MutexGuard < FxHashMap < Uuid , DynTypeConstructorDefinition > > {
299+ self . map . safe_lock ( )
300+ }
285301}
0 commit comments