@@ -11,7 +11,7 @@ use bitflags::bitflags;
1111use core:: {
1212 cell:: UnsafeCell ,
1313 hash, mem,
14- ops:: Deref ,
14+ ops:: { Deref , Index } ,
1515 sync:: atomic:: { AtomicU8 , AtomicU16 , AtomicUsize , Ordering } ,
1616} ;
1717use itertools:: Itertools ;
@@ -32,7 +32,7 @@ pub use crate::bytecode::{
3232} ;
3333
3434mod instruction;
35- mod oparg;
35+ pub mod oparg;
3636
3737/// Exception table entry for zero-cost exception handling
3838/// Format: (start, size, target, depth<<1|lasti)
@@ -293,6 +293,31 @@ impl ConstantBag for BasicBag {
293293 }
294294}
295295
296+ #[ derive( Clone ) ]
297+ pub struct Constants < C : Constant > ( Box < [ C ] > ) ;
298+
299+ impl < C : Constant > Deref for Constants < C > {
300+ type Target = [ C ] ;
301+
302+ fn deref ( & self ) -> & Self :: Target {
303+ & self . 0
304+ }
305+ }
306+
307+ impl < C : Constant > Index < oparg:: ConstIdx > for Constants < C > {
308+ type Output = C ;
309+
310+ fn index ( & self , consti : oparg:: ConstIdx ) -> & Self :: Output {
311+ & self . 0 [ consti. as_usize ( ) ]
312+ }
313+ }
314+
315+ impl < C : Constant > FromIterator < C > for Constants < C > {
316+ fn from_iter < T : IntoIterator < Item = C > > ( iter : T ) -> Self {
317+ Self ( iter. into_iter ( ) . collect ( ) )
318+ }
319+ }
320+
296321/// Primary container of a single code object. Each python function has
297322/// a code object. Also a module has a code object.
298323#[ derive( Clone ) ]
@@ -312,7 +337,7 @@ pub struct CodeObject<C: Constant = ConstantData> {
312337 /// Qualified name of the object (like CPython's co_qualname)
313338 pub qualname : C :: Name ,
314339 pub cell2arg : Option < Box < [ i32 ] > > ,
315- pub constants : Box < [ C ] > ,
340+ pub constants : Constants < C > ,
316341 pub names : Box < [ C :: Name ] > ,
317342 pub varnames : Box < [ C :: Name ] > ,
318343 pub cellvars : Box < [ C :: Name ] > ,
@@ -1020,8 +1045,7 @@ impl<C: Constant> CodeObject<C> {
10201045 CodeObject {
10211046 constants : self
10221047 . constants
1023- . into_vec ( )
1024- . into_iter ( )
1048+ . iter ( )
10251049 . map ( |x| bag. make_constant ( x. borrow_constant ( ) ) )
10261050 . collect ( ) ,
10271051 names : map_names ( self . names ) ,
@@ -1095,7 +1119,7 @@ impl<C: Constant> fmt::Display for CodeObject<C> {
10951119pub trait InstrDisplayContext {
10961120 type Constant : Constant ;
10971121
1098- fn get_constant ( & self , i : usize ) -> & Self :: Constant ;
1122+ fn get_constant ( & self , consti : oparg :: ConstIdx ) -> & Self :: Constant ;
10991123
11001124 fn get_name ( & self , i : usize ) -> & str ;
11011125
@@ -1107,8 +1131,8 @@ pub trait InstrDisplayContext {
11071131impl < C : Constant > InstrDisplayContext for CodeObject < C > {
11081132 type Constant = C ;
11091133
1110- fn get_constant ( & self , i : usize ) -> & C {
1111- & self . constants [ i ]
1134+ fn get_constant ( & self , consti : oparg :: ConstIdx ) -> & C {
1135+ & self . constants [ consti ]
11121136 }
11131137
11141138 fn get_name ( & self , i : usize ) -> & str {
0 commit comments