Code tidying, particularly SampleFormat, with many functions as const - #1353
Code tidying, particularly SampleFormat, with many functions as const#1353Marco-Farruggio wants to merge 10 commits into
Conversation
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 |
|
Yeah I personally like neat-looking code, and I can easily add back the Note on the branch name: I was originally going to clean up the |
|
Clippy shows 62 functions which could be |
roderickvd
left a comment
There was a problem hiding this comment.
Direction is good and I appreciate these quality-fof-code PRs. A few points.
| debug_assert_eq!( | ||
| buffer.len() % sample_size, | ||
| 0, | ||
| debug_assert!( |
There was a problem hiding this comment.
Please revert these to use _eq!.
| #[must_use] | ||
| pub fn sample_size(&self) -> usize { | ||
| match *self { | ||
| pub const fn sample_size(self) -> usize { |
There was a problem hiding this comment.
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)
| 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. |
There was a problem hiding this comment.
Nitpick: I'd prefer the comment to go above both lines, or to be duplicated to the F64 arm for visibility.
There was a problem hiding this comment.
I'd vote for duplicating
| /// match config.buffer_size() { | ||
| /// SupportedBufferSize::Range { min, max } => { | ||
| /// println!("Buffer size range: {} - {}", min, max); | ||
| /// println!("Buffer size range: {min} - {max}"); |
There was a problem hiding this comment.
Nitpick: two trailing spaces.
|
To the I'd leave I think we'd really need to go over it case-by-case. |
|
@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. |
|
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 |
roderickvd
left a comment
There was a problem hiding this comment.
Glad to hear your op went well. A few nitpicks.
| **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. |
There was a problem hiding this comment.
Typo: "dereferencing".
In the rest of the file we don't use newlines.
| // 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 |
There was a problem hiding this comment.
The spacing of the comment seems unaligned with the one above it.
| // Before (v0.17) | ||
| for line in desc.extended() { // &[String] | ||
| println!("{}", line); // line: &String | ||
| println!("{line}"); // line: &String |
There was a problem hiding this comment.
The spacing of the comment seems unaligned with the one above it.
|
|
||
| [`CallbackInfo::xrun()`]: https://docs.rs/cpal/latest/cpal/struct.CallbackInfo.html#method.xrun | ||
|
|
||
| ## 5. `SampleFormat` methods made `const`, and take `self` |
There was a problem hiding this comment.
For consistency, this should also have an entry in the numbered list at the top.
| - `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` |

equilibrium.rsframes_to_durationa bit more legibleSizedSampleSampleFormatconst