Skip to content

Commit

Permalink
capi: remove dependency on try_blocks
Browse files Browse the repository at this point in the history
Uses an IIFE + return type changes to remove the need for try blocks
  • Loading branch information
chyyran committed Sep 15, 2024
1 parent 8b2ff57 commit e6036fb
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 32 deletions.
12 changes: 8 additions & 4 deletions librashader-capi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ impl LibrashaderError {
}

macro_rules! assert_non_null {
($value:ident) => {
(@EXPORT $value:ident) => {
if $value.is_null() || !$crate::ffi::ptr_is_aligned($value) {
return $crate::error::LibrashaderError::InvalidParameter(stringify!($value)).export();
}
};
(noexport $value:ident) => {
($value:ident) => {
if $value.is_null() || !$crate::ffi::ptr_is_aligned($value) {
return Err($crate::error::LibrashaderError::InvalidParameter(
stringify!($value),
Expand All @@ -228,14 +228,18 @@ macro_rules! assert_non_null {
macro_rules! assert_some_ptr {
($value:ident) => {
if $value.is_none() {
return $crate::error::LibrashaderError::InvalidParameter(stringify!($value)).export();
return Err($crate::error::LibrashaderError::InvalidParameter(
stringify!($value),
));
}

let $value = unsafe { $value.as_ref().unwrap_unchecked().as_ref() };
};
(mut $value:ident) => {
if $value.is_none() {
return $crate::error::LibrashaderError::InvalidParameter(stringify!($value)).export();
return Err($crate::error::LibrashaderError::InvalidParameter(
stringify!($value),
));
}

let $value = unsafe { $value.as_mut().unwrap_unchecked().as_mut() };
Expand Down
37 changes: 22 additions & 15 deletions librashader-capi/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#[macro_export]
macro_rules! wrap_ok {
($e:expr) => {
::core::iter::empty().try_fold($e, |_, __x: ::core::convert::Infallible| match __x {})
};
}

macro_rules! ffi_body {
(nopanic $body:block) => {
{
let result: Result<(), $crate::error::LibrashaderError> = try {
let result: Result<(), $crate::error::LibrashaderError> = (|| $crate::ffi::wrap_ok!({
$body
};
}))();

let Err(e) = result else {
return $crate::error::LibrashaderError::ok()
Expand All @@ -22,13 +29,13 @@ macro_rules! ffi_body {
};
(nopanic |$($ref_capture:ident),*|; mut |$($mut_capture:ident),*| $body:block) => {
{
$($crate::error::assert_non_null!($ref_capture);)*
$($crate::error::assert_non_null!(@EXPORT $ref_capture);)*
$(let $ref_capture = unsafe { &*$ref_capture };)*
$($crate::error::assert_non_null!($mut_capture);)*
$($crate::error::assert_non_null!(@EXPORT $mut_capture);)*
$(let $mut_capture = unsafe { &mut *$mut_capture };)*
let result: Result<(), $crate::error::LibrashaderError> = try {
let result: Result<(), $crate::error::LibrashaderError> = (|| $crate::ffi::wrap_ok!({
$body
};
}))();

let Err(e) = result else {
return $crate::error::LibrashaderError::ok()
Expand All @@ -47,11 +54,11 @@ macro_rules! ffi_body {
};
(nopanic mut |$($mut_capture:ident),*| $body:block) => {
{
$($crate::error::assert_non_null!($mut_capture);)*
$($crate::error::assert_non_null!(@EXPORT $mut_capture);)*
$(let $mut_capture = unsafe { &mut *$mut_capture };)*
let result: Result<(), $crate::error::LibrashaderError> = try {
let result: Result<(), $crate::error::LibrashaderError> = (|| $crate::ffi::wrap_ok!({
$body
};
}))();

let Err(e) = result else {
return $crate::error::LibrashaderError::ok()
Expand All @@ -70,11 +77,11 @@ macro_rules! ffi_body {
};
(nopanic |$($ref_capture:ident),*| $body:block) => {
{
$($crate::error::assert_non_null!($ref_capture);)*
$($crate::error::assert_non_null!(@EXPORT $ref_capture);)*
$(let $ref_capture = unsafe { &*$ref_capture };)*
let result: Result<(), $crate::error::LibrashaderError> = try {
let result: Result<(), $crate::error::LibrashaderError> = (|| $crate::ffi::wrap_ok!({
$body
};
}))();

let Err(e) = result else {
return $crate::error::LibrashaderError::ok()
Expand Down Expand Up @@ -231,16 +238,16 @@ pub unsafe fn boxed_slice_from_raw_parts<T>(ptr: *mut T, len: usize) -> Box<[T]>
unsafe { Box::from_raw(std::slice::from_raw_parts_mut(ptr, len)) }
}

#[allow(unstable_name_collisions)]
pub fn ptr_is_aligned<T: Sized>(ptr: *const T) -> bool {
use sptr::Strict;
let align = std::mem::align_of::<T>();
if !align.is_power_of_two() {
panic!("is_aligned_to: align is not a power-of-two");
}
ptr.addr() & (align - 1) == 0
sptr::Strict::addr(ptr) & (align - 1) == 0
}

pub(crate) use extern_fn;
pub(crate) use ffi_body;
pub(crate) use wrap_ok;

use std::mem::ManuallyDrop;
1 change: 0 additions & 1 deletion librashader-capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
//! called from one thread at a time.
#![allow(non_camel_case_types)]
#![feature(try_blocks)]
#![deny(unsafe_op_in_unsafe_fn)]
#![deny(deprecated)]

Expand Down
4 changes: 2 additions & 2 deletions librashader-capi/src/runtime/d3d11/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ extern_fn! {
let name = name.to_str()?;

if chain.parameters().set_parameter_value(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
}
}
}
Expand All @@ -293,7 +293,7 @@ extern_fn! {
let name = name.to_str()?;

let Some(value) = chain.parameters().parameter_value(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
};

out.write(MaybeUninit::new(value));
Expand Down
4 changes: 2 additions & 2 deletions librashader-capi/src/runtime/d3d12/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ extern_fn! {
let name = name.to_str()?;

if chain.parameters().set_parameter_value(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
}
}
}
Expand All @@ -313,7 +313,7 @@ extern_fn! {
let name = name.to_str()?;

let Some(value) = chain.parameters().parameter_value(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
};

out.write(MaybeUninit::new(value));
Expand Down
4 changes: 2 additions & 2 deletions librashader-capi/src/runtime/d3d9/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ extern_fn! {
let name = name.to_str()?;

if chain.parameters().set_parameter_value(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
}
}
}
Expand All @@ -203,7 +203,7 @@ extern_fn! {
let name = name.to_str()?;

let Some(value) = chain.parameters().parameter_value(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
};

out.write(MaybeUninit::new(value));
Expand Down
4 changes: 2 additions & 2 deletions librashader-capi/src/runtime/gl/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ extern_fn! {
let name = name.to_str()?;

if chain.parameters().set_parameter_value(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
}
}
}
Expand All @@ -260,7 +260,7 @@ extern_fn! {
let name = name.to_str()?;

let Some(value) = chain.parameters().parameter_value(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
};

out.write(MaybeUninit::new(value));
Expand Down
4 changes: 2 additions & 2 deletions librashader-capi/src/runtime/mtl/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ extern_fn! {
let name = name.to_str()?;

if chain.parameters().set_parameter_value(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
}
}
}
Expand All @@ -261,7 +261,7 @@ extern_fn! {
let name = name.to_str()?;

let Some(value) = chain.parameters().parameter_value(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
};

out.write(MaybeUninit::new(value));
Expand Down
4 changes: 2 additions & 2 deletions librashader-capi/src/runtime/vk/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ extern_fn! {
let name = name.to_str()?;

if chain.parameters().set_parameter_value(name, value).is_none() {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
}
}
}
Expand All @@ -339,7 +339,7 @@ extern_fn! {
let name = name.to_str()?;

let Some(value) = chain.parameters().parameter_value(name) else {
return LibrashaderError::UnknownShaderParameter(param_name).export()
return Err(LibrashaderError::UnknownShaderParameter(param_name))
};

out.write(MaybeUninit::new(value));
Expand Down

0 comments on commit e6036fb

Please sign in to comment.