diff --git a/bytecode/src/lib.rs b/bytecode/src/lib.rs index cc602957d..f052719a0 100644 --- a/bytecode/src/lib.rs +++ b/bytecode/src/lib.rs @@ -49,7 +49,7 @@ pub trait Constant: Sized { fn borrow_constant(&self) -> BorrowedConstant; /// Map this Constant to a Bag's constant fn map_constant(self, bag: &Bag) -> Bag::Constant { - bag.make_constant(self.borrow_constant().to_owned()) + bag.make_constant(self.borrow_constant()) } /// Maps the name for the given Bag. @@ -88,10 +88,7 @@ impl Constant for ConstantData { /// A Constant Bag pub trait ConstantBag: Sized { type Constant: Constant; - fn make_constant(&self, constant: ConstantData) -> Self::Constant; - fn make_constant_borrowed(&self, constant: BorrowedConstant) -> Self::Constant { - self.make_constant(constant.to_owned()) - } + fn make_constant(&self, constant: BorrowedConstant) -> Self::Constant; fn make_name(&self, name: String) -> ::Name; fn make_name_ref(&self, name: &str) -> ::Name { self.make_name(name.to_owned()) @@ -103,8 +100,8 @@ pub struct BasicBag; impl ConstantBag for BasicBag { type Constant = ConstantData; - fn make_constant(&self, constant: ConstantData) -> Self::Constant { - constant + fn make_constant(&self, constant: BorrowedConstant) -> Self::Constant { + constant.to_owned() } fn make_name(&self, name: String) -> ::Name { name @@ -801,7 +798,7 @@ impl CodeObject { constants: self .constants .iter() - .map(|x| bag.make_constant_borrowed(x.borrow_constant())) + .map(|x| bag.make_constant(x.borrow_constant())) .collect(), names: map_names(&self.names), varnames: map_names(&self.varnames), diff --git a/vm/src/builtins/code.rs b/vm/src/builtins/code.rs index 9932ff216..67476ff34 100644 --- a/vm/src/builtins/code.rs +++ b/vm/src/builtins/code.rs @@ -67,7 +67,7 @@ impl Constant for PyConstant { borrow_obj_constant(&self.0) } fn map_constant(self, bag: &Bag) -> Bag::Constant { - bag.make_constant_borrowed(self.borrow_constant()) + bag.make_constant(self.borrow_constant()) } } @@ -75,32 +75,8 @@ pub(crate) struct PyObjBag<'a>(pub &'a Context); impl ConstantBag for PyObjBag<'_> { type Constant = PyConstant; - fn make_constant(&self, constant: bytecode::ConstantData) -> Self::Constant { - let ctx = self.0; - let obj = match constant { - bytecode::ConstantData::Integer { value } => ctx.new_int(value).into(), - bytecode::ConstantData::Float { value } => ctx.new_float(value).into(), - bytecode::ConstantData::Complex { value } => ctx.new_complex(value).into(), - bytecode::ConstantData::Str { value } if value.len() <= 20 => { - ctx.intern_string(value).into_pyref().into() - } - bytecode::ConstantData::Str { value } => ctx.new_str(value).into(), - bytecode::ConstantData::Bytes { value } => ctx.new_bytes(value.to_vec()).into(), - bytecode::ConstantData::Boolean { value } => ctx.new_bool(value).into(), - bytecode::ConstantData::Code { code } => ctx.new_code(code.map_bag(self)).into(), - bytecode::ConstantData::Tuple { elements } => { - let elements = elements - .into_iter() - .map(|constant| self.make_constant(constant).0) - .collect(); - ctx.new_tuple(elements).into() - } - bytecode::ConstantData::None => ctx.none(), - bytecode::ConstantData::Ellipsis => ctx.ellipsis(), - }; - PyConstant(obj) - } - fn make_constant_borrowed(&self, constant: BorrowedConstant) -> Self::Constant { + + fn make_constant(&self, constant: BorrowedConstant) -> Self::Constant { let ctx = self.0; let obj = match constant { bytecode::BorrowedConstant::Integer { value } => ctx.new_bigint(value).into(), @@ -118,7 +94,7 @@ impl ConstantBag for PyObjBag<'_> { bytecode::BorrowedConstant::Tuple { elements } => { let elements = elements .into_iter() - .map(|constant| self.make_constant_borrowed(constant).0) + .map(|constant| self.make_constant(constant).0) .collect(); ctx.new_tuple(elements).into() }