Add non-panicking variant of with_sample_rate (#828)

This commit is contained in:
Adam Gausmann 2024-02-04 19:29:30 -06:00 • committed by GitHub
parent 49c1196999
commit 4706396ca3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -633,15 +633,28 @@ impl SupportedStreamConfigRange {
/// ///
/// # Panics /// # Panics
/// ///
/// Panics if the given `sample_rate` is outside the range specified within this /// Panics if the given `sample_rate` is outside the range specified within
/// [`SupportedStreamConfigRange`] instance. /// 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 { pub fn with_sample_rate(self, sample_rate: SampleRate) -> SupportedStreamConfig {
assert!(self.min_sample_rate <= sample_rate && sample_rate <= self.max_sample_rate); self.try_with_sample_rate(sample_rate)
SupportedStreamConfig { .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, channels: self.channels,
sample_rate, sample_rate,
sample_format: self.sample_format, sample_format: self.sample_format,
buffer_size: self.buffer_size, buffer_size: self.buffer_size,
})
} else {
None
} }
} }