From adf1d61212b4a7f7a4d2fd7051dd6aafd2f83568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 17 Jul 2024 15:50:44 +0200 Subject: [PATCH] pyc: replace data in place This is in preparation for future changes. The code for 'struct Pyc' is put together in one place, separate from the more general code for PycParser. parser.clear_unused_flag_refs() modifies parser in place. A scratch copy of the data is still used, so that modifications can be discarder on failure. Returning a new copy of data would be awkward if we want to do further processing on that data. --- src/handlers/pyc.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/handlers/pyc.rs b/src/handlers/pyc.rs index 577cbb6..59666e5 100644 --- a/src/handlers/pyc.rs +++ b/src/handlers/pyc.rs @@ -296,16 +296,6 @@ pub fn pyc_python_version(buf: &[u8; 4]) -> Result<((u32, u32), usize)> { } } -pub struct Pyc { - config: Rc, -} - -impl Pyc { - pub fn boxed(config: &Rc) -> Box { - Box::new(Self { config: config.clone() }) - } -} - #[derive(Debug)] #[allow(dead_code)] // Right now, we only use dbg! to print the object. enum Object { @@ -697,7 +687,7 @@ impl PycParser { Ok(Object::Dict(dict)) } - fn clear_unused_flag_refs(&mut self) -> Result<(bool, Vec)> { + fn clear_unused_flag_refs(&mut self) -> Result { // Sequence of flag_refs and irefs ordered by number of byte in a file let final_list = iter::zip(self.flag_refs.iter(), iter::repeat(true)) @@ -743,7 +733,22 @@ impl PycParser { debug!("{}: removed {} unused FLAG_REFs", self.input_path.display(), removed_count); assert_eq!(data == self.data, removed_count == 0); - Ok((removed_count > 0, data)) + if removed_count > 0 { + self.data = data; + } + + Ok(removed_count > 0) + } +} + + +pub struct Pyc { + config: Rc, +} + +impl Pyc { + pub fn boxed(config: &Rc) -> Box { + Box::new(Self { config: config.clone() }) } } @@ -766,10 +771,10 @@ impl super::Processor for Pyc { parser.read_object()?; - let (have_mod, data) = parser.clear_unused_flag_refs()?; + let have_mod = parser.clear_unused_flag_refs()?; if have_mod { io.open_output()?; - io.output.as_mut().unwrap().write_all(&data)?; + io.output.as_mut().unwrap().write_all(&parser.data)?; } io.finalize(have_mod)