-
Notifications
You must be signed in to change notification settings - Fork 540
Code tidying, particularly SampleFormat, with many functions as const #1353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
3999b15
8609e77
1718ccb
0a1d753
9615dc7
255d063
ff7aa55
3b49e43
41e840c
b9ab2bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,6 +92,30 @@ Ordering of `xrun()` relative to the glitch it reports varies by host; see [`Cal | |
|
|
||
| [`CallbackInfo::xrun()`]: https://docs.rs/cpal/latest/cpal/struct.CallbackInfo.html#method.xrun | ||
|
|
||
| ## 5. `SampleFormat` methods made `const`, and take `self` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| **What changed:** `SampleFormat` methods are now constant, and don't take a reference anymore. | ||
|
|
||
| ```rust | ||
| // Before (v0.18): | ||
| let i16_is_int: bool = SampleFormat::I16.is_int(); | ||
|
|
||
| let mut formats = vec![SampleFormat::I16, SampleFormat::F32]; | ||
| formats.retain(SampleFormat::is_int); | ||
|
|
||
| // After (v0.19): constant, and not referenced | ||
| const I16_IS_INT: bool = SampleFormat::I16.is_int(); | ||
|
|
||
| let mut formats = vec![SampleFormat::I16, SampleFormat::F32]; | ||
| formats.retain(|f| f.is_int()); | ||
| ``` | ||
|
|
||
| **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. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| [`SampleFormat`]: https://docs.rs/cpal/latest/cpal/enum.SampleFormat.html | ||
| --- | ||
|
|
||
| # Upgrading from v0.17 to v0.18 | ||
|
|
@@ -407,12 +431,12 @@ let device = host.device_by_id(&id); | |
| ```rust | ||
| // Before (v0.17) | ||
| for line in desc.extended() { // &[String] | ||
| println!("{}", line); // line: &String | ||
| println!("{line}"); // line: &String | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The spacing of the comment seems unaligned with the one above it. |
||
| } | ||
|
|
||
| // 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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The spacing of the comment seems unaligned with the one above it. |
||
| } | ||
| ``` | ||
|
|
||
|
|
@@ -540,7 +564,7 @@ let name = device.name()?; | |
|
|
||
| // New: For user-facing display | ||
| let desc = device.description()?; | ||
| println!("Device: {}", desc); // or desc.name() for just the name | ||
| println!("Device: {desc}"); // or desc.name() for just the name | ||
|
|
||
| // New: For stable identification and persistence | ||
| let id = device.id()?; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: trailing period.