diff --git a/cargo-espflash/src/main.rs b/cargo-espflash/src/main.rs index 99cc881d..3be40061 100644 --- a/cargo-espflash/src/main.rs +++ b/cargo-espflash/src/main.rs @@ -171,7 +171,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { print_board_info(&mut flasher)?; if args.flash_args.ram { - flasher.load_elf_to_ram(&elf_data, Some(&mut EspflashProgress::default()))?; + flasher.load_elf_to_ram(&elf_data, EspflashProgress::default())?; } else { let bootloader = args .flash_args diff --git a/espflash/src/bin/espflash.rs b/espflash/src/bin/espflash.rs index 584bb9a9..e5928376 100644 --- a/espflash/src/bin/espflash.rs +++ b/espflash/src/bin/espflash.rs @@ -126,7 +126,7 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { let elf_data = fs::read(&args.image).into_diagnostic()?; if args.flash_args.ram { - flasher.load_elf_to_ram(&elf_data, Some(&mut EspflashProgress::default()))?; + flasher.load_elf_to_ram(&elf_data, EspflashProgress::default())?; } else { let bootloader = args.flash_args.bootloader.as_deref(); let partition_table = args.flash_args.partition_table.as_deref(); @@ -233,7 +233,7 @@ fn write_bin(args: WriteBinArgs, config: &Config) -> Result<()> { let mut buffer = Vec::with_capacity(size.try_into().into_diagnostic()?); f.read_to_end(&mut buffer).into_diagnostic()?; - flasher.write_bin_to_flash(args.addr, &buffer, Some(&mut EspflashProgress::default()))?; + flasher.write_bin_to_flash(args.addr, &buffer, EspflashProgress::default())?; Ok(()) } diff --git a/espflash/src/cli/mod.rs b/espflash/src/cli/mod.rs index 15fa1177..dfa090bf 100644 --- a/espflash/src/cli/mod.rs +++ b/espflash/src/cli/mod.rs @@ -468,7 +468,7 @@ pub fn flash_elf_image( flash_mode, flash_size, flash_freq, - Some(&mut EspflashProgress::default()), + EspflashProgress::default(), )?; info!("Flashing has completed!"); diff --git a/espflash/src/flasher/mod.rs b/espflash/src/flasher/mod.rs index 6b591437..3a547b0f 100644 --- a/espflash/src/flasher/mod.rs +++ b/espflash/src/flasher/mod.rs @@ -273,6 +273,17 @@ pub trait ProgressCallbacks { fn finish(&mut self); } +/// Progress update callbacks that do nothing. +pub struct NoProgress; + +impl ProgressCallbacks for NoProgress { + fn init(&mut self, _addr: u32, _total: usize) {} + + fn update(&mut self, _current: usize) {} + + fn finish(&mut self) {} +} + /// Connect to and flash a target device pub struct Flasher { /// Connection for flash operations @@ -362,7 +373,7 @@ impl Flasher { addr: text_addr, data: Cow::Borrowed(&text), }, - &mut None, + &mut NoProgress, ) .flashing()?; @@ -376,7 +387,7 @@ impl Flasher { addr: data_addr, data: Cow::Borrowed(&data), }, - &mut None, + &mut NoProgress, ) .flashing()?; @@ -618,7 +629,7 @@ impl Flasher { pub fn load_elf_to_ram( &mut self, elf_data: &[u8], - mut progress: Option<&mut dyn ProgressCallbacks>, + mut progress: impl ProgressCallbacks, ) -> Result<(), Error> { let image = ElfFirmwareImage::try_from(elf_data)?; if image.rom_segments(self.chip).next().is_some() { @@ -652,7 +663,7 @@ impl Flasher { flash_mode: Option, flash_size: Option, flash_freq: Option, - mut progress: Option<&mut dyn ProgressCallbacks>, + mut progress: impl ProgressCallbacks, ) -> Result<(), Error> { let image = ElfFirmwareImage::try_from(elf_data)?; @@ -694,7 +705,7 @@ impl Flasher { &mut self, addr: u32, data: &[u8], - mut progress: Option<&mut dyn ProgressCallbacks>, + mut progress: impl ProgressCallbacks, ) -> Result<(), Error> { let segment = RomSegment { addr, @@ -718,7 +729,7 @@ impl Flasher { flash_mode: Option, flash_size: Option, flash_freq: Option, - progress: Option<&mut dyn ProgressCallbacks>, + progress: impl ProgressCallbacks, ) -> Result<(), Error> { self.load_elf_to_flash_with_format( elf_data, diff --git a/espflash/src/targets/flash_target/esp32.rs b/espflash/src/targets/flash_target/esp32.rs index fe903647..19024e34 100644 --- a/espflash/src/targets/flash_target/esp32.rs +++ b/espflash/src/targets/flash_target/esp32.rs @@ -98,7 +98,7 @@ impl FlashTarget for Esp32Target { &mut self, connection: &mut Connection, segment: RomSegment, - progress: &mut Option<&mut dyn ProgressCallbacks>, + progress: &mut dyn ProgressCallbacks, ) -> Result<(), Error> { let addr = segment.addr; @@ -131,7 +131,7 @@ impl FlashTarget for Esp32Target { let chunks = compressed.chunks(flash_write_size); let num_chunks = chunks.len(); - progress.as_mut().map(|cb| cb.init(addr, num_chunks)); + progress.init(addr, num_chunks); // decode the chunks to see how much data the device will have to save let mut decoder = ZlibDecoder::new(Vec::new()); @@ -156,10 +156,10 @@ impl FlashTarget for Esp32Target { }, )?; - progress.as_mut().map(|cb| cb.update(i + 1)); + progress.update(i + 1); } - progress.as_mut().map(|cb| cb.finish()); + progress.finish(); Ok(()) } diff --git a/espflash/src/targets/flash_target/esp8266.rs b/espflash/src/targets/flash_target/esp8266.rs index c132fe78..a13f8ec1 100644 --- a/espflash/src/targets/flash_target/esp8266.rs +++ b/espflash/src/targets/flash_target/esp8266.rs @@ -34,7 +34,7 @@ impl FlashTarget for Esp8266Target { &mut self, connection: &mut Connection, segment: RomSegment, - progress: &mut Option<&mut dyn ProgressCallbacks>, + progress: &mut dyn ProgressCallbacks, ) -> Result<(), Error> { let addr = segment.addr; let block_count = (segment.data.len() + FLASH_WRITE_SIZE - 1) / FLASH_WRITE_SIZE; @@ -57,7 +57,7 @@ impl FlashTarget for Esp8266Target { let chunks = segment.data.chunks(FLASH_WRITE_SIZE); let num_chunks = chunks.len(); - progress.as_mut().map(|cb| cb.init(addr, num_chunks)); + progress.init(addr, num_chunks); for (i, block) in chunks.enumerate() { connection.command(Command::FlashData { @@ -67,10 +67,10 @@ impl FlashTarget for Esp8266Target { data: block, })?; - progress.as_mut().map(|cb| cb.update(i + 1)); + progress.update(i + 1); } - progress.as_mut().map(|cb| cb.finish()); + progress.finish(); Ok(()) } diff --git a/espflash/src/targets/flash_target/mod.rs b/espflash/src/targets/flash_target/mod.rs index 508ba62c..54f0a0ed 100644 --- a/espflash/src/targets/flash_target/mod.rs +++ b/espflash/src/targets/flash_target/mod.rs @@ -18,7 +18,7 @@ pub trait FlashTarget { &mut self, connection: &mut Connection, segment: RomSegment, - progress: &mut Option<&mut dyn ProgressCallbacks>, + progress: &mut dyn ProgressCallbacks, ) -> Result<(), Error>; /// Complete the flashing operation diff --git a/espflash/src/targets/flash_target/ram.rs b/espflash/src/targets/flash_target/ram.rs index 133a1858..5fbc70fc 100644 --- a/espflash/src/targets/flash_target/ram.rs +++ b/espflash/src/targets/flash_target/ram.rs @@ -45,7 +45,7 @@ impl FlashTarget for RamTarget { &mut self, connection: &mut Connection, segment: RomSegment, - progress: &mut Option<&mut dyn ProgressCallbacks>, + progress: &mut dyn ProgressCallbacks, ) -> Result<(), Error> { let addr = segment.addr; @@ -63,7 +63,7 @@ impl FlashTarget for RamTarget { let chunks = segment.data.chunks(self.block_size); let num_chunks = chunks.len(); - progress.as_mut().map(|cb| cb.init(addr, num_chunks)); + progress.init(addr, num_chunks); for (i, block) in chunks.enumerate() { connection.command(Command::MemData { @@ -73,10 +73,10 @@ impl FlashTarget for RamTarget { data: block, })?; - progress.as_mut().map(|cb| cb.update(i + 1)); + progress.update(i + 1); } - progress.as_mut().map(|cb| cb.finish()); + progress.finish(); Ok(()) }