Skip to content

Commit abe644b

Browse files
committed
remove make_constant_borrowed
1 parent ec7a709 commit abe644b

2 files changed

Lines changed: 9 additions & 36 deletions

File tree

bytecode/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub trait Constant: Sized {
4949
fn borrow_constant(&self) -> BorrowedConstant<Self>;
5050
/// Map this Constant to a Bag's constant
5151
fn map_constant<Bag: ConstantBag>(self, bag: &Bag) -> Bag::Constant {
52-
bag.make_constant(self.borrow_constant().to_owned())
52+
bag.make_constant(self.borrow_constant())
5353
}
5454

5555
/// Maps the name for the given Bag.
@@ -88,10 +88,7 @@ impl Constant for ConstantData {
8888
/// A Constant Bag
8989
pub trait ConstantBag: Sized {
9090
type Constant: Constant;
91-
fn make_constant(&self, constant: ConstantData) -> Self::Constant;
92-
fn make_constant_borrowed<C: Constant>(&self, constant: BorrowedConstant<C>) -> Self::Constant {
93-
self.make_constant(constant.to_owned())
94-
}
91+
fn make_constant<C: Constant>(&self, constant: BorrowedConstant<C>) -> Self::Constant;
9592
fn make_name(&self, name: String) -> <Self::Constant as Constant>::Name;
9693
fn make_name_ref(&self, name: &str) -> <Self::Constant as Constant>::Name {
9794
self.make_name(name.to_owned())
@@ -103,8 +100,8 @@ pub struct BasicBag;
103100

104101
impl ConstantBag for BasicBag {
105102
type Constant = ConstantData;
106-
fn make_constant(&self, constant: ConstantData) -> Self::Constant {
107-
constant
103+
fn make_constant<C: Constant>(&self, constant: BorrowedConstant<C>) -> Self::Constant {
104+
constant.to_owned()
108105
}
109106
fn make_name(&self, name: String) -> <Self::Constant as Constant>::Name {
110107
name
@@ -801,7 +798,7 @@ impl<C: Constant> CodeObject<C> {
801798
constants: self
802799
.constants
803800
.iter()
804-
.map(|x| bag.make_constant_borrowed(x.borrow_constant()))
801+
.map(|x| bag.make_constant(x.borrow_constant()))
805802
.collect(),
806803
names: map_names(&self.names),
807804
varnames: map_names(&self.varnames),

vm/src/builtins/code.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,40 +67,16 @@ impl Constant for PyConstant {
6767
borrow_obj_constant(&self.0)
6868
}
6969
fn map_constant<Bag: ConstantBag>(self, bag: &Bag) -> Bag::Constant {
70-
bag.make_constant_borrowed(self.borrow_constant())
70+
bag.make_constant(self.borrow_constant())
7171
}
7272
}
7373

7474
pub(crate) struct PyObjBag<'a>(pub &'a Context);
7575

7676
impl ConstantBag for PyObjBag<'_> {
7777
type Constant = PyConstant;
78-
fn make_constant(&self, constant: bytecode::ConstantData) -> Self::Constant {
79-
let ctx = self.0;
80-
let obj = match constant {
81-
bytecode::ConstantData::Integer { value } => ctx.new_int(value).into(),
82-
bytecode::ConstantData::Float { value } => ctx.new_float(value).into(),
83-
bytecode::ConstantData::Complex { value } => ctx.new_complex(value).into(),
84-
bytecode::ConstantData::Str { value } if value.len() <= 20 => {
85-
ctx.intern_string(value).into_pyref().into()
86-
}
87-
bytecode::ConstantData::Str { value } => ctx.new_str(value).into(),
88-
bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()).into(),
89-
bytecode::ConstantData::Boolean { value } => ctx.new_bool(value).into(),
90-
bytecode::ConstantData::Code { code } => ctx.new_code(code.map_bag(self)).into(),
91-
bytecode::ConstantData::Tuple { elements } => {
92-
let elements = elements
93-
.into_iter()
94-
.map(|constant| self.make_constant(constant).0)
95-
.collect();
96-
ctx.new_tuple(elements).into()
97-
}
98-
bytecode::ConstantData::None => ctx.none(),
99-
bytecode::ConstantData::Ellipsis => ctx.ellipsis(),
100-
};
101-
PyConstant(obj)
102-
}
103-
fn make_constant_borrowed<C: Constant>(&self, constant: BorrowedConstant<C>) -> Self::Constant {
78+
79+
fn make_constant<C: Constant>(&self, constant: BorrowedConstant<C>) -> Self::Constant {
10480
let ctx = self.0;
10581
let obj = match constant {
10682
bytecode::BorrowedConstant::Integer { value } => ctx.new_bigint(value).into(),
@@ -118,7 +94,7 @@ impl ConstantBag for PyObjBag<'_> {
11894
bytecode::BorrowedConstant::Tuple { elements } => {
11995
let elements = elements
12096
.into_iter()
121-
.map(|constant| self.make_constant_borrowed(constant).0)
97+
.map(|constant| self.make_constant(constant).0)
12298
.collect();
12399
ctx.new_tuple(elements).into()
124100
}

0 commit comments

Comments
 (0)