Skip to content

Commit

Permalink
Adopt if_none(|err|) patternd
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Jan 3, 2024
1 parent 11768bc commit 0353f95
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 54 deletions.
41 changes: 6 additions & 35 deletions cidre/src/mtl/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,7 @@ impl Device {
source: &ns::String,
options: Option<&mtl::CompileOpts>,
) -> Result<arc::R<Lib>, &'ear ns::Error> {
let mut error = None;
unsafe {
if let Some(lib) = Self::new_lib_with_src_err(self, source, options, &mut error) {
Ok(lib)
} else {
Err(error.unwrap_unchecked())
}
}
ns::if_none(|err| unsafe { Self::new_lib_with_src_err(self, source, options, err) })
}

pub async fn new_lib_with_src_opts(
Expand Down Expand Up @@ -189,30 +182,15 @@ impl Device {
&self,
descriptor: &mtl::RenderPipelineDesc,
) -> Result<arc::R<mtl::RenderPipelineState>, &'ear ns::Error> {
let mut error = None;
unsafe {
if let Some(res) = Self::new_render_ps_err(self, descriptor, &mut error) {
Ok(res)
} else {
Err(error.unwrap_unchecked())
}
}
ns::if_none(|err| unsafe { Self::new_render_ps_err(self, descriptor, err) })
}

#[inline]
pub fn new_compute_ps_with_fn<'ear>(
&self,
function: &mtl::Fn,
) -> Result<arc::R<mtl::ComputePipelineState>, &'ear ns::Error> {
let mut error = None;
unsafe {
let res = self.new_compute_ps_with_fn_err(function, &mut error);
if res.is_some() {
Ok(res.unwrap_unchecked())
} else {
Err(error.unwrap_unchecked())
}
}
ns::if_none(|err| unsafe { self.new_compute_ps_with_fn_err(function, err) })
}

#[objc::msg_send(newRenderPipelineStateWithTileDescriptor:options:reflection:error:)]
Expand All @@ -229,16 +207,9 @@ impl Device {
desc: &mtl::TileRenderPipelineDesc,
options: mtl::PipelineOption,
) -> Result<arc::R<mtl::RenderPipelineState>, &'ear ns::Error> {
let mut error = None;
unsafe {
if let Some(res) =
self.new_tile_render_ps_err(desc, options, std::ptr::null_mut(), &mut error)
{
Ok(res)
} else {
Err(error.unwrap_unchecked())
}
}
ns::if_none(|err| unsafe {
self.new_tile_render_ps_err(desc, options, std::ptr::null_mut(), err)
})
}

#[objc::msg_send(newBufferWithLength:options:)]
Expand Down
22 changes: 3 additions & 19 deletions cidre/src/mtl/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,39 +239,23 @@ impl Lib {
name: &ns::String,
constant_values: &mtl::FnConstValues,
) -> Result<arc::R<Fn>, &'ar ns::Error> {
let mut error = None;

if let Some(func) =
unsafe { Self::new_fn_with_consts_err(self, name, constant_values, &mut error) }
{
Ok(func)
} else {
Err(error.unwrap())
}
ns::if_none(|err| unsafe { Self::new_fn_with_consts_err(self, name, constant_values, err) })
}

#[objc::msg_send(newFunctionWithName:descriptor:error:)]
pub unsafe fn new_fn_with_desc_err<'ar>(
&self,
name: &ns::String,
descriptor: &mtl::FnDesc,
error: &mut Option<&'ar ns::Error>,
error: *mut Option<&'ar ns::Error>,
) -> Option<arc::R<Fn>>;

pub fn new_fn_with_desc<'ar>(
&self,
name: &ns::String,
descriptor: &mtl::FnDesc,
) -> Result<arc::R<Fn>, &'ar ns::Error> {
let mut error = None;

if let Some(func) =
unsafe { Self::new_fn_with_desc_err(self, name, descriptor, &mut error) }
{
Ok(func)
} else {
Err(unsafe { error.unwrap_unchecked() })
}
ns::if_none(|err| unsafe { Self::new_fn_with_desc_err(self, name, descriptor, err) })
}

#[objc::msg_send(type)]
Expand Down

0 comments on commit 0353f95

Please sign in to comment.