Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dojo-core): add fixed array support in models #2888

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/dojo/core-cairo-test/Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version = 1

[[package]]
name = "dojo"
version = "1.0.0-rc.0"
version = "1.0.9"
dependencies = [
"dojo_plugin",
]
Expand Down
128 changes: 126 additions & 2 deletions crates/dojo/core-cairo-test/src/tests/meta/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ struct WithArray {
arr: Array<u8>
}

#[derive(Drop, Introspect)]
struct WithFixedArray {
value: u32,
arr: [u8; 3]
}

#[derive(Drop, Introspect)]
struct WithByteArray {
value: u32,
Expand Down Expand Up @@ -111,6 +117,31 @@ struct Generic<T> {
value: T,
}

#[derive(Drop, Introspect)]
struct StructWithFixedArray {
x: [u8; 3]
}

#[derive(Drop, Introspect)]
struct StructWithComplexFixedArray {
x: [[EnumWithSameData; 2]; 3]
}

#[derive(Drop, Introspect)]
enum EnumWithFixedArray {
A: [u8; 3]
}

#[derive(Drop, IntrospectPacked)]
struct StructWithFixedArrayPacked {
x: [u8; 3]
}

#[derive(Drop, IntrospectPacked)]
enum EnumWithFixedArrayPacked {
A: [u8; 3]
}

fn field(selector: felt252, layout: Layout) -> FieldLayout {
FieldLayout { selector, layout }
}
Expand All @@ -123,6 +154,10 @@ fn tuple(values: Array<Layout>) -> Layout {
Layout::Tuple(values.span())
}

fn fixed_array(inner_layout: Layout, size: u32) -> Layout {
Layout::FixedArray(array![(inner_layout, size)].span())
}

fn _enum(values: Array<Option<Layout>>) -> Layout {
let mut items = array![];
let mut i = 0;
Expand Down Expand Up @@ -166,6 +201,12 @@ fn test_size_with_array() {
assert!(Introspect::<WithArray>::size().is_none());
}

// fn test_size_with_fixed_array() {
// let size = Introspect::<WithFixedArray>::size();
// assert!(size.is_some());
// assert!(size.unwrap() == 4);
// }

#[test]
fn test_size_with_byte_array() {
assert!(Introspect::<WithByteArray>::size().is_none());
Expand Down Expand Up @@ -212,7 +253,6 @@ fn test_size_of_enum_with_same_tuple_variant_data() {
assert!(size.unwrap() == 4);
}


#[test]
fn test_size_of_struct_with_option() {
let size = Introspect::<StructWithOption>::size();
Expand All @@ -225,6 +265,41 @@ fn test_size_of_enum_with_variant_data() {
assert!(size.is_none());
}

#[test]
fn test_size_of_struct_with_fixed_array() {
let size = Introspect::<StructWithFixedArray>::size();
assert!(size.is_some());
assert!(size.unwrap() == 3);
}

#[test]
fn test_size_of_struct_with_complex_fixed_array() {
let size = Introspect::<StructWithComplexFixedArray>::size();
assert!(size.is_some());
assert!(size.unwrap() == 18);
}

#[test]
fn test_size_of_packed_struct_with_fixed_array() {
let size = Introspect::<StructWithFixedArrayPacked>::size();
assert!(size.is_some());
assert!(size.unwrap() == 3);
}

#[test]
fn test_size_of_enum_with_fixed_array() {
let size = Introspect::<EnumWithFixedArray>::size();
assert!(size.is_some());
assert!(size.unwrap() == 4);
}

#[test]
fn test_size_of_packed_enum_with_fixed_array() {
let size = Introspect::<EnumWithFixedArrayPacked>::size();
assert!(size.is_some());
assert!(size.unwrap() == 4);
}

#[test]
fn test_layout_of_enum_without_variant_data() {
let layout = Introspect::<EnumNoData>::layout();
Expand Down Expand Up @@ -264,6 +339,56 @@ fn test_layout_of_struct_with_option() {
assert!(layout == expected);
}

#[test]
fn test_layout_of_struct_with_fixed_array() {
let layout = Introspect::<StructWithFixedArray>::layout();
let expected = Layout::Struct(
array![field(selector!("x"), fixed_array(Introspect::<u8>::layout(), 3))].span()
);

assert!(layout == expected);
}

#[test]
fn test_layout_of_struct_with_complex_fixed_array() {
let layout = Introspect::<StructWithComplexFixedArray>::layout();
let expected = Layout::Struct(
array![
field(
selector!("x"),
fixed_array(fixed_array(Introspect::<EnumWithSameData>::layout(), 2), 3)
)
]
.span()
);

assert!(layout == expected);
}

#[test]
fn test_layout_of_packed_struct_with_fixed_array() {
let layout = Introspect::<StructWithFixedArrayPacked>::layout();
let expected = Layout::Fixed([8, 8, 8].span());

assert!(layout == expected);
}

#[test]
fn test_layout_of_enum_with_fixed_array() {
let layout = Introspect::<EnumWithFixedArray>::layout();
let expected = _enum(array![Option::Some(fixed_array(Introspect::<u8>::layout(), 3))]);

assert!(layout == expected);
}

#[test]
fn test_layout_of_packed_enum_with_fixed_array() {
let layout = Introspect::<EnumWithFixedArrayPacked>::layout();
let expected = Layout::Fixed([8, 8, 8, 8].span());

assert!(layout == expected);
}

#[test]
fn test_layout_of_packed_struct() {
let layout = Introspect::<Vec3>::layout();
Expand All @@ -286,7 +411,6 @@ fn test_layout_of_not_packed_inner_struct() {
let _ = Introspect::<StructInnerNotPacked>::layout();
}


#[test]
fn test_layout_of_packed_enum() {
let layout = Introspect::<EnumPacked>::layout();
Expand Down
26 changes: 26 additions & 0 deletions crates/dojo/core-cairo-test/src/tests/model/model.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,21 @@ struct Foo2 {
v2: u32
}


#[derive(Copy, Drop, Serde, Debug)]
#[dojo::model]
struct ModelWithFixedArray {
#[key]
k1: u8,
v1: [u16; 3]
}

fn namespace_def() -> NamespaceDef {
NamespaceDef {
namespace: "dojo_cairo_test", resources: [
TestResource::Model(m_Foo::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Model(m_Foo2::TEST_CLASS_HASH.try_into().unwrap()),
TestResource::Model(m_ModelWithFixedArray::TEST_CLASS_HASH.try_into().unwrap()),
].span()
}
}
Expand Down Expand Up @@ -181,3 +191,19 @@ fn test_model_ptr_from_entity_id() {
let v1 = world.read_member(ptr, selector!("v1"));
assert!(foo.v1 == v1);
}

#[test]
fn test_model_with_fixed_array() {
let mut world = spawn_foo_world();
let model = ModelWithFixedArray { k1: 1, v1: [4, 32, 256] };

world.write_model(@model);
let read_model: ModelWithFixedArray = world.read_model(model.keys());

assert!(model.v1 == read_model.v1);

world.erase_model(@model);
let read_model: ModelWithFixedArray = world.read_model(model.keys());

assert!(read_model.v1 == [0, 0, 0]);
}
17 changes: 17 additions & 0 deletions crates/dojo/core/src/meta/introspect.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum Ty {
// And `Box` is not serializable. So using a Span, even if it's to have
// one element, does the trick.
Array: Span<Ty>,
FixedArray: Span<(Ty, u32)>,
ByteArray,
}

Expand Down Expand Up @@ -90,6 +91,22 @@ pub impl Introspect_bool of Introspect<bool> {
}
}

pub impl Introspect_FixedArray<T, const N: usize, +Introspect<T>> of Introspect<[T; N]> {
fn size() -> Option<usize> {
match Introspect::<T>::size() {
Option::Some(size) => Option::Some(size * N),
Option::None => Option::None
}
}
fn layout() -> Layout {
Layout::FixedArray([(Introspect::<T>::layout(), N)].span())
}
fn ty() -> Ty {
Ty::FixedArray([(Introspect::<T>::ty(), N)].span())
}
}


pub impl Introspect_u8 of Introspect<u8> {
fn size() -> Option<usize> {
Option::Some(1)
Expand Down
3 changes: 3 additions & 0 deletions crates/dojo/core/src/meta/layout.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct FieldLayout {
pub layout: Layout
}


#[derive(Copy, Drop, Serde, Debug, PartialEq)]
pub enum Layout {
Fixed: Span<u8>,
Expand All @@ -15,6 +16,7 @@ pub enum Layout {
// And `Box` is not serializable. So using a Span, even if it's to have
// one element, does the trick.
Array: Span<Layout>,
FixedArray: Span<(Layout, u32)>,
ByteArray,
// there is one layout per variant.
// the `selector` field identifies the variant
Expand All @@ -30,6 +32,7 @@ pub impl LayoutCompareImpl of LayoutCompareTrait {
(Layout::Struct(_), Layout::Struct(_)) => true,
(Layout::Tuple(_), Layout::Tuple(_)) => true,
(Layout::Array(_), Layout::Array(_)) => true,
(Layout::FixedArray(_), Layout::FixedArray(_)) => true,
(Layout::ByteArray, Layout::ByteArray) => true,
(Layout::Enum(_), Layout::Enum(_)) => true,
_ => false
Expand Down
Loading
Loading