Skip to content
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

Add non-panicking variant of with_sample_rate #828

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,15 +633,28 @@ impl SupportedStreamConfigRange {
///
/// # Panics
///
/// Panics if the given `sample_rate` is outside the range specified within this
/// [`SupportedStreamConfigRange`] instance.
/// Panics if the given `sample_rate` is outside the range specified within
/// this [`SupportedStreamConfigRange`] instance. For a non-panicking
/// variant, use [`try_with_sample_rate`](#method.try_with_sample_rate).
pub fn with_sample_rate(self, sample_rate: SampleRate) -> SupportedStreamConfig {
assert!(self.min_sample_rate <= sample_rate && sample_rate <= self.max_sample_rate);
SupportedStreamConfig {
channels: self.channels,
sample_rate,
sample_format: self.sample_format,
buffer_size: self.buffer_size,
self.try_with_sample_rate(sample_rate)
.expect("sample rate out of range")
}

/// Retrieve a [`SupportedStreamConfig`] with the given sample rate and buffer size.
///
/// Returns `None` if the given sample rate is outside the range specified
/// within this [`SupportedStreamConfigRange`] instance.
pub fn try_with_sample_rate(self, sample_rate: SampleRate) -> Option<SupportedStreamConfig> {
if self.min_sample_rate <= sample_rate && sample_rate <= self.max_sample_rate {
Some(SupportedStreamConfig {
channels: self.channels,
sample_rate,
sample_format: self.sample_format,
buffer_size: self.buffer_size,
})
} else {
None
}
}

Expand Down
Loading