diff --git a/crates/dojo/core-cairo-test/src/tests/model/model.cairo b/crates/dojo/core-cairo-test/src/tests/model/model.cairo index 68f6cf5778..e53673c40e 100644 --- a/crates/dojo/core-cairo-test/src/tests/model/model.cairo +++ b/crates/dojo/core-cairo-test/src/tests/model/model.cairo @@ -1,8 +1,8 @@ -use dojo::model::{Model, ModelValue, ModelStorage, ModelValueStorage}; +use dojo::model::{Model, ModelValue, ModelStorage, ModelValueStorage, ModelPtr}; use dojo::world::WorldStorage; use dojo_cairo_test::{spawn_test_world, NamespaceDef, TestResource}; -#[derive(Copy, Drop, Serde, Debug)] +#[derive(Copy, Drop, Serde, Debug, PartialEq)] #[dojo::model] struct Foo { #[key] @@ -194,3 +194,28 @@ fn test_model_ptr_from_entity_id() { let v1 = world.read_member(ptr, selector!("v1")); assert!(foo.v1 == v1); } + +#[test] +fn test_ptr_from() { + let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 }; + let ptr_a = ModelPtr:: { id: foo.entity_id() }; + let ptr_b = Model::::ptr_from_keys(foo.keys()); + let ptr_c = Model::::ptr_from_serialized_keys([foo.k1.into(), foo.k2].span()); + let ptr_d = Model::::ptr_from_id(foo.entity_id()); + assert!(ptr_a == ptr_b && ptr_a == ptr_c && ptr_a == ptr_d); +} + +fn test_ptrs_from() { + let foo = Foo { k1: 1, k2: 2, v1: 3, v2: 4 }; + let foo2 = Foo { k1: 3, k2: 4, v1: 5, v2: 6 }; + let ptrs_a = [ModelPtr:: { id: foo.entity_id() }, ModelPtr:: { id: foo2.entity_id() }] + .span(); + let ptrs_b = Model::::ptrs_from_keys([foo.keys(), foo2.keys()].span()); + let ptrs_c = Model::< + Foo, + >::ptrs_from_serialized_keys( + [[foo.k1.into(), foo.k2].span(), [foo2.k1.into(), foo2.k2].span()].span(), + ); + let ptrs_d = Model::::ptrs_from_ids([foo.entity_id(), foo2.entity_id()].span()); + assert!(ptrs_a == ptrs_b && ptrs_a == ptrs_c && ptrs_a == ptrs_d); +} diff --git a/crates/dojo/core/src/model/model.cairo b/crates/dojo/core/src/model/model.cairo index 5b5e20a72d..134a924daa 100644 --- a/crates/dojo/core/src/model/model.cairo +++ b/crates/dojo/core/src/model/model.cairo @@ -58,17 +58,23 @@ pub trait Model { fn definition() -> ModelDef; /// Returns the selector of the model computed for the given namespace hash. fn selector(namespace_hash: felt252) -> felt252; - /// Returns the pointer to the model from the key. - fn ptr_from_keys, +Drop>(keys: K) -> ModelPtr; /// Returns the pointer to the model from the keys. + fn ptr_from_keys, +Drop>(keys: K) -> ModelPtr; + /// Returns the pointer to the model from the serialised keys. fn ptr_from_serialized_keys(keys: Span) -> ModelPtr; /// Returns the pointer to the model from the entity id. fn ptr_from_id(entity_id: felt252) -> ModelPtr; + /// Returns the pointers to models from the keys. + fn ptrs_from_keys, +Drop>(keys: Span) -> Span>; + /// Returns the pointers to models from the serialised keys. + fn ptrs_from_serialized_keys(keys: Span>) -> Span>; + /// Returns the pointers to models from the entity ids. + fn ptrs_from_ids(entity_ids: Span) -> Span>; /// Returns the ptr of the model. fn ptr(self: @M) -> ModelPtr; } -pub impl ModelImpl, +ModelDefinition, +Serde> of Model { +pub impl ModelImpl, +ModelDefinition, +Serde, +Drop> of Model { fn keys>(self: @M) -> K { KeyParser::::parse_key(self) } @@ -147,6 +153,30 @@ pub impl ModelImpl, +ModelDefinition, +Serde> of Model< ModelPtr:: { id: entity_id } } + fn ptrs_from_keys, +Drop>(keys: Span) -> Span> { + let mut ptrs = ArrayTrait::>::new(); + for key in keys { + ptrs.append(ModelPtr { id: entity_id_from_keys(key) }); + }; + ptrs.span() + } + + fn ptrs_from_serialized_keys(keys: Span>) -> Span> { + let mut ptrs = ArrayTrait::>::new(); + for key in keys { + ptrs.append(Self::ptr_from_serialized_keys(*key)); + }; + ptrs.span() + } + + fn ptrs_from_ids(entity_ids: Span) -> Span> { + let mut ptrs = ArrayTrait::>::new(); + for entity_id in entity_ids { + ptrs.append(Self::ptr_from_id(*entity_id)); + }; + ptrs.span() + } + fn ptr(self: @M) -> ModelPtr { ModelPtr:: { id: self.entity_id() } }