Skip to content

Code tidying, particularly SampleFormat, with many functions as const - #1353

Open
Marco-Farruggio wants to merge 10 commits into
RustAudio:masterfrom
Marco-Farruggio:simplify-fill-equilibrium
Open

Code tidying, particularly SampleFormat, with many functions as const#1353
Marco-Farruggio wants to merge 10 commits into
RustAudio:masterfrom
Marco-Farruggio:simplify-fill-equilibrium

Conversation

@Marco-Farruggio

Copy link
Copy Markdown
Contributor
  1. Cleaned up a debug assertion in equilibrium.rs
  2. Made frames_to_duration a bit more legible
  3. Removed commented out i48/i48 lines
  4. Made a macro to implement SizedSample
  5. Made many methods on SampleFormat const

  1. Lined up match arms (I can undo this quickly if you'd like)

@LastExceed

LastExceed commented Sep 3, 2026

Copy link
Copy Markdown
Contributor
  1. replacing assert_eq!(x,y) with assert!(x == y) seems backwards to me. Isn't this the very thing assert_eq exists for?
  2. LGTM
  3. LGTM
  4. LGTM
  5. LGTM - particularly excited about this one, as I (kind of) needed it for new ASIO host implementation #1331 :D
  6. I too like lining things up, and I've been wondering if rust-fmt can be configured to allow this (as well as a few other things that I am currently too stubborn to fix in new ASIO host implementation #1331). But we should probably gather a few more opinions on this

PS: Try to record each change in a separate commit in the future. The enumeration you provided is the ideal degree of segmentation in my eyes. Probably not worth rewriting history retroactively here, as the PR is pretty easy to comprehend as-is, but as a general rule of thumb: "Commit early and often". You almost cannot overdo this

@Marco-Farruggio

Marco-Farruggio commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

Yeah I personally like neat-looking code, and I can easily add back the _eq! if you'd like, I just removed it because when I thought the == was a bit cleaner, and sure, I'll stage it as separate commits going forward, thanks for the tip :)

Note on the branch name: I was originally going to clean up the fill_equilibrium function, alot more, but every time I refactored it I realised the original approach was good)

@Marco-Farruggio

Copy link
Copy Markdown
Contributor Author

Clippy shows 62 functions which could be const for me (I think some could be false positives), I see zero downside of making many of these const, but I spose making some of them const may come across as a false 'promise' that it will be const forever? For some methods it doesn't really even make sense, like SupportedStreamConfig::new, which would be runtime only. Things such as StreamInstant::new() though, I think should just be const as there's no reason for them not to be

@roderickvd roderickvd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direction is good and I appreciate these quality-fof-code PRs. A few points.

Comment thread src/host/equilibrium.rs Outdated
debug_assert_eq!(
buffer.len() % sample_size,
0,
debug_assert!(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert these to use _eq!.

Comment thread src/sample_format.rs
#[must_use]
pub fn sample_size(&self) -> usize {
match *self {
pub const fn sample_size(self) -> usize {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are breaking and should be documented both in CHANGELOG.md and UPGRADING.md. (consider stuff like formats.iter().map(SampleFormat::is_int) that will no longer compile)

Comment thread src/sample_format.rs Outdated
SampleFormat::I64 => i64::BITS,
SampleFormat::U64 => u64::BITS,
SampleFormat::F32 => 32,
SampleFormat::F32 => 32, // f32/64::BITS is currently unstable, so we hardcode the values here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I'd prefer the comment to go above both lines, or to be duplicated to the F64 arm for visibility.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd vote for duplicating

Comment thread src/lib.rs Outdated
/// match config.buffer_size() {
/// SupportedBufferSize::Range { min, max } => {
/// println!("Buffer size range: {} - {}", min, max);
/// println!("Buffer size range: {min} - {max}");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: two trailing spaces.

@roderickvd

Copy link
Copy Markdown
Member

To the const point:

I'd leave SupportedStreamConfig::new alone because in the future it may want to do validation or clamping (#956) and then we want to have the flexibility to call something non-const.

I think we'd really need to go over it case-by-case. StreamInstant and SampleFormat you did in this PR were good ones to do it for.

@roderickvd

Copy link
Copy Markdown
Member

@Marco-Farruggio thanks for the changes - I hope that means your op went well I hope! - let me know when you'd like me to do another review pass.

@Marco-Farruggio

Copy link
Copy Markdown
Contributor Author

The op did go well thank you! I needed to take a break though before writing up the documentation, so I’ll do the change log and upgrading.md today, see if there’s anything else I want to do in this pr, and then request review/merge

@Marco-Farruggio

Copy link
Copy Markdown
Contributor Author
image

Just noticed the 18.2 docs had the commented out u48 line in them

@roderickvd roderickvd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to hear your op went well. A few nitpicks.

Comment thread UPGRADING.md
**Impact:** `SampleFormat` can now be used in a `const` environment.

**Why:** `SampleFormat` is a simple enum, there was no reason why it shouldn't be const-friendly, and since every method was both `inline` and it implements `Copy`, there
is no performance downside to it taking `self`, but simply more legible than derefrencing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: "dereferencing".

In the rest of the file we don't use newlines.

Comment thread UPGRADING.md
// After (v0.18)
for line in desc.extended() { // impl Iterator<Item = &str>
println!("{}", line); // line: &str — Display, write!, format! all unchanged
println!("{line}"); // line: &str — Display, write!, format! all unchanged

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spacing of the comment seems unaligned with the one above it.

Comment thread UPGRADING.md
// Before (v0.17)
for line in desc.extended() { // &[String]
println!("{}", line); // line: &String
println!("{line}"); // line: &String

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spacing of the comment seems unaligned with the one above it.

Comment thread UPGRADING.md

[`CallbackInfo::xrun()`]: https://docs.rs/cpal/latest/cpal/struct.CallbackInfo.html#method.xrun

## 5. `SampleFormat` methods made `const`, and take `self`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency, this should also have an entry in the numbered list at the top.

Comment thread CHANGELOG.md
- `StreamTrait::play` is renamed to `start`.
- `InputCallbackInfo`/`OutputCallbackInfo` merged into `CallbackInfo`.
- `InputStreamTimestamp`/`OutputStreamTimestamp` merged into `StreamTimestamp`; `capture`/`playback` renamed `device`.
- `StreamInstant` creation is now `const`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: trailing period.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants