@@ -15,8 +15,8 @@ use std::{collections::BTreeSet, fmt, hash};
1515/// Sourcecode location.
1616#[ derive( Clone , Copy , Debug , Default , PartialEq , Serialize , Deserialize ) ]
1717pub struct Location {
18- row : usize ,
19- column : usize ,
18+ row : u32 ,
19+ column : u32 ,
2020}
2121
2222impl Location {
@@ -28,17 +28,19 @@ impl Location {
2828 /// let loc = Location::new(10, 10);
2929 /// ```
3030 pub fn new ( row : usize , column : usize ) -> Self {
31+ let row = row. try_into ( ) . expect ( "Location::row over u32" ) ;
32+ let column = column. try_into ( ) . expect ( "Location::column over u32" ) ;
3133 Location { row, column }
3234 }
3335
3436 /// Current row
3537 pub fn row ( & self ) -> usize {
36- self . row
38+ self . row as usize
3739 }
3840
3941 /// Current column
4042 pub fn column ( & self ) -> usize {
41- self . column
43+ self . column as usize
4244 }
4345}
4446
@@ -47,22 +49,6 @@ pub trait Constant: Sized {
4749
4850 /// Transforms the given Constant to a BorrowedConstant
4951 fn borrow_constant ( & self ) -> BorrowedConstant < Self > ;
50- /// Get the data this Constant holds.
51- fn into_data ( self ) -> ConstantData {
52- self . borrow_constant ( ) . into_data ( )
53- }
54- /// Map this Constant to a Bag's constant
55- fn map_constant < Bag : ConstantBag > ( self , bag : & Bag ) -> Bag :: Constant {
56- bag. make_constant ( self . into_data ( ) )
57- }
58-
59- /// Maps the name for the given Bag.
60- fn map_name < Bag : ConstantBag > (
61- name : Self :: Name ,
62- bag : & Bag ,
63- ) -> <Bag :: Constant as Constant >:: Name {
64- bag. make_name_ref ( name. as_ref ( ) )
65- }
6652}
6753
6854impl Constant for ConstantData {
@@ -84,37 +70,25 @@ impl Constant for ConstantData {
8470 ConstantData :: Ellipsis => Ellipsis ,
8571 }
8672 }
87- fn into_data ( self ) -> ConstantData {
88- self
89- }
90- fn map_name < Bag : ConstantBag > ( name : String , bag : & Bag ) -> <Bag :: Constant as Constant >:: Name {
91- bag. make_name ( name)
92- }
9373}
9474
9575/// A Constant Bag
96- pub trait ConstantBag : Sized {
76+ pub trait ConstantBag : Sized + Copy {
9777 type Constant : Constant ;
98- fn make_constant ( & self , constant : ConstantData ) -> Self :: Constant ;
99- fn make_constant_borrowed < C : Constant > ( & self , constant : BorrowedConstant < C > ) -> Self :: Constant {
100- self . make_constant ( constant. into_data ( ) )
101- }
102- fn make_name ( & self , name : String ) -> <Self :: Constant as Constant >:: Name ;
103- fn make_name_ref ( & self , name : & str ) -> <Self :: Constant as Constant >:: Name {
104- self . make_name ( name. to_owned ( ) )
105- }
78+ fn make_constant < C : Constant > ( & self , constant : BorrowedConstant < C > ) -> Self :: Constant ;
79+ fn make_name ( & self , name : & str ) -> <Self :: Constant as Constant >:: Name ;
10680}
10781
108- #[ derive( Clone ) ]
82+ #[ derive( Clone , Copy ) ]
10983pub struct BasicBag ;
11084
11185impl ConstantBag for BasicBag {
11286 type Constant = ConstantData ;
113- fn make_constant ( & self , constant : ConstantData ) -> Self :: Constant {
114- constant
87+ fn make_constant < C : Constant > ( & self , constant : BorrowedConstant < C > ) -> Self :: Constant {
88+ constant. to_owned ( )
11589 }
116- fn make_name ( & self , name : String ) -> <Self :: Constant as Constant >:: Name {
117- name
90+ fn make_name ( & self , name : & str ) -> <Self :: Constant as Constant >:: Name {
91+ name. to_owned ( )
11892 }
11993}
12094
@@ -556,7 +530,7 @@ impl<C: Constant> BorrowedConstant<'_, C> {
556530 BorrowedConstant :: Ellipsis => write ! ( f, "..." ) ,
557531 }
558532 }
559- pub fn into_data ( self ) -> ConstantData {
533+ pub fn to_owned ( self ) -> ConstantData {
560534 use ConstantData :: * ;
561535 match self {
562536 BorrowedConstant :: Integer { value } => Integer {
@@ -575,7 +549,7 @@ impl<C: Constant> BorrowedConstant<'_, C> {
575549 code : Box :: new ( code. map_clone_bag ( & BasicBag ) ) ,
576550 } ,
577551 BorrowedConstant :: Tuple { elements } => Tuple {
578- elements : elements. map ( BorrowedConstant :: into_data ) . collect ( ) ,
552+ elements : elements. map ( BorrowedConstant :: to_owned ) . collect ( ) ,
579553 } ,
580554 BorrowedConstant :: None => None ,
581555 BorrowedConstant :: Ellipsis => Ellipsis ,
@@ -762,27 +736,27 @@ impl<C: Constant> CodeObject<C> {
762736 }
763737
764738 /// Map this CodeObject to one that holds a Bag::Constant
765- pub fn map_bag < Bag : ConstantBag > ( self , bag : & Bag ) -> CodeObject < Bag :: Constant > {
739+ pub fn map_bag < Bag : ConstantBag > ( self , bag : Bag ) -> CodeObject < Bag :: Constant > {
766740 let map_names = |names : Box < [ C :: Name ] > | {
767741 names
768742 . into_vec ( )
769743 . into_iter ( )
770- . map ( |x| C :: map_name ( x , bag ) )
744+ . map ( |x| bag . make_name ( x . as_ref ( ) ) )
771745 . collect :: < Box < [ _ ] > > ( )
772746 } ;
773747 CodeObject {
774748 constants : self
775749 . constants
776750 . into_vec ( )
777751 . into_iter ( )
778- . map ( |x| x . map_constant ( bag ) )
752+ . map ( |x| bag . make_constant ( x . borrow_constant ( ) ) )
779753 . collect ( ) ,
780754 names : map_names ( self . names ) ,
781755 varnames : map_names ( self . varnames ) ,
782756 cellvars : map_names ( self . cellvars ) ,
783757 freevars : map_names ( self . freevars ) ,
784- source_path : C :: map_name ( self . source_path , bag ) ,
785- obj_name : C :: map_name ( self . obj_name , bag ) ,
758+ source_path : bag . make_name ( self . source_path . as_ref ( ) ) ,
759+ obj_name : bag . make_name ( self . obj_name . as_ref ( ) ) ,
786760
787761 instructions : self . instructions ,
788762 locations : self . locations ,
@@ -798,24 +772,20 @@ impl<C: Constant> CodeObject<C> {
798772
799773 /// Same as `map_bag` but clones `self`
800774 pub fn map_clone_bag < Bag : ConstantBag > ( & self , bag : & Bag ) -> CodeObject < Bag :: Constant > {
801- let map_names = |names : & [ C :: Name ] | {
802- names
803- . iter ( )
804- . map ( |x| bag. make_name_ref ( x. as_ref ( ) ) )
805- . collect ( )
806- } ;
775+ let map_names =
776+ |names : & [ C :: Name ] | names. iter ( ) . map ( |x| bag. make_name ( x. as_ref ( ) ) ) . collect ( ) ;
807777 CodeObject {
808778 constants : self
809779 . constants
810780 . iter ( )
811- . map ( |x| bag. make_constant_borrowed ( x. borrow_constant ( ) ) )
781+ . map ( |x| bag. make_constant ( x. borrow_constant ( ) ) )
812782 . collect ( ) ,
813783 names : map_names ( & self . names ) ,
814784 varnames : map_names ( & self . varnames ) ,
815785 cellvars : map_names ( & self . cellvars ) ,
816786 freevars : map_names ( & self . freevars ) ,
817- source_path : bag. make_name_ref ( self . source_path . as_ref ( ) ) ,
818- obj_name : bag. make_name_ref ( self . obj_name . as_ref ( ) ) ,
787+ source_path : bag. make_name ( self . source_path . as_ref ( ) ) ,
788+ obj_name : bag. make_name ( self . obj_name . as_ref ( ) ) ,
819789
820790 instructions : self . instructions . clone ( ) ,
821791 locations : self . locations . clone ( ) ,
@@ -1254,12 +1224,8 @@ impl<C: Constant> fmt::Debug for CodeObject<C> {
12541224
12551225/// A frozen module. Holds a code object and whether it is part of a package
12561226#[ derive( Serialize , Deserialize , Debug ) ]
1257- pub struct FrozenModule < C : Constant = ConstantData > {
1258- #[ serde( bound(
1259- deserialize = "C: serde::Deserialize<'de>, C::Name: serde::Deserialize<'de>" ,
1260- serialize = "C: serde::Serialize, C::Name: serde::Serialize"
1261- ) ) ]
1262- pub code : CodeObject < C > ,
1227+ pub struct FrozenModule {
1228+ pub code : CodeObject < ConstantData > ,
12631229 pub package : bool ,
12641230}
12651231
0 commit comments