refactor(core): make FuelGauge soc non-optional
What changed, and why it matters
This is a small internal code cleanup in the Trezor firmware's battery indicator. It changes the battery charge level from an optional value (which could be missing) to a regular number that defaults to 0. There is no security issue visible in the change.
No security action needed. Treat as a normal refactoring review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors FuelGauge in the Eckhart UI layout so that the soc (state-of-charge) field is u8 instead of Option<u8>. It removes None/Some handling and replaces unwrap_or(0) with direct use of the value. The behavior is functionally equivalent because the previous code already defaulted to 0 when the value was missing. No security-sensitive logic is introduced or altered.
Changed components
core/embed/rust/src/ui/layout_eckhart/component/fuel_gauge.rsInspect captured patch +7 / −10
diff --git a/core/embed/rust/src/ui/layout_eckhart/component/fuel_gauge.rs b/core/embed/rust/src/ui/layout_eckhart/component/fuel_gauge.rs
index 8eb63a5c..eeb67495 100644
--- a/core/embed/rust/src/ui/layout_eckhart/component/fuel_gauge.rs
+++ b/core/embed/rust/src/ui/layout_eckhart/component/fuel_gauge.rs
@@ -37,7 +37,7 @@ pub struct FuelGauge {
/// State of battery charging
charging_state: ChargingState,
/// State of charge (0-100) [%]
- soc: Option<u8>,
+ soc: u8,
/// Cached formatted SOC text
soc_text: ShortString,
/// Font used for the soc percentage
@@ -79,11 +79,10 @@ impl FuelGauge {
}
pub fn update_pm_state(&mut self) {
- self.soc = Some(power_manager::soc());
+ self.soc = power_manager::soc();
self.charging_state = power_manager::charging_state();
- self.soc_text = uformat!("{} %", self.soc.unwrap_or(0));
- self.battery_indication =
- self.battery_indication(self.charging_state, self.soc.unwrap_or(0));
+ self.soc_text = uformat!("{} %", self.soc);
+ self.battery_indication = self.battery_indication(self.charging_state, self.soc);
}
pub fn should_be_shown(&self) -> bool {
@@ -122,7 +121,7 @@ impl FuelGauge {
area: Rect::zero(),
mode,
charging_state: ChargingState::Idle,
- soc: None,
+ soc: 0,
soc_text: ShortString::new(),
font,
battery_indication: (ICON_BATTERY_EMPTY, GREY_LIGHT, GREY_LIGHT),
@@ -177,9 +176,7 @@ impl Component for FuelGauge {
fn event(&mut self, ctx: &mut EventCtx, event: Event) -> Option<Self::Msg> {
match event {
Event::Attach(_) => {
- if self.soc.is_none() {
- self.update_pm_state();
- }
+ self.update_pm_state();
ctx.request_paint();
}
Event::PM(_e) => {
@@ -258,7 +255,7 @@ impl Component for FuelGauge {
impl crate::trace::Trace for FuelGauge {
fn trace(&self, t: &mut dyn crate::trace::Tracer) {
t.component("FuelGauge");
- t.int("soc", self.soc.unwrap_or(0) as i64);
+ t.int("soc", self.soc as i64);
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.