Skip to content

Commit

Permalink
Clean up remove map_basic
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Oct 18, 2020
1 parent 2cc76c2 commit 68de4aa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
19 changes: 9 additions & 10 deletions bytecode/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub trait Constant: Sized {
fn into_data(self) -> ConstantData {
self.borrow_constant().into_data()
}
fn map_constant<Bag: ConstantBag>(self, bag: &Bag) -> Bag::Constant {
bag.make_constant(self.into_data())
}
}
impl Constant for ConstantData {
fn borrow_constant(&self) -> BorrowedConstant<Self> {
Expand Down Expand Up @@ -591,9 +594,13 @@ impl<C: Constant> CodeObject<C> {
Display(self)
}

fn _map_inner<U: Constant>(self, map: impl Fn(C) -> U) -> CodeObject<U> {
pub fn map_bag<Bag: ConstantBag>(self, bag: &Bag) -> CodeObject<Bag::Constant> {
CodeObject {
constants: self.constants.into_iter().map(map).collect(),
constants: self
.constants
.into_iter()
.map(|x| x.map_constant(bag))
.collect(),

instructions: self.instructions,
label_map: self.label_map,
Expand All @@ -610,10 +617,6 @@ impl<C: Constant> CodeObject<C> {
}
}

pub fn map_bag<Bag: ConstantBag>(self, bag: &Bag) -> CodeObject<Bag::Constant> {
self._map_inner(|x| bag.make_constant_borrowed(x.borrow_constant()))
}

pub fn map_clone_bag<Bag: ConstantBag>(&self, bag: &Bag) -> CodeObject<Bag::Constant> {
CodeObject {
constants: self
Expand Down Expand Up @@ -651,10 +654,6 @@ impl CodeObject<ConstantData> {
let data = bincode::serialize(&self).expect("Code object must be serializable");
lz4_compression::compress::compress(&data)
}

pub fn map_basic<Bag: ConstantBag>(self, bag: &Bag) -> CodeObject<Bag::Constant> {
self._map_inner(|x| bag.make_constant(x))
}
}

impl<C: Constant> fmt::Display for CodeObject<C> {
Expand Down
3 changes: 3 additions & 0 deletions vm/src/builtins/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ impl Constant for PyConstant {
fn borrow_constant(&self) -> BorrowedConstant<Self> {
borrow_obj_constant(&self.0)
}
fn map_constant<Bag: ConstantBag>(self, bag: &Bag) -> Bag::Constant {
bag.make_constant_borrowed(self.borrow_constant())
}
}

pub(crate) struct PyObjBag<'a>(pub &'a PyContext);
Expand Down
2 changes: 1 addition & 1 deletion vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ impl PyContext {
}

pub fn map_codeobj(&self, code: bytecode::CodeObject) -> code::CodeObject {
code.map_basic(&code::PyObjBag(self))
code.map_bag(&code::PyObjBag(self))
}

pub fn add_tp_new_wrapper(&self, ty: &PyTypeRef) {
Expand Down

0 comments on commit 68de4aa

Please sign in to comment.