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 support for making multiple model pointers #2940

Merged
merged 2 commits into from
Jan 24, 2025

Conversation

bengineer42
Copy link
Contributor

@bengineer42 bengineer42 commented Jan 21, 2025

…n methods

Description

added methods to make mutiple model ptrs and tests

pub trait Model<M> {
    ...
    /// Returns the pointers to models from the keys.
    fn ptrs_from_keys<K, +Serde<K>, +Drop<K>>(keys: Span<K>) -> Span<ModelPtr<M>>;
    /// Returns the pointers to models from the serialised keys.
    fn ptrs_from_serialized_keys(keys: Span<Span<felt252>>) -> Span<ModelPtr<M>>;
    /// Returns the pointers to models from the entity ids.
    fn ptrs_from_ids(entity_ids: Span<felt252>) -> Span<ModelPtr<M>>;
    ...
}

Related issue

Tests

  • Yes
  • No, because they aren't needed
  • No, because I need help

Added to documentation?

  • README.md
  • Dojo Book
  • No documentation needed

Checklist

  • I've formatted my code (scripts/prettier.sh, scripts/rust_fmt.sh, scripts/cairo_fmt.sh)
  • I've linted my code (scripts/clippy.sh, scripts/docs.sh)
  • I've commented my code
  • I've requested a review after addressing the comments

Summary by CodeRabbit

  • New Features

    • Added new methods to generate model pointers from keys, serialized keys, and entity IDs
    • Introduced ability to create spans of model pointers
    • Enhanced model pointer functionality with new ModelPtr type
  • Tests

    • Added test functions to validate model pointer creation and behavior
  • Improvements

    • Updated Foo struct to support equality comparisons
    • Modified ModelImpl trait to include Drop trait requirement

Copy link

coderabbitai bot commented Jan 21, 2025

Walkthrough

Ohayo, sensei! This pull request introduces enhancements to the model pointer functionality in the Dojo framework. The changes primarily focus on expanding the Model trait with new methods for generating spans of model pointers from various input types like keys, serialized keys, and entity IDs. The modifications include adding new methods to retrieve multiple model pointers and updating the trait implementation to support these new capabilities.

Changes

File Changes
crates/dojo/core-cairo-test/src/tests/model/model.cairo - Added PartialEq derive to Foo struct
- Added test functions test_ptr_from() and test_ptrs_from()
crates/dojo/core/src/model/model.cairo - Added methods ptrs_from_keys(), ptrs_from_serialized_keys(), ptrs_from_ids()
- Updated ModelImpl trait bound to include +Drop<M>

Possibly related PRs

Suggested Labels

contributor

Suggested Reviewers

  • glihm

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
crates/dojo/core/src/model/model.cairo (2)

164-170: Consider making span dereferencing more explicit, sensei.

While the implementation is correct, the *key dereferencing could be made more explicit for better readability.

-            ptrs.append(Self::ptr_from_serialized_keys(*key));
+            let key_span = *key;
+            ptrs.append(Self::ptr_from_serialized_keys(key_span));

172-178: Apply consistent dereferencing style, sensei.

Similar to the previous suggestion, consider making the dereferencing more explicit.

-            ptrs.append(Self::ptr_from_id(*entity_id));
+            let id = *entity_id;
+            ptrs.append(Self::ptr_from_id(id));
crates/dojo/core-cairo-test/src/tests/model/model.cairo (1)

208-221: Consider simplifying the type parameter formatting, sensei.

While the test coverage is excellent, the type parameter formatting for Model::<Foo> spans multiple lines unnecessarily.

-    let ptrs_c = Model::<
-        Foo,
-    >::ptrs_from_serialized_keys(
+    let ptrs_c = Model::<Foo>::ptrs_from_serialized_keys(
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ac35492 and d8f8767.

📒 Files selected for processing (2)
  • crates/dojo/core-cairo-test/src/tests/model/model.cairo (2 hunks)
  • crates/dojo/core/src/model/model.cairo (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: build
  • GitHub Check: docs
  • GitHub Check: ensure-wasm
🔇 Additional comments (5)
crates/dojo/core/src/model/model.cairo (4)

Line range hint 8-12: Ohayo! Clean and well-documented struct implementation, sensei!

The ModelPtr<M> struct is well-designed with appropriate trait derivations and clear documentation.


67-72: Excellent addition of batch processing methods, sensei!

The new methods for generating multiple model pointers follow a consistent pattern and are well-documented.


156-162: Clean implementation of batch key processing, sensei!

The implementation efficiently reuses entity_id_from_keys and follows the established pattern.


62-62: Verify impact of Drop bound addition.

The addition of the Drop bound to ptr_from_keys might affect existing implementations.

✅ Verification successful

Ohayo sensei!

Drop bound addition is safe to proceed

The verification shows all existing implementations and usages of ptr_from_keys are compatible with the new Drop bound. Found usages in tests and examples already work with types that implement Drop.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for existing implementations that might be affected by the new Drop bound
rg -l "impl.*Model.*ptr_from_keys" | while read -r file; do
    echo "=== $file ==="
    rg "impl.*Model.*ptr_from_keys" "$file" -A 5
done

Length of output: 51


Script:

#!/bin/bash
# Try multiple search strategies

echo "=== Searching for Model implementations ==="
ast-grep --pattern 'impl $_ for $_ {
  $$$
}'

echo "=== Searching for ptr_from_keys usages ==="
rg "ptr_from_keys" -A 3

echo "=== Searching in test files ==="
fd "test" --type f --exec rg "ptr_from_keys" {} -A 3 \;

Length of output: 66095


Script:

#!/bin/bash
# Search for Model implementations containing ptr_from_keys
echo "=== Searching for Model implementations with ptr_from_keys ==="
ast-grep --pattern 'impl $_ for $_ {
  $$$
  fn ptr_from_keys$_($_) $_
  $$$
}'

echo "=== Searching for ptr_from_keys method calls ==="
rg "ptr_from_keys" -B 2 -A 2

Length of output: 2892

crates/dojo/core-cairo-test/src/tests/model/model.cairo (1)

198-206: Excellent test coverage for pointer creation methods, sensei!

The test comprehensively verifies that all pointer creation methods produce equivalent results.

Copy link

codecov bot commented Jan 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 55.90%. Comparing base (01111f4) to head (52eba0f).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2940      +/-   ##
==========================================
- Coverage   55.91%   55.90%   -0.01%     
==========================================
  Files         446      446              
  Lines       57814    57814              
==========================================
- Hits        32325    32321       -4     
- Misses      25489    25493       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@glihm glihm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are mostly convenient implementations, right? Or is this something that will directly be used for the second PR with the several models field at the same time?

@glihm
Copy link
Collaborator

glihm commented Jan 21, 2025

Those are mostly convenient implementations, right? Or is this something that will directly be used for the second PR with the several models field at the same time?

If yes, could you please update #2939?

@glihm glihm enabled auto-merge (squash) January 24, 2025 03:38
@glihm glihm disabled auto-merge January 24, 2025 03:38
@glihm glihm changed the title feat(model): add support for making multiple model pointers feat(dojo-core): add support for making multiple model pointers Jan 24, 2025
@glihm glihm enabled auto-merge (squash) January 24, 2025 03:38
@glihm glihm merged commit 94e6e16 into dojoengine:main Jan 24, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants