Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Refactor error handling with let-else chains

Richie McIlroy1de769fe51e6
diff --git a/crates/enc-mediafoundation/src/d3d.rs b/crates/enc-mediafoundation/src/d3d.rs
index 81bf3917e..e06e16384 100644
--- a/crates/enc-mediafoundation/src/d3d.rs
+++ b/crates/enc-mediafoundation/src/d3d.rs
@@ -46,10 +46,10 @@ pub fn create_d3d_device() -> Result<ID3D11Device> {
flags
};
let mut result = create_d3d_device_with_type(D3D_DRIVER_TYPE_HARDWARE, flags, &mut device);
- if let Err(error) = &result {
- if error.code() == DXGI_ERROR_UNSUPPORTED {
- result = create_d3d_device_with_type(D3D_DRIVER_TYPE_WARP, flags, &mut device);
- }
+ if let Err(error) = &result
+ && error.code() == DXGI_ERROR_UNSUPPORTED
+ {
+ result = create_d3d_device_with_type(D3D_DRIVER_TYPE_WARP, flags, &mut device);
}
result?;
Ok(device.unwrap())
diff --git a/crates/enc-mediafoundation/src/video/h264.rs b/crates/enc-mediafoundation/src/video/h264.rs
index 43a8b39f2..2552fc924 100644
--- a/crates/enc-mediafoundation/src/video/h264.rs
+++ b/crates/enc-mediafoundation/src/video/h264.rs
@@ -11,7 +11,7 @@ use windows::{
Foundation::TimeSpan,
Graphics::SizeInt32,
Win32::{
- Foundation::{E_FAIL, E_NOTIMPL},
+ Foundation::E_NOTIMPL,
Graphics::{
Direct3D11::{ID3D11Device, ID3D11Texture2D},
Dxgi::Common::{DXGI_FORMAT, DXGI_FORMAT_NV12},
@@ -239,10 +239,10 @@ impl H264Encoder {
let mut count = 0;
loop {
let result = transform.GetInputAvailableType(input_stream_id, count);
- if let Err(error) = &result {
- if error.code() == MF_E_NO_MORE_TYPES {
- break Ok(None);
- }
+ if let Err(error) = &result
+ && error.code() == MF_E_NO_MORE_TYPES
+ {
+ break Ok(None);
}
let input_type = result?;
@@ -261,11 +261,11 @@ impl H264Encoder {
&input_type,
MFT_SET_TYPE_TEST_ONLY.0 as u32,
);
- if let Err(error) = &result {
- if error.code() == MF_E_INVALIDMEDIATYPE {
- count += 1;
- continue;
- }
+ if let Err(error) = &result
+ && error.code() == MF_E_INVALIDMEDIATYPE
+ {
+ count += 1;
+ continue;
}
result?;
break Ok(Some(input_type));
@@ -404,24 +404,24 @@ impl H264Encoder {
match event_type {
MediaFoundation::METransformNeedInput => {
should_exit = true;
- if !should_stop.load(Ordering::SeqCst) {
- if let Some((texture, timestamp)) = get_frame()? {
- self.video_processor.process_texture(&texture)?;
- let input_buffer = {
- MFCreateDXGISurfaceBuffer(
- &ID3D11Texture2D::IID,
- self.video_processor.output_texture(),
- 0,
- false,
- )?
- };
- let mf_sample = MFCreateSample()?;
- mf_sample.AddBuffer(&input_buffer)?;
- mf_sample.SetSampleTime(timestamp.Duration)?;
- self.transform
- .ProcessInput(self.input_stream_id, &mf_sample, 0)?;
- should_exit = false;
- }
+ if !should_stop.load(Ordering::SeqCst)
+ && let Some((texture, timestamp)) = get_frame()?
+ {
+ self.video_processor.process_texture(&texture)?;
+ let input_buffer = {
+ MFCreateDXGISurfaceBuffer(
+ &ID3D11Texture2D::IID,
+ self.video_processor.output_texture(),
+ 0,
+ false,
+ )?
+ };
+ let mf_sample = MFCreateSample()?;
+ mf_sample.AddBuffer(&input_buffer)?;
+ mf_sample.SetSampleTime(timestamp.Duration)?;
+ self.transform
+ .ProcessInput(self.input_stream_id, &mf_sample, 0)?;
+ should_exit = false;
}
}
MediaFoundation::METransformHaveOutput => {