Add set_attrs! macro for setting multiple attributes on an object#8034
Conversation
|
Wondering what really moved? Review this PR in Change Stack to inspect semantic changes, definitions, and references. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdds an exported ChangesMacro Introduction and Cross-Crate Adoption
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
e15042b to
6177f89
Compare
| .set_attr("__ctype_be__", type_ref.as_object().to_owned(), vm)?; | ||
| set_attrs!( | ||
| type_ref.as_object(), vm, | ||
| "__ctype_le__" => type_ref.as_object().to_owned(), |
There was a problem hiding this comment.
I like this macro, but => is looking a bit awkward to me. is there other options that can show this is not a match flow?
There was a problem hiding this comment.
I think => was the clearest token, but if you have something else in mind I'd be happy tp change
There was a problem hiding this comment.
🧹 Nitpick comments (2)
crates/vm/src/protocol/object.rs (1)
833-842: ⚡ Quick winConsolidate duplicated macro branch logic.
Both
set_attrs!arms repeat the same loop body and only differ in failure handling (unwrapvs?). Please factor the differing behavior so the shared loop exists once.As per coding guidelines, "When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once to avoid duplicate code".
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/vm/src/protocol/object.rs` around lines 833 - 842, The two arms of the set_attrs! macro duplicate the same loop body but differ only by error handling (one uses unwrap, the other uses ?); refactor by first selecting the error-handling strategy (e.g., bind a local handler/closure or a flag like let handle_err = |res| -> _ { res.unwrap() } or let handle_err = |res| -> _ { res?; Ok(()) }) inside each match arm, then run the common loop once calling that handler for each $obj.set_attr($key, $val, $vm) invocation; update the macro arms so they differ only in the handler binding and then execute the shared loop (referencing set_attrs!, set_attr, unwrap, and ? to locate the spots).crates/vm/src/stdlib/_ctypes/simple.rs (1)
667-693: ⚡ Quick winCollapse endian branches into one shared assignment path.
The
if is_little_endian/elseblocks duplicate the sameset_attrs!calls and only differ in target values. Extract the two targets once, then invoke shared logic once.♻️ Proposed refactor
- if is_little_endian { - // Little-endian system: __ctype_le__ = self, __ctype_be__ = swapped - set_attrs!( - type_ref.as_object(), vm, - "__ctype_le__" => type_ref.as_object().to_owned(), - "__ctype_be__" => swapped_type.clone(), - ); - - set_attrs!( - swapped_type, vm, - "__ctype_le__" => type_ref.as_object().to_owned(), - "__ctype_be__" => swapped_type.clone(), - ); - } else { - // Big-endian system: __ctype_be__ = self, __ctype_le__ = swapped - set_attrs!( - type_ref.as_object(), vm, - "__ctype_be__" => type_ref.as_object().to_owned(), - "__ctype_le__" => swapped_type.clone(), - ); - - set_attrs!( - swapped_type, vm, - "__ctype_be__" => type_ref.as_object().to_owned(), - "__ctype_le__" => swapped_type.clone(), - ); - } + let type_obj = type_ref.as_object().to_owned(); + let (ctype_le, ctype_be) = if is_little_endian { + (type_obj.clone(), swapped_type.clone()) + } else { + (swapped_type.clone(), type_obj.clone()) + }; + + set_attrs!( + type_ref.as_object(), vm, + "__ctype_le__" => ctype_le.clone(), + "__ctype_be__" => ctype_be.clone(), + ); + + set_attrs!( + swapped_type, vm, + "__ctype_le__" => ctype_le, + "__ctype_be__" => ctype_be, + );As per coding guidelines, "When branches differ only in a value but share common logic, extract the differing value first, then call the common logic once to avoid duplicate code".
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@crates/vm/src/stdlib/_ctypes/simple.rs` around lines 667 - 693, The two endian branches duplicate set_attrs! calls; compute the two attribute values first based on is_little_endian (e.g. let ctype_le = if is_little_endian { type_ref.as_object().to_owned() } else { swapped_type.clone() } and let ctype_be = if is_little_endian { swapped_type.clone() } else { type_ref.as_object().to_owned() }), then call set_attrs! once for type_ref.as_object() and once for swapped_type using those computed ctype_le and ctype_be variables; this removes the duplicated logic while keeping the same semantics for type_ref, swapped_type, is_little_endian and the set_attrs! macro.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@crates/vm/src/protocol/object.rs`:
- Around line 833-842: The two arms of the set_attrs! macro duplicate the same
loop body but differ only by error handling (one uses unwrap, the other uses ?);
refactor by first selecting the error-handling strategy (e.g., bind a local
handler/closure or a flag like let handle_err = |res| -> _ { res.unwrap() } or
let handle_err = |res| -> _ { res?; Ok(()) }) inside each match arm, then run
the common loop once calling that handler for each $obj.set_attr($key, $val,
$vm) invocation; update the macro arms so they differ only in the handler
binding and then execute the shared loop (referencing set_attrs!, set_attr,
unwrap, and ? to locate the spots).
In `@crates/vm/src/stdlib/_ctypes/simple.rs`:
- Around line 667-693: The two endian branches duplicate set_attrs! calls;
compute the two attribute values first based on is_little_endian (e.g. let
ctype_le = if is_little_endian { type_ref.as_object().to_owned() } else {
swapped_type.clone() } and let ctype_be = if is_little_endian {
swapped_type.clone() } else { type_ref.as_object().to_owned() }), then call
set_attrs! once for type_ref.as_object() and once for swapped_type using those
computed ctype_le and ctype_be variables; this removes the duplicated logic
while keeping the same semantics for type_ref, swapped_type, is_little_endian
and the set_attrs! macro.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
Run ID: d14dd515-78ee-443c-99c1-bd2c0dd23f78
📒 Files selected for processing (4)
crates/vm/src/exceptions.rscrates/vm/src/protocol/object.rscrates/vm/src/stdlib/_ctypes/simple.rscrates/vm/src/vm/vm_new.rs
Summary
Summary by CodeRabbit