[RFC] Split oparg type from the opcode enum
@coolreader18 đang làm issue này rồi.
Từ ngày 16/1/2026.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
Summary
Introduce a new AnyOparg enum & make Instruction + PseudoInstruction enums to not hold the oparg type.
Detailed Explanation
The current Instruction enum couples the opcode and its oparg type together. In practice, this creates friction and many boilerplate code that can be avoided.
Suggested API
New pure Opcode enum that will have only the mapping between the opcode name and it's ID, like this:
enum Opcode {
Nop = 0,
IsOp = 1,
BinaryOp = 2,
...
}
A new AnyOparg enum that will have all oparg types as it's variants:
enum AnyOparg {
NameIdx(oparg::NameIdx),
IntristicFunction1(oparg::IntristicFunction1),
Invert(oparg::Invert),
...
}
An Instruction struct that is like a typed version of the CodeUnit struct:
struct Instruction {
opcode: AnyOpcode,
oparg: AnyOparg,
}
impl Instruction {
pub fn new<T: Into<AnyOpcode>>(opcode: T, oparg: Option<i32>) -> Result<Self, MarshalError> {
let oparg = match opcode.into() {
AnyOpcode::Real(Opcode::Nop) => None,
AnyOpcode::Real(Opcode::IsOp) => Some(oparg::Invert::try_from(oparg.unwrap())?),
...
};
Ok(Self { opcode: opcode.into(), oparg: oparg.map(Into::into) })
}
}
Drawbacks, Rationale, and Alternatives
Drawbacks
- Will require a serious refactoring to the code
Rational
- Reduce boilerplate code (use
num_enumcrate for example). - Easier to auto-generate the opcodes (and their IDs) in the future.
- Simplify
opcode -> opargmapping.
Unresolved Questions
Performance implifications?
- Will probably increase performance because we will validate the oparg once instead of doing so every time we need to do an operation on the opcode.
Implementation & refactoring efforts?
- Ngôn ngữ chính
- Rust
- Star
- 22.4k
- Fork
- 1.5k
- Merge trung bình
- 15 giờ 18 phút
- Pull request đã merge (30 ngày)
- 172
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của RustPython/RustPython
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
RustPython/RustPython#8470 · 2 bình luận ·
-
C-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
RustPython/RustPython#7995 · 5 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
RustPython/RustPython#5577 ·
-
C-bug
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
RustPython/RustPython#8685 · 7 bình luận · 1 reaction ·
-
C-compat
RustPython/RustPython#8496 · 1 bình luận · 1 người được giao ·
Tất cả issue của RustPython/RustPython
Issue tương tự
-
Accept -c in more positions Đang mởarea: compat bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
area: dogs bug priority: P3 silent failure test-code
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
MystenLabs/sui#28056 · 1 bình luận ·
-
type/bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Luminous-Dynamics/mycelix#2577 ·