Make validate_downcastable_from unsafe by coolreader18 · Pull Request #6851 · RustPython/RustPython
📝 Walkthrough
Walkthrough
The PR refactors RustPython's object downcasting mechanism by making validate_downcastable_from unsafe across the codebase and optimizing PyObject::downcastable with a type-ID fast path. The trait method downcastable_from is removed and replaced with instance method dispatch via downcastable.
Changes
| Cohort / File(s) | Summary |
|---|---|
Payload Trait Updates crates/vm/src/object/payload.rs |
Removed trait method downcastable_from, changed validate_downcastable_from signature to unsafe fn, and updated try_downcast_from to use obj.downcastable::<Self>() for delegation |
Core Downcast Optimization crates/vm/src/object/core.rs |
Refactored PyObject::downcastable<T> to perform type-ID comparison before calling unsafe T::validate_downcastable_from(self) for a fast-path optimization |
Derive Macro Implementations crates/derive-impl/src/pyclass.rs, crates/derive-impl/src/pystructseq.rs |
Updated validate_downcastable_from method signature in PyPayload implementations from fn to unsafe fn |
Builtin Type Implementation crates/vm/src/builtins/str.rs |
Changed PyUtf8Str::validate_downcastable_from signature to unsafe fn |
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~12 minutes
Possibly related PRs
- Wtf8-compatible fixes #5985: Modifies
PyObject::downcastablewith type-ID fast path and updates PyPayload validation handling in similar pattern - overridable validate_downcastable_from #6393: Changes PyPayload downcast validation logic with
validate_downcastable_fromand updatesdowncastable_fromusage patterns - Separate Debug from PyPayload #6320: Modifies the PyPayload trait definition in
crates/vm/src/object/payload.rsaffecting trait structure and constraints
Poem
🐰 Unsafe functions now guard the path,
Type IDs dash through fast,
Downcasting logic, sleek and lean,
Faster validation's never been seen! ✨
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title 'Make validate_downcastable_from unsafe' clearly and specifically describes the primary change across all modified files: converting validate_downcastable_from from a safe function to an unsafe function. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
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 @coderabbitai help to get the list of available commands and usage tips.