@@ -58,7 +58,7 @@ pub struct Entities {
58
58
/// Important internal invariant: for any `Entities` object that exists, the
59
59
/// the `ancestor` relation is transitively closed.
60
60
#[ serde_as( as = "Vec<(_, _)>" ) ]
61
- entities : HashMap < EntityUID , Entity > ,
61
+ entities : HashMap < EntityUID , Arc < Entity > > ,
62
62
63
63
/// The mode flag determines whether this store functions as a partial store or
64
64
/// as a fully concrete store.
@@ -109,7 +109,7 @@ impl Entities {
109
109
110
110
/// Iterate over the `Entity`s in the `Entities`
111
111
pub fn iter ( & self ) -> impl Iterator < Item = & Entity > {
112
- self . entities . values ( )
112
+ self . entities . values ( ) . map ( |e| e . as_ref ( ) )
113
113
}
114
114
115
115
/// Adds the [`crate::ast::Entity`]s in the iterator to this [`Entities`].
@@ -125,7 +125,7 @@ impl Entities {
125
125
/// responsible for ensuring that TC and DAG hold before calling this method.
126
126
pub fn add_entities (
127
127
mut self ,
128
- collection : impl IntoIterator < Item = Entity > ,
128
+ collection : impl IntoIterator < Item = Arc < Entity > > ,
129
129
schema : Option < & impl Schema > ,
130
130
tc_computation : TCComputation ,
131
131
extensions : & Extensions < ' _ > ,
@@ -174,7 +174,7 @@ impl Entities {
174
174
tc_computation : TCComputation ,
175
175
extensions : & Extensions < ' _ > ,
176
176
) -> Result < Self > {
177
- let mut entity_map = create_entity_map ( entities. into_iter ( ) ) ?;
177
+ let mut entity_map = create_entity_map ( entities. into_iter ( ) . map ( Arc :: new ) ) ?;
178
178
if let Some ( schema) = schema {
179
179
// Validate non-action entities against schema.
180
180
// We do this before adding the actions, because we trust the
@@ -213,7 +213,7 @@ impl Entities {
213
213
schema
214
214
. action_entities ( )
215
215
. into_iter ( )
216
- . map ( |e| ( e. uid ( ) . clone ( ) , Arc :: unwrap_or_clone ( e ) ) ) ,
216
+ . map ( |e : Arc < Entity > | ( e. uid ( ) . clone ( ) , e ) ) ,
217
217
) ;
218
218
}
219
219
Ok ( Self {
@@ -252,6 +252,7 @@ impl Entities {
252
252
fn to_ejsons ( & self ) -> Result < Vec < EntityJson > > {
253
253
self . entities
254
254
. values ( )
255
+ . map ( Arc :: as_ref)
255
256
. map ( EntityJson :: from_entity)
256
257
. collect :: < std:: result:: Result < _ , JsonSerializationError > > ( )
257
258
. map_err ( Into :: into)
@@ -322,7 +323,9 @@ impl Entities {
322
323
}
323
324
324
325
/// Create a map from EntityUids to Entities, erroring if there are any duplicates
325
- fn create_entity_map ( es : impl Iterator < Item = Entity > ) -> Result < HashMap < EntityUID , Entity > > {
326
+ fn create_entity_map (
327
+ es : impl Iterator < Item = Arc < Entity > > ,
328
+ ) -> Result < HashMap < EntityUID , Arc < Entity > > > {
326
329
let mut map = HashMap :: new ( ) ;
327
330
for e in es {
328
331
match map. entry ( e. uid ( ) . clone ( ) ) {
@@ -338,10 +341,13 @@ fn create_entity_map(es: impl Iterator<Item = Entity>) -> Result<HashMap<EntityU
338
341
impl IntoIterator for Entities {
339
342
type Item = Entity ;
340
343
341
- type IntoIter = hash_map:: IntoValues < EntityUID , Entity > ;
344
+ type IntoIter = std:: iter:: Map <
345
+ std:: collections:: hash_map:: IntoValues < EntityUID , Arc < Entity > > ,
346
+ fn ( Arc < Entity > ) -> Entity ,
347
+ > ;
342
348
343
349
fn into_iter ( self ) -> Self :: IntoIter {
344
- self . entities . into_values ( )
350
+ self . entities . into_values ( ) . map ( Arc :: unwrap_or_clone )
345
351
}
346
352
}
347
353
@@ -497,7 +503,8 @@ mod json_parsing_tests {
497
503
498
504
let addl_entities = parser
499
505
. iter_from_json_value ( new)
500
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
506
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
507
+ . map ( Arc :: new) ;
501
508
let err = simple_entities ( & parser) . add_entities (
502
509
addl_entities,
503
510
None :: < & NoEntitiesSchema > ,
@@ -537,7 +544,8 @@ mod json_parsing_tests {
537
544
538
545
let addl_entities = parser
539
546
. iter_from_json_value ( new)
540
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
547
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
548
+ . map ( Arc :: new) ;
541
549
let err = simple_entities ( & parser) . add_entities (
542
550
addl_entities,
543
551
None :: < & NoEntitiesSchema > ,
@@ -576,7 +584,8 @@ mod json_parsing_tests {
576
584
577
585
let addl_entities = parser
578
586
. iter_from_json_value ( new)
579
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
587
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
588
+ . map ( Arc :: new) ;
580
589
let err = simple_entities ( & parser) . add_entities (
581
590
addl_entities,
582
591
None :: < & NoEntitiesSchema > ,
@@ -619,7 +628,8 @@ mod json_parsing_tests {
619
628
620
629
let addl_entities = parser
621
630
. iter_from_json_value ( new)
622
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
631
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
632
+ . map ( Arc :: new) ;
623
633
let es = simple_entities ( & parser)
624
634
. add_entities (
625
635
addl_entities,
@@ -658,7 +668,8 @@ mod json_parsing_tests {
658
668
659
669
let addl_entities = parser
660
670
. iter_from_json_value ( new)
661
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
671
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
672
+ . map ( Arc :: new) ;
662
673
let es = simple_entities ( & parser)
663
674
. add_entities (
664
675
addl_entities,
@@ -699,7 +710,8 @@ mod json_parsing_tests {
699
710
700
711
let addl_entities = parser
701
712
. iter_from_json_value ( new)
702
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
713
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
714
+ . map ( Arc :: new) ;
703
715
let es = simple_entities ( & parser)
704
716
. add_entities (
705
717
addl_entities,
@@ -739,7 +751,8 @@ mod json_parsing_tests {
739
751
740
752
let addl_entities = parser
741
753
. iter_from_json_value ( new)
742
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
754
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
755
+ . map ( Arc :: new) ;
743
756
let es = simple_entities ( & parser)
744
757
. add_entities (
745
758
addl_entities,
@@ -766,7 +779,8 @@ mod json_parsing_tests {
766
779
767
780
let addl_entities = parser
768
781
. iter_from_json_value ( new)
769
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
782
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
783
+ . map ( Arc :: new) ;
770
784
let err = simple_entities ( & parser)
771
785
. add_entities (
772
786
addl_entities,
@@ -787,7 +801,8 @@ mod json_parsing_tests {
787
801
let new = serde_json:: json!( [ { "uid" : { "type" : "Test" , "id" : "alice" } , "attrs" : { } , "parents" : [ ] } ] ) ;
788
802
let addl_entities = parser
789
803
. iter_from_json_value ( new)
790
- . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) ) ;
804
+ . unwrap_or_else ( |e| panic ! ( "{:?}" , & miette:: Report :: new( e) ) )
805
+ . map ( Arc :: new) ;
791
806
let err = simple_entities ( & parser) . add_entities (
792
807
addl_entities,
793
808
None :: < & NoEntitiesSchema > ,
0 commit comments