Explain runtime invariants and avoid redundant unwraps

This commit is contained in:
Samuel Onoja committed 2026-10-22 01:28:03 +01:00
1 parent 34f2f07301
commit 1b51ba83b4
116 files changed
+1643 -515

No files matched your search

+5 -1
View File
@@ -336,7 +336,11 @@ impl FereseShell {
presentation.rows = rows;
continue;
}
let runtime = output.runtime.hub.as_ref().unwrap();
let runtime = output
.runtime
.hub
.as_ref()
.expect("this activity action belongs to an output with a Morph hub");
let name = self
.output_for_bar(output.bar)
.map(|output| output.name.as_str())
+5 -1
View File
@@ -67,7 +67,11 @@ impl Compact {
+ count as f32 * (ICON + TEXT_GAP)
+ count.saturating_sub(1) as f32 * (2. * ITEM_GAP + 1.)
+ if hidden > 0 {
ITEM_GAP + self.badge.unwrap().1
ITEM_GAP
+ self
.badge
.expect("hidden activity count is nonzero only when its badge was measured")
.1
} else {
0.
};
+5 -2
View File
@@ -451,8 +451,11 @@ fn parse_document(
impl ShellConfig {
pub(crate) fn apply_theme(&mut self, theme: &ferese_config::theme::ResolvedTheme) {
let config: ThemeConfig =
serde_json::from_value(serde_json::to_value(&theme.tokens).unwrap()).unwrap();
let config: ThemeConfig = serde_json::from_value(
serde_json::to_value(&theme.tokens)
.expect("typed configuration and IPC values must serialize to JSON"),
)
.expect("resolved theme tokens must match their consumer schema");
self.theme = shell_theme(&config);
self.theme.appearance = theme.appearance;
self.theme.high_contrast = theme.accessibility.increase_contrast;
+1 -1
View File
@@ -295,7 +295,7 @@ impl FereseShell {
let current = Mode::ALL
.iter()
.position(|mode| *mode == self.display_mode.selected)
.unwrap();
.expect("Mode::ALL contains every selectable display mode");
let mode = match key {
Key::Character(character) => character
.parse::<usize>()
+4 -1
View File
@@ -460,7 +460,10 @@ impl FereseShell {
launcher.rows = rows;
launcher.file_status = status;
let task = self.merge_launcher_windows(true);
let launcher = self.launcher.as_mut().unwrap();
let launcher = self
.launcher
.as_mut()
.expect("merging launcher windows keeps the active launcher alive");
launcher.selected = selected
.and_then(|id| launcher.rows.iter().position(|row| row.id == id))
.unwrap_or(0);
+3 -1
View File
@@ -51,7 +51,9 @@ impl FereseShell {
&& let Some(effects) = &launcher.effects
{
let _ = effects.set_presentation(
&launcher.regions.lock().unwrap(),
&launcher.regions.lock().expect(
"surface rendering state must remain consistent across callbacks",
),
launcher.progress.current as f32,
[],
ferese_surface_effects_v1::Role::Popover,
+23 -6
View File
@@ -79,7 +79,11 @@ impl Service {
return;
}
self.requested = key.clone();
self.queue.0.lock().unwrap().pending = key;
self.queue
.0
.lock()
.expect("artwork decode queue must remain consistent across workers")
.pending = key;
self.queue.1.notify_one();
}
@@ -94,7 +98,11 @@ impl Service {
impl Drop for Service {
fn drop(&mut self) {
self.queue.0.lock().unwrap().stopped = true;
self.queue
.0
.lock()
.expect("artwork decode queue must remain consistent across workers")
.stopped = true;
self.queue.1.notify_one();
}
}
@@ -107,18 +115,27 @@ fn work(
let mut cache: VecDeque<Artwork> = VecDeque::new();
loop {
let (lock, changed) = &*queue;
let mut state = lock.lock().unwrap();
let mut state = lock
.lock()
.expect("artwork decode queue must remain consistent across workers");
while state.pending.is_none() && !state.stopped {
state = changed.wait(state).unwrap();
state = changed
.wait(state)
.expect("artwork decode queue must remain consistent across workers");
}
if state.stopped {
return;
}
let key = state.pending.take().unwrap();
let key = state
.pending
.take()
.expect("the artwork wait completed with a pending request");
drop(state);
let artwork = match cache.iter().position(|art| art.key == key) {
Some(index) => cache.remove(index).unwrap(),
Some(index) => cache
.remove(index)
.expect("the cache lookup returned an existing artwork index"),
None => Artwork {
handle: load(&key.uri),
key,
+3 -1
View File
@@ -503,7 +503,9 @@ impl<M> Widget<M, Theme, cosmic::Renderer> for Motion<'_, M> {
viewport: &Rectangle,
) {
if let Some(regions) = &self.regions {
let mut collected = regions.lock().unwrap();
let mut collected = regions
.lock()
.expect("surface rendering state must remain consistent across callbacks");
collected.clear();
self.content.as_widget_mut().operate(
tree,
+7 -2
View File
@@ -250,7 +250,10 @@ impl FereseShell {
.as_ref()
.is_some_and(|surface| !wanted || selected.as_ref() != Some(&surface.output))
{
let surface = self.notification_surface.take().unwrap();
let surface = self
.notification_surface
.take()
.expect("notification teardown selected an existing notification surface");
tasks.push(destroy_layer_surface(surface.id));
}
if !wanted {
@@ -630,7 +633,9 @@ impl FereseShell {
&& let Some(effects) = &surface.effects
{
let _ = effects.set_presentation(
&surface.regions.lock().unwrap(),
&surface.regions.lock().expect(
"surface rendering state must remain consistent across callbacks",
),
1.0,
self.notifications
.popup_groups_at(now)
+10 -4
View File
@@ -91,7 +91,10 @@ impl Server {
hints: HashMap<String, zbus::zvariant::OwnedValue>,
expire_timeout: i32,
) -> zbus::fdo::Result<u32> {
let mut registry = self.registry.lock().unwrap();
let mut registry = self
.registry
.lock()
.expect("notification ownership registry must remain consistent across callbacks");
let replacing = registry.live.contains_key(&replaces_id);
if !replacing && registry.live.len() >= HISTORY_LIMIT {
return Err(zbus::fdo::Error::LimitsExceeded(
@@ -156,7 +159,10 @@ impl Server {
}
fn close_notification(&self, id: u32) -> zbus::fdo::Result<()> {
let mut registry = self.registry.lock().unwrap();
let mut registry = self
.registry
.lock()
.expect("notification ownership registry must remain consistent across callbacks");
let revision = *registry
.live
.get(&id)
@@ -249,7 +255,7 @@ impl Service {
continue;
}
Command::Close(id, revision, reason) => {
if !registry.lock().unwrap().close(id, revision, reason) {
if !registry.lock().expect("notification ownership registry must remain consistent across callbacks").close(id, revision, reason) {
continue;
}
connection.emit_signal(
@@ -261,7 +267,7 @@ impl Service {
)
}
Command::Action(id, revision, action) => {
if registry.lock().unwrap().live.get(&id) != Some(&revision) {
if registry.lock().expect("notification ownership registry must remain consistent across callbacks").live.get(&id) != Some(&revision) {
continue;
}
connection.emit_signal(
+3 -1
View File
@@ -844,7 +844,9 @@ impl FereseShell {
let body: Element<'_, cosmic::Action<Message>> = match mode {
_ if activity.is_some() => self.hub_activity_strip(
id,
activity.unwrap(),
activity.expect(
"the activity-specific hub view requires its selected activity",
),
mode == Mode::Peek,
intrinsic,
opacity,
+38 -9
View File
@@ -140,8 +140,14 @@ type PollState = (Mutex<(u64, bool, Option<ActionError>, bool, u64)>, Condvar);
fn wait_for_poll(shared: &PollState) -> Option<u64> {
let state = shared
.1
.wait_while(shared.0.lock().unwrap(), |state| state.1 && !state.3)
.unwrap();
.wait_while(
shared
.0
.lock()
.expect("status polling state must not be used after a worker panics"),
|state| state.1 && !state.3,
)
.expect("status polling state must not be used after a worker panics");
(!state.3).then_some(state.0)
}
@@ -184,7 +190,11 @@ impl Service {
let mut night_light_monitor = night_light::Monitor::start(polling.clone()).ok();
while let Some(before) = wait_for_poll(&polling) {
let revision = polling.0.lock().unwrap().4;
let revision = polling
.0
.lock()
.expect("status polling state must not be used after a worker panics")
.4;
if night_light_monitor.is_none() {
night_light_monitor = night_light::Monitor::start(polling.clone()).ok();
}
@@ -222,7 +232,10 @@ impl Service {
snapshot.audio = audio.read(before);
snapshot.network = network.read(before, network::read);
snapshot.bluetooth = bluetooth.read(before, bluetooth::read);
let state = polling.0.lock().unwrap();
let state = polling
.0
.lock()
.expect("status polling state must not be used after a worker panics");
if state.3 {
break;
@@ -282,15 +295,24 @@ impl Service {
for (id, action) in pending {
{
let mut state = shared.0.lock().unwrap();
let mut state = shared
.0
.lock()
.expect("status polling state must not be used after a worker panics");
state.0 = id;
state.1 = true;
}
let settings = settings.lock().unwrap().clone();
let settings = settings
.lock()
.expect("status polling state must not be used after a worker panics")
.clone();
let error = execute(&action, settings.as_deref())
.err()
.map(|message| ActionError { action, message });
let mut state = shared.0.lock().unwrap();
let mut state = shared
.0
.lock()
.expect("status polling state must not be used after a worker panics");
state.1 = false;
state.2 = error;
state.4 = state.4.wrapping_add(1);
@@ -298,7 +320,11 @@ impl Service {
}
}
shared.0.lock().unwrap().3 = true;
shared
.0
.lock()
.expect("status polling state must not be used after a worker panics")
.3 = true;
shared.1.notify_one();
});
@@ -325,7 +351,10 @@ impl Service {
}
pub fn update_settings(&self, settings: Option<Vec<String>>) {
*self.settings.lock().unwrap() = settings;
*self
.settings
.lock()
.expect("status polling state must not be used after a worker panics") = settings;
}
pub fn subscription(&self) -> cosmic::iced::Subscription<Update> {
@@ -60,18 +60,26 @@ impl Signals {
pw::init();
loop {
if *stop.stopped.lock().unwrap() {
if *stop.stopped.lock().expect(
"audio listener lifecycle state must remain consistent across workers",
) {
break;
}
let _ = monitor(state.clone(), &stop, remote.as_deref());
state.set_alive(false);
*stop.sender.lock().unwrap() = None;
let stopped = stop.stopped.lock().unwrap();
*stop.sender.lock().expect(
"audio listener lifecycle state must remain consistent across workers",
) = None;
let stopped = stop.stopped.lock().expect(
"audio listener lifecycle state must remain consistent across workers",
);
let (stopped, _) = stop
.wake
.wait_timeout_while(stopped, Duration::from_secs(5), |value| !*value)
.unwrap();
.expect(
"audio listener lifecycle state must remain consistent across workers",
);
if *stopped {
break;
@@ -115,10 +123,20 @@ impl Signals {
impl Drop for Signals {
fn drop(&mut self) {
*self.stop.stopped.lock().unwrap() = true;
*self
.stop
.stopped
.lock()
.expect("audio listener lifecycle state must remain consistent across workers") = true;
self.stop.wake.notify_one();
if let Some(sender) = self.stop.sender.lock().unwrap().as_ref() {
if let Some(sender) = self
.stop
.sender
.lock()
.expect("audio listener lifecycle state must remain consistent across workers")
.as_ref()
{
let _ = sender.send(());
}
}
@@ -273,9 +291,17 @@ fn monitor(state: Arc<State>, stop: &Stop, remote: Option<&str>) -> Result<(), p
mainloop.quit();
}
});
*stop.sender.lock().unwrap() = Some(sender);
*stop
.sender
.lock()
.expect("audio listener lifecycle state must remain consistent across workers") =
Some(sender);
if !*stop.stopped.lock().unwrap() {
if !*stop
.stopped
.lock()
.expect("audio listener lifecycle state must remain consistent across workers")
{
mainloop.run();
}
@@ -21,8 +21,12 @@ fn menu_positioner(
};
SctkPositioner {
anchor_rect: rectangle,
anchor: anchor.try_into().unwrap(),
gravity: gravity.try_into().unwrap(),
anchor: anchor
.try_into()
.expect("popup placement uses supported anchor and gravity combinations"),
gravity: gravity
.try_into()
.expect("popup placement uses supported anchor and gravity combinations"),
offset: (rectangle.width / 2, gap),
size_limits: Limits::NONE
.max_width(368.0)
+7 -2
View File
@@ -164,7 +164,10 @@ impl OpenMenu {
}
}
if let Some(effects) = &self.effects {
let regions = self.regions.lock().unwrap();
let regions = self
.regions
.lock()
.expect("surface rendering state must remain consistent across callbacks");
if let Err(error) = effects.set_regions(&regions) {
eprintln!("ferese-shell: could not prepare popover material: {error}");
}
@@ -355,7 +358,9 @@ impl FereseShell {
|now| {
if let Some(effects) = &menu.effects {
let _ = effects.set_presentation(
&menu.regions.lock().unwrap(),
&menu.regions.lock().expect(
"surface rendering state must remain consistent across callbacks",
),
menu.motion.progress_at(now),
[],
super::ferese_surface_effects_v1::Role::Popover,
+8 -2
View File
@@ -557,7 +557,10 @@ impl FereseShell {
.as_ref()
.is_some_and(|entry| entry.id == layer_id) =>
{
let entry = self.notification_surface.as_mut().unwrap();
let entry = self
.notification_surface
.as_mut()
.expect("the notification update branch matched an existing surface");
if entry.effects.is_none() {
match EffectsBinding::attach_role(
@@ -766,7 +769,10 @@ impl EffectsBinding {
queue,
}));
}
let context = context.as_ref().unwrap().clone();
let context = context
.as_ref()
.expect("surface effects binding initialized its native display context")
.clone();
let effects = context
.manager
.as_ref()
+8 -2
View File
@@ -436,7 +436,11 @@ impl FereseShell {
let Content::Power(power) = modal.content else {
return self.close_system_modal();
};
let approval = modal.inhibitors.as_ref().unwrap().clone();
let approval = modal
.inhibitors
.as_ref()
.expect("power confirmation was populated with its inhibitor decision")
.clone();
let action = match power {
PowerAction::Logout(serial) => {
if let Some(control) = &self.control {
@@ -516,7 +520,9 @@ impl FereseShell {
|now| {
if let Some(effects) = &surface.effects {
let _ = effects.set_presentation(
&surface.regions.lock().unwrap(),
&surface.regions.lock().expect(
"surface rendering state must remain consistent across callbacks",
),
modal.motion.progress_at(now),
[],
ferese_surface_effects_v1::Role::Modal,
+6 -2
View File
@@ -305,8 +305,12 @@ impl FereseShell {
width: bounds.width.round() as i32,
height: bounds.height.round() as i32,
},
anchor: anchor.try_into().unwrap(),
gravity: gravity.try_into().unwrap(),
anchor: anchor
.try_into()
.expect("popup placement uses supported anchor and gravity combinations"),
gravity: gravity
.try_into()
.expect("popup placement uses supported anchor and gravity combinations"),
offset: (0, gap),
constraint_adjustment: 3,
size_limits: cosmic::iced::Limits::NONE.max_width(320.).max_height(64.),