Address review comments

This commit is contained in:
Noa 2023-03-01 21:05:59 -06:00
parent 41b465dee1
commit 461ef3d71d

View file

@ -218,6 +218,27 @@ impl OpArgType for bool {
} }
} }
macro_rules! oparg_enum {
($(#[$attr:meta])* $vis:vis enum $name:ident { $($(#[$var_attr:meta])* $var:ident = $discr:literal,)* }) => {
$(#[$attr])*
$vis enum $name {
$($(#[$var_attr])* $var = $discr,)*
}
impl OpArgType for $name {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match u8::try_from(x).ok()? {
$($discr => Self::$var,)*
_ => return None,
})
}
}
};
}
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct Arg<T: OpArgType>(PhantomData<T>); pub struct Arg<T: OpArgType>(PhantomData<T>);
@ -292,6 +313,7 @@ impl fmt::Display for Label {
} }
} }
oparg_enum!(
/// Transforms a value prior to formatting it. /// Transforms a value prior to formatting it.
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
@ -305,21 +327,7 @@ pub enum ConversionFlag {
/// Converts by calling `repr(<value>)`. /// Converts by calling `repr(<value>)`.
Repr = b'r', Repr = b'r',
} }
);
impl OpArgType for ConversionFlag {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match u8::try_from(x).ok()? {
0 => Self::None,
b's' => Self::Str,
b'a' => Self::Ascii,
b'r' => Self::Repr,
_ => return None,
})
}
}
impl TryFrom<usize> for ConversionFlag { impl TryFrom<usize> for ConversionFlag {
type Error = usize; type Error = usize;
@ -328,28 +336,16 @@ impl TryFrom<usize> for ConversionFlag {
} }
} }
oparg_enum!(
/// The kind of Raise that occurred. /// The kind of Raise that occurred.
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
pub enum RaiseKind { pub enum RaiseKind {
Reraise, Reraise = 0,
Raise, Raise = 1,
RaiseCause, RaiseCause = 2,
}
impl OpArgType for RaiseKind {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match x {
0 => Self::Reraise,
1 => Self::Raise,
2 => Self::RaiseCause,
_ => return None,
})
}
} }
);
pub type NameIdx = u32; pub type NameIdx = u32;
@ -785,6 +781,7 @@ impl<C: Constant> BorrowedConstant<'_, C> {
} }
} }
oparg_enum!(
/// The possible comparison operators /// The possible comparison operators
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
@ -798,51 +795,22 @@ pub enum ComparisonOperator {
LessOrEqual = 0b101, LessOrEqual = 0b101,
GreaterOrEqual = 0b110, GreaterOrEqual = 0b110,
} }
);
impl OpArgType for ComparisonOperator { oparg_enum!(
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match x {
0b001 => Self::Less,
0b010 => Self::Greater,
0b011 => Self::NotEqual,
0b100 => Self::Equal,
0b101 => Self::LessOrEqual,
0b110 => Self::GreaterOrEqual,
_ => return None,
})
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
pub enum TestOperator { pub enum TestOperator {
In, In = 0,
NotIn, NotIn = 1,
Is, Is = 2,
IsNot, IsNot = 3,
/// two exceptions that match? /// two exceptions that match?
ExceptionMatch, ExceptionMatch = 4,
}
impl OpArgType for TestOperator {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match x {
0 => Self::In,
1 => Self::NotIn,
2 => Self::Is,
3 => Self::IsNot,
4 => Self::ExceptionMatch,
_ => return None,
})
}
} }
);
oparg_enum!(
/// The possible Binary operators /// The possible Binary operators
/// # Examples /// # Examples
/// ///
@ -854,69 +822,33 @@ impl OpArgType for TestOperator {
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
pub enum BinaryOperator { pub enum BinaryOperator {
Power, Power = 0,
Multiply, Multiply = 1,
MatrixMultiply, MatrixMultiply = 2,
Divide, Divide = 3,
FloorDivide, FloorDivide = 4,
Modulo, Modulo = 5,
Add, Add = 6,
Subtract, Subtract = 7,
Lshift, Lshift = 8,
Rshift, Rshift = 9,
And, And = 10,
Xor, Xor = 11,
Or, Or = 12,
}
impl OpArgType for BinaryOperator {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match x {
0 => Self::Power,
1 => Self::Multiply,
2 => Self::MatrixMultiply,
3 => Self::Divide,
4 => Self::FloorDivide,
5 => Self::Modulo,
6 => Self::Add,
7 => Self::Subtract,
8 => Self::Lshift,
9 => Self::Rshift,
10 => Self::And,
11 => Self::Xor,
12 => Self::Or,
_ => return None,
})
}
} }
);
oparg_enum!(
/// The possible unary operators /// The possible unary operators
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
pub enum UnaryOperator { pub enum UnaryOperator {
Not, Not = 0,
Invert, Invert = 1,
Minus, Minus = 2,
Plus, Plus = 3,
}
impl OpArgType for UnaryOperator {
fn to_oparg(self) -> u32 {
self as u32
}
fn from_oparg(x: u32) -> Option<Self> {
Some(match x {
0 => Self::Not,
1 => Self::Invert,
2 => Self::Minus,
3 => Self::Plus,
_ => return None,
})
}
} }
);
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub struct UnpackExArgs { pub struct UnpackExArgs {