Skip to content

Commit 2e699ec

Browse files
refactor: adopt rust 1.100 (1.96-1.100) std APIs and tidy things
Signed-off-by: Maika Namuo <httpworldview@gmail.com>
1 parent 82bb03e commit 2e699ec

34 files changed

Lines changed: 260 additions & 256 deletions

‎src/dsp.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ impl Biquad {
465465
std::array::from_fn(|i| b[2] * sample[i] - a[1] * output[i]),
466466
];
467467
let output = std::array::from_fn(|channel| {
468-
if output[channel].abs() <= f32::MAX as f64 {
468+
if output[channel].abs() <= f64::from(f32::MAX) {
469469
output[channel] as f32
470470
} else {
471471
state[0][channel] = 0.0;

‎src/infra/pipewire/graph.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,7 @@ fn stream_identity(
222222
node_name: Option<&str>,
223223
) -> Option<StreamIdentity> {
224224
let media_class = clean_property(media_class)?;
225-
let media_class = clean_property(Some(
226-
media_class.strip_prefix("Stream/").unwrap_or(media_class),
227-
))?;
225+
let media_class = clean_property(Some(media_class.trim_prefix("Stream/")))?;
228226
let (property, value) = match clean_property(application_name) {
229227
Some(name) => ("application.name", name),
230228
None => ("node.name", clean_property(node_name)?),
@@ -306,7 +304,7 @@ impl Graph {
306304
}
307305

308306
pub(super) fn remove_global(&mut self, id: u32) {
309-
let _ = self.default_sink.take_if(|target| target.metadata_id == id);
307+
self.default_sink.take_if(|target| target.metadata_id == id);
310308
if self.ports.remove(&id).is_some() {
311309
return;
312310
}
@@ -340,7 +338,7 @@ impl Graph {
340338
let same_entry =
341339
|target: &mut Target| target.metadata_id == metadata_id && target.subject == subject;
342340
let Some(key) = key else {
343-
let _ = self.default_sink.take_if(same_entry);
341+
self.default_sink.take_if(same_entry);
344342
return;
345343
};
346344
if key != "default.audio.sink" {
@@ -443,8 +441,10 @@ impl Graph {
443441
let Some(target) = &mut self.default_sink else {
444442
return;
445443
};
446-
let _ = target.node_id.take_if(|id| !self.nodes.contains_key(id));
447-
if target.node_id.is_none() {
444+
if target
445+
.node_id
446+
.is_none_or(|id| !self.nodes.contains_key(&id))
447+
{
448448
target.node_id = target.name.as_deref().and_then(|name| {
449449
self.nodes
450450
.iter()

‎src/infra/pipewire/live_tests.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ fn live_capture_preserves_graph_invariants() {
699699
);
700700
server.wait_audio("surround application mix", &mut audio, |graph| {
701701
let source = graph.node_id(&surround_playback)?;
702-
stable_mix(graph).filter(|_| graph.link_count(source, tap_id) == channels)
702+
stable_mix(graph).filter(|()| graph.link_count(source, tap_id) == channels)
703703
});
704704
wait_for_mapped_signal(
705705
"captured surround application mix",
@@ -709,7 +709,7 @@ fn live_capture_preserves_graph_invariants() {
709709
);
710710
drop(surround);
711711
server.wait_audio("stable application mix", &mut audio, |graph| {
712-
stable_mix(graph).filter(|_| graph.node(&surround_playback).is_none())
712+
stable_mix(graph).filter(|()| graph.node(&surround_playback).is_none())
713713
});
714714
}
715715

‎src/infra/pipewire/runtime.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ fn wait_for_retry(
7171
}
7272

7373
fn take_retry_delay(delay: &mut Duration, maximum: Duration) -> Duration {
74-
let current = *delay;
75-
*delay = delay.saturating_mul(2).min(maximum);
76-
current
74+
std::mem::replace(delay, delay.saturating_mul(2).min(maximum))
7775
}
7876

7977
#[derive(Clone, Copy)]
@@ -225,8 +223,8 @@ fn run_session(
225223
// harmlessly; any other core error, such as a lost connection, ends the session.
226224
let race = std::io::Error::from_raw_os_error(result.wrapping_neg()).kind()
227225
== std::io::ErrorKind::NotFound;
228-
if id == pw::core::PW_ID_CORE && !race && fatal.borrow().is_none() {
229-
*fatal.borrow_mut() = Some(detail);
226+
if id == pw::core::PW_ID_CORE && !race {
227+
fatal.borrow_mut().get_or_insert(detail);
230228
}
231229
})
232230
.register()
@@ -367,7 +365,7 @@ impl CaptureState {
367365
.tap
368366
.node_id()
369367
.and_then(|id| graph.node(id))
370-
.map_or_else(Vec::new, |tap| policy::desired_links(&graph, &plan, tap));
368+
.map_or_default(|tap| policy::desired_links(&graph, &plan, tap));
371369
self.owned_links.apply(desired, now);
372370
let view = graph.view(self.tap.node_id(), config.device.as_deref());
373371
if let Some(selected) = &view.selected_device
@@ -475,7 +473,9 @@ impl OwnedLinks {
475473
*pw::keys::MEDIA_CATEGORY => "Capture",
476474
*pw::keys::MEDIA_ROLE => "Production",
477475
};
478-
let link = (self.core).create_object::<pw::link::Link>(LINK_FACTORY, &props)?;
476+
let link = self
477+
.core
478+
.create_object::<pw::link::Link>(LINK_FACTORY, &props)?;
479479
let state = Rc::new(Cell::new(OwnedLinkState::Pending));
480480
let fail = {
481481
let (state, dirty) = (Rc::clone(&state), Rc::clone(&self.dirty));
@@ -696,7 +696,7 @@ mod tests {
696696
// A replacement stream does not inherit the accepted time of the one it replaces, even
697697
// when it is accepted before reconcile sees it start.
698698
let mut stale = capture.tap.config().copied().expect("configured tap");
699-
stale.passive = !stale.passive;
699+
stale.passive.toggle();
700700
capture.tap.configure(stale).expect("stale tap");
701701
writer.borrow_mut().set_status(StreamStatus::Paused);
702702
capture.dirty.set(true);

‎src/infra/pipewire/stream.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn process(stream: &pw::stream::Stream, writer: &mut CaptureWriter) {
209209
let channels = format.channels();
210210
let frame_bytes = channels * size_of::<f32>();
211211
if stride != 0 && usize::try_from(stride).ok() != Some(frame_bytes) {
212-
writer.push_fault(size as u64 / frame_bytes as u64);
212+
writer.push_fault(u64::from(size) / frame_bytes as u64);
213213
return;
214214
}
215215
let Some(chunk) = pcm_chunk(data.as_raw().maxsize as usize, offset, size, frame_bytes) else {

‎src/infra/pipewire/transport.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn idle_watchdog_ns(rate: u64, burst_ns: u64) -> u64 {
4949
)
5050
}
5151

52+
#[cfg_attr(test, derive(Debug))]
5253
pub enum CapturedSpan<'a> {
5354
Pcm {
5455
samples: &'a [f32],
@@ -160,7 +161,7 @@ impl Shared {
160161

161162
fn wake_ingestion(&self) -> bool {
162163
let (dormant, active) = (IngestState::Dormant as u8, IngestState::Active as u8);
163-
(self.ingest)
164+
self.ingest
164165
.compare_exchange(dormant, active, Ordering::SeqCst, Ordering::Relaxed)
165166
.is_ok()
166167
}
@@ -261,9 +262,7 @@ impl SamplePool {
261262
self.clear();
262263
self.samples = format.channels() * packet_frame_limit(format.rate());
263264
self.limit = packet_pool_limit(format.rate(), queue_capacity);
264-
self.free.reserve(self.limit);
265-
self.free
266-
.extend((0..self.limit).map(|_| vec![0.0; self.samples].into_boxed_slice()));
265+
self.free = Vec::from_fn(self.limit, |_| vec![0.0; self.samples].into_boxed_slice());
267266
}
268267

269268
fn clear(&mut self) {
@@ -704,7 +703,7 @@ impl AudioReader {
704703

705704
pub fn sleep_until_signal(&mut self) -> bool {
706705
let (active, dormant) = (IngestState::Active as u8, IngestState::Dormant as u8);
707-
if let Err(state) = (self.shared.ingest).compare_exchange(
706+
if let Err(state) = self.shared.ingest.compare_exchange(
708707
active,
709708
dormant,
710709
Ordering::SeqCst,
@@ -889,6 +888,7 @@ fn channel_with_capacity(capacity: usize) -> (CaptureWriter, AudioReader) {
889888
#[cfg(test)]
890889
mod tests {
891890
use super::*;
891+
use std::assert_matches;
892892

893893
fn mono(capacity: usize, rate: u32) -> (CaptureWriter, AudioReader, AudioFormat) {
894894
let (mut writer, mut reader) = channel_with_capacity(capacity);
@@ -981,7 +981,7 @@ mod tests {
981981
let now = writer.shared.epoch + Duration::from_nanos(end + 1) + MAX_BACKLOG;
982982
let mut resets = 0;
983983
reader.drain(now, |span| {
984-
assert!(matches!(span, CapturedSpan::Reset));
984+
assert_matches!(span, CapturedSpan::Reset);
985985
resets += 1;
986986
});
987987
assert_eq!(resets, 1);

‎src/meter.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ impl DspBatcher {
3434
}
3535

3636
fn push(&mut self, manager: &mut VisualManager, mut samples: &[f32], format: AudioFormat) {
37-
if self.format.is_some_and(|current| current != format) {
37+
if self
38+
.format
39+
.replace(format)
40+
.is_some_and(|previous| previous != format)
41+
{
3842
self.samples.clear();
3943
}
40-
self.format = Some(format);
4144
let batch = format.sample_rate().frames(DSP_BATCH_SECONDS) * format.channels();
4245
if !self.samples.is_empty() {
4346
let take = (batch - self.samples.len()).min(samples.len());

‎src/persistence/schema.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ mod tests {
120120
use super::super::visuals::{PopoutWindowSettings, SpectrumSettings, VisualConfig};
121121
use super::*;
122122
use crate::{domain::visuals::VisualKind, visuals::spectrum::processor::AveragingMode};
123+
use std::assert_matches;
123124

124125
fn load(value: Value) -> UiSettings {
125126
lossy::settings(value, UiSettings::SCOPE, UiSettings::read_fields)
@@ -279,6 +280,6 @@ mod tests {
279280
);
280281
assert_eq!(spectrum.hop_size, SpectrumSettings::default().hop_size);
281282
assert_eq!(spectrum.bar_gap, SpectrumSettings::default().bar_gap);
282-
assert!(matches!(spectrum.averaging, AveragingMode::None));
283+
assert_matches!(spectrum.averaging, AveragingMode::None);
283284
}
284285
}

‎src/persistence/visuals.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ impl VisualSettings {
9696
}
9797

9898
fn module_mut(&mut self, kind: VisualKind) -> &mut ModuleSettings {
99-
(self.modules.entry(kind)).or_insert_with(|| ModuleSettings::new(kind))
99+
self.modules
100+
.entry(kind)
101+
.or_insert_with(|| ModuleSettings::new(kind))
100102
}
101103

102104
pub(crate) fn set_enabled(&mut self, kind: VisualKind, enabled: bool) {

‎src/persistence/watch.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use rustix::fs::inotify::{self, CreateFlags, WatchFlags as W};
55
use rustix::io::Errno;
6-
use std::convert::Infallible;
76
use std::ffi::{OsStr, OsString};
87
use std::hash::{Hash, Hasher};
98
use std::iter;
@@ -63,7 +62,7 @@ pub fn changes(watcher: &SettingsWatcher) -> async_channel::Receiver<()> {
6362
}
6463

6564
// Runs for the life of the app, since the settings keep the channel open.
66-
fn watch_loop(dirs: &[PathBuf], sender: &async_channel::Sender<()>) -> io::Result<Infallible> {
65+
fn watch_loop(dirs: &[PathBuf], sender: &async_channel::Sender<()>) -> io::Result<!> {
6766
// Reads shouldn't wake the watcher
6867
let mask = W::CLOSE_WRITE | W::MOVED_TO | W::MOVED_FROM | W::CREATE | W::DELETE | W::ONLYDIR;
6968
let mut buf = [MaybeUninit::uninit(); 4096];
@@ -115,7 +114,9 @@ fn concerns(watched: &[(i32, Option<OsString>)], event: &inotify::Event<'_>) ->
115114
event.file_name().is_none_or(|name| {
116115
watched.iter().any(|(wd, entry)| {
117116
*wd == event.wd()
118-
&& (entry.as_deref()).is_none_or(|entry| entry.as_bytes() == name.to_bytes())
117+
&& entry
118+
.as_deref()
119+
.is_none_or(|entry| entry.as_bytes() == name.to_bytes())
119120
})
120121
})
121122
}

0 commit comments

Comments
 (0)