Skip to content

Commit

Permalink
Replace optional with a null object
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Jan 11, 2023
1 parent 421a8dc commit 3d61fce
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 25 deletions.
4 changes: 2 additions & 2 deletions espflash/src/bin/espflash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &mut EspflashProgress::default())?;
} else {
let bootloader = args.flash_args.bootloader.as_deref();
let partition_table = args.flash_args.partition_table.as_deref();
Expand Down Expand Up @@ -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, &mut EspflashProgress::default())?;

Ok(())
}
2 changes: 1 addition & 1 deletion espflash/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ pub fn flash_elf_image(
flash_mode,
flash_size,
flash_freq,
Some(&mut EspflashProgress::default()),
&mut EspflashProgress::default(),
)?;
info!("Flashing has completed!");

Expand Down
29 changes: 20 additions & 9 deletions espflash/src/flasher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -362,7 +373,7 @@ impl Flasher {
addr: text_addr,
data: Cow::Borrowed(&text),
},
&mut None,
&mut NoProgress,
)
.flashing()?;

Expand All @@ -376,7 +387,7 @@ impl Flasher {
addr: data_addr,
data: Cow::Borrowed(&data),
},
&mut None,
&mut NoProgress,
)
.flashing()?;

Expand Down Expand Up @@ -618,7 +629,7 @@ impl Flasher {
pub fn load_elf_to_ram(
&mut self,
elf_data: &[u8],
mut progress: Option<&mut dyn ProgressCallbacks>,
progress: &mut dyn ProgressCallbacks,
) -> Result<(), Error> {
let image = ElfFirmwareImage::try_from(elf_data)?;
if image.rom_segments(self.chip).next().is_some() {
Expand All @@ -635,7 +646,7 @@ impl Flasher {

for segment in image.ram_segments(self.chip) {
target
.write_segment(&mut self.connection, segment.into(), &mut progress)
.write_segment(&mut self.connection, segment.into(), progress)
.flashing()?;
}

Expand All @@ -652,7 +663,7 @@ impl Flasher {
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
flash_freq: Option<FlashFrequency>,
mut progress: Option<&mut dyn ProgressCallbacks>,
progress: &mut dyn ProgressCallbacks,
) -> Result<(), Error> {
let image = ElfFirmwareImage::try_from(elf_data)?;

Expand Down Expand Up @@ -680,7 +691,7 @@ impl Flasher {

for segment in image.flash_segments() {
target
.write_segment(&mut self.connection, segment, &mut progress)
.write_segment(&mut self.connection, segment, progress)
.flashing()?;
}

Expand All @@ -694,7 +705,7 @@ impl Flasher {
&mut self,
addr: u32,
data: &[u8],
mut progress: Option<&mut dyn ProgressCallbacks>,
progress: &mut dyn ProgressCallbacks,
) -> Result<(), Error> {
let segment = RomSegment {
addr,
Expand All @@ -703,7 +714,7 @@ impl Flasher {

let mut target = self.chip.flash_target(self.spi_params, self.use_stub);
target.begin(&mut self.connection).flashing()?;
target.write_segment(&mut self.connection, segment, &mut progress)?;
target.write_segment(&mut self.connection, segment, progress)?;
target.finish(&mut self.connection, true).flashing()?;

Ok(())
Expand All @@ -718,7 +729,7 @@ impl Flasher {
flash_mode: Option<FlashMode>,
flash_size: Option<FlashSize>,
flash_freq: Option<FlashFrequency>,
progress: Option<&mut dyn ProgressCallbacks>,
progress: &mut dyn ProgressCallbacks,
) -> Result<(), Error> {
self.load_elf_to_flash_with_format(
elf_data,
Expand Down
8 changes: 4 additions & 4 deletions espflash/src/targets/flash_target/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand All @@ -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(())
}
Expand Down
8 changes: 4 additions & 4 deletions espflash/src/targets/flash_target/esp8266.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion espflash/src/targets/flash_target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions espflash/src/targets/flash_target/ram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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(())
}
Expand Down

0 comments on commit 3d61fce

Please sign in to comment.