@@ -2,10 +2,13 @@ use bitflags::bitflags;
22
33use core:: { fmt, num:: NonZeroU8 } ;
44
5- use crate :: bytecode:: { CodeUnit , instruction:: Instruction } ;
5+ use crate :: {
6+ bytecode:: { CodeUnit , instruction:: Instruction } ,
7+ marshal:: MarshalError ,
8+ } ;
69
710pub trait OpArgType : Copy {
8- fn from_op_arg ( x : u32 ) -> Option < Self > ;
11+ fn from_op_arg ( x : u32 ) -> Result < Self , MarshalError > ;
912
1013 fn to_op_arg ( self ) -> u32 ;
1114}
@@ -158,15 +161,15 @@ impl fmt::Display for ConvertValueOparg {
158161
159162impl OpArgType for ConvertValueOparg {
160163 #[ inline]
161- fn from_op_arg ( x : u32 ) -> Option < Self > {
162- Some ( match x {
164+ fn from_op_arg ( x : u32 ) -> Result < Self , MarshalError > {
165+ Ok ( match x {
163166 // Ruff `ConversionFlag::None` is `-1i8`,
164167 // when its converted to `u8` its value is `u8::MAX`
165168 0 | 255 => Self :: None ,
166169 1 => Self :: Str ,
167170 2 => Self :: Repr ,
168171 3 => Self :: Ascii ,
169- _ => return None ,
172+ _ => return Err ( MarshalError :: InvalidBytecode ) ,
170173 } )
171174 }
172175
@@ -188,8 +191,8 @@ pub enum ResumeType {
188191
189192impl OpArgType for u32 {
190193 #[ inline( always) ]
191- fn from_op_arg ( x : u32 ) -> Option < Self > {
192- Some ( x)
194+ fn from_op_arg ( x : u32 ) -> Result < Self , MarshalError > {
195+ Ok ( x)
193196 }
194197
195198 #[ inline( always) ]
@@ -200,8 +203,8 @@ impl OpArgType for u32 {
200203
201204impl OpArgType for bool {
202205 #[ inline( always) ]
203- fn from_op_arg ( x : u32 ) -> Option < Self > {
204- Some ( x != 0 )
206+ fn from_op_arg ( x : u32 ) -> Result < Self , MarshalError > {
207+ Ok ( x != 0 )
205208 }
206209
207210 #[ inline( always) ]
@@ -217,10 +220,10 @@ macro_rules! op_arg_enum_impl {
217220 self as u32
218221 }
219222
220- fn from_op_arg( x: u32 ) -> Option <Self > {
221- Some ( match u8 :: try_from( x) . ok ( ) ? {
223+ fn from_op_arg( x: u32 ) -> Result <Self , MarshalError > {
224+ Ok ( match u8 :: try_from( x) . map_err ( |_| MarshalError :: InvalidBytecode ) ? {
222225 $( $value => Self :: $var, ) *
223- _ => return None ,
226+ _ => return Err ( MarshalError :: InvalidBytecode ) ,
224227 } )
225228 }
226229 }
@@ -248,8 +251,8 @@ pub struct Label(pub u32);
248251
249252impl OpArgType for Label {
250253 #[ inline( always) ]
251- fn from_op_arg ( x : u32 ) -> Option < Self > {
252- Some ( Self ( x) )
254+ fn from_op_arg ( x : u32 ) -> Result < Self , MarshalError > {
255+ Ok ( Self ( x) )
253256 }
254257
255258 #[ inline( always) ]
@@ -341,8 +344,8 @@ bitflags! {
341344
342345impl OpArgType for MakeFunctionFlags {
343346 #[ inline( always) ]
344- fn from_op_arg ( x : u32 ) -> Option < Self > {
345- Self :: from_bits ( x as u8 )
347+ fn from_op_arg ( x : u32 ) -> Result < Self , MarshalError > {
348+ Self :: from_bits ( x as u8 ) . ok_or ( MarshalError :: InvalidBytecode )
346349 }
347350
348351 #[ inline( always) ]
@@ -601,11 +604,11 @@ pub enum BuildSliceArgCount {
601604
602605impl OpArgType for BuildSliceArgCount {
603606 #[ inline( always) ]
604- fn from_op_arg ( x : u32 ) -> Option < Self > {
605- Some ( match x {
607+ fn from_op_arg ( x : u32 ) -> Result < Self , MarshalError > {
608+ Ok ( match x {
606609 2 => Self :: Two ,
607610 3 => Self :: Three ,
608- _ => return None ,
611+ _ => return Err ( MarshalError :: InvalidBytecode ) ,
609612 } )
610613 }
611614
@@ -636,9 +639,9 @@ pub struct UnpackExArgs {
636639
637640impl OpArgType for UnpackExArgs {
638641 #[ inline( always) ]
639- fn from_op_arg ( x : u32 ) -> Option < Self > {
642+ fn from_op_arg ( x : u32 ) -> Result < Self , MarshalError > {
640643 let [ before, after, ..] = x. to_le_bytes ( ) ;
641- Some ( Self { before, after } )
644+ Ok ( Self { before, after } )
642645 }
643646
644647 #[ inline( always) ]
0 commit comments