From 656ef1e5dc8a79430517f8559593f57085006221 Mon Sep 17 00:00:00 2001 From: Jeremy L Thompson Date: Wed, 23 Oct 2024 15:27:06 -0600 Subject: [PATCH] rust - directly pass ierr to check_error() --- rust/libceed/src/basis.rs | 36 +++++----- rust/libceed/src/elem_restriction.rs | 47 ++++++------ rust/libceed/src/lib.rs | 4 +- rust/libceed/src/operator.rs | 103 +++++++++++---------------- rust/libceed/src/qfunction.rs | 56 ++++++--------- rust/libceed/src/vector.rs | 67 +++++++---------- 6 files changed, 130 insertions(+), 183 deletions(-) diff --git a/rust/libceed/src/basis.rs b/rust/libceed/src/basis.rs index 9b8ef714dd..a3188f7bfb 100644 --- a/rust/libceed/src/basis.rs +++ b/rust/libceed/src/basis.rs @@ -152,7 +152,7 @@ impl<'a> Basis<'a> { i32::try_from(P1d).unwrap(), i32::try_from(Q1d).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateTensorH1( ceed.ptr, dim, @@ -165,8 +165,7 @@ impl<'a> Basis<'a> { qweight1d.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -196,10 +195,9 @@ impl<'a> Basis<'a> { i32::try_from(Q).unwrap(), qmode as bind_ceed::CeedQuadMode, ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateTensorH1Lagrange(ceed.ptr, dim, ncomp, P, Q, qmode, &mut ptr) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -224,7 +222,7 @@ impl<'a> Basis<'a> { i32::try_from(nnodes).unwrap(), i32::try_from(nqpts).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateH1( ceed.ptr, topo, @@ -237,8 +235,7 @@ impl<'a> Basis<'a> { qweight.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -263,7 +260,7 @@ impl<'a> Basis<'a> { i32::try_from(nnodes).unwrap(), i32::try_from(nqpts).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateHdiv( ceed.ptr, topo, @@ -276,8 +273,7 @@ impl<'a> Basis<'a> { qweight.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -302,7 +298,7 @@ impl<'a> Basis<'a> { i32::try_from(nnodes).unwrap(), i32::try_from(nqpts).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedBasisCreateHcurl( ceed.ptr, topo, @@ -315,8 +311,7 @@ impl<'a> Basis<'a> { qweight.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -413,9 +408,9 @@ impl<'a> Basis<'a> { tmode as bind_ceed::CeedTransposeMode, emode as bind_ceed::CeedEvalMode, ); - let ierr = - unsafe { bind_ceed::CeedBasisApply(self.ptr, nelem, tmode, emode, u.ptr, v.ptr) }; - self.check_error(ierr) + self.check_error(unsafe { + bind_ceed::CeedBasisApply(self.ptr, nelem, tmode, emode, u.ptr, v.ptr) + }) } /// Returns the dimension for given Basis @@ -532,8 +527,9 @@ impl<'a> Basis<'a> { /// ``` pub fn create_projection(&self, to: &Self) -> crate::Result { let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedBasisCreateProjection(self.ptr, to.ptr, &mut ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { + bind_ceed::CeedBasisCreateProjection(self.ptr, to.ptr, &mut ptr) + })?; Ok(Self { ptr, _lifeline: PhantomData, diff --git a/rust/libceed/src/elem_restriction.rs b/rust/libceed/src/elem_restriction.rs index e2779e876c..1bbf6b0643 100644 --- a/rust/libceed/src/elem_restriction.rs +++ b/rust/libceed/src/elem_restriction.rs @@ -172,7 +172,7 @@ impl<'a> ElemRestriction<'a> { isize::try_from(lsize).unwrap(), mtype as bind_ceed::CeedMemType, ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedElemRestrictionCreate( ceed.ptr, nelem, @@ -185,8 +185,7 @@ impl<'a> ElemRestriction<'a> { offsets.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -220,7 +219,7 @@ impl<'a> ElemRestriction<'a> { isize::try_from(lsize).unwrap(), mtype as bind_ceed::CeedMemType, ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedElemRestrictionCreateOriented( ceed.ptr, nelem, @@ -234,8 +233,7 @@ impl<'a> ElemRestriction<'a> { orients.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -262,7 +260,7 @@ impl<'a> ElemRestriction<'a> { isize::try_from(lsize).unwrap(), mtype as bind_ceed::CeedMemType, ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedElemRestrictionCreateCurlOriented( ceed.ptr, nelem, @@ -276,8 +274,7 @@ impl<'a> ElemRestriction<'a> { curlorients.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -299,7 +296,7 @@ impl<'a> ElemRestriction<'a> { i32::try_from(ncomp).unwrap(), isize::try_from(lsize).unwrap(), ); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedElemRestrictionCreateStrided( ceed.ptr, nelem, @@ -309,8 +306,7 @@ impl<'a> ElemRestriction<'a> { strides.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -352,9 +348,9 @@ impl<'a> ElemRestriction<'a> { pub fn create_lvector<'b>(&self) -> crate::Result> { let mut ptr_lvector = std::ptr::null_mut(); let null = std::ptr::null_mut() as *mut _; - let ierr = - unsafe { bind_ceed::CeedElemRestrictionCreateVector(self.ptr, &mut ptr_lvector, null) }; - self.check_error(ierr)?; + self.check_error(unsafe { + bind_ceed::CeedElemRestrictionCreateVector(self.ptr, &mut ptr_lvector, null) + })?; Vector::from_raw(ptr_lvector) } @@ -381,9 +377,9 @@ impl<'a> ElemRestriction<'a> { pub fn create_evector<'b>(&self) -> crate::Result> { let mut ptr_evector = std::ptr::null_mut(); let null = std::ptr::null_mut() as *mut _; - let ierr = - unsafe { bind_ceed::CeedElemRestrictionCreateVector(self.ptr, null, &mut ptr_evector) }; - self.check_error(ierr)?; + self.check_error(unsafe { + bind_ceed::CeedElemRestrictionCreateVector(self.ptr, null, &mut ptr_evector) + })?; Vector::from_raw(ptr_evector) } @@ -411,10 +407,9 @@ impl<'a> ElemRestriction<'a> { pub fn create_vectors<'b, 'c>(&self) -> crate::Result<(Vector<'b>, Vector<'c>)> { let mut ptr_lvector = std::ptr::null_mut(); let mut ptr_evector = std::ptr::null_mut(); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedElemRestrictionCreateVector(self.ptr, &mut ptr_lvector, &mut ptr_evector) - }; - self.check_error(ierr)?; + })?; let lvector = Vector::from_raw(ptr_lvector)?; let evector = Vector::from_raw(ptr_evector)?; Ok((lvector, evector)) @@ -460,7 +455,7 @@ impl<'a> ElemRestriction<'a> { /// ``` pub fn apply(&self, tmode: TransposeMode, u: &Vector, ru: &mut Vector) -> crate::Result { let tmode = tmode as bind_ceed::CeedTransposeMode; - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedElemRestrictionApply( self.ptr, tmode, @@ -468,8 +463,7 @@ impl<'a> ElemRestriction<'a> { ru.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } /// Returns the Lvector component stride @@ -630,8 +624,9 @@ impl<'a> ElemRestriction<'a> { /// # } /// ``` pub fn multiplicity(&self, mult: &mut Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedElemRestrictionGetMultiplicity(self.ptr, mult.ptr) }; - self.check_error(ierr) + self.check_error(unsafe { + bind_ceed::CeedElemRestrictionGetMultiplicity(self.ptr, mult.ptr) + }) } } diff --git a/rust/libceed/src/lib.rs b/rust/libceed/src/lib.rs index 36ad38a1c5..b248aa9fa7 100755 --- a/rust/libceed/src/lib.rs +++ b/rust/libceed/src/lib.rs @@ -228,8 +228,8 @@ impl Clone for Ceed { /// ``` fn clone(&self) -> Self { let mut ptr_clone = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedReferenceCopy(self.ptr, &mut ptr_clone) }; - self.check_error(ierr).expect("failed to clone Ceed"); + self.check_error(unsafe { bind_ceed::CeedReferenceCopy(self.ptr, &mut ptr_clone) }) + .expect("failed to clone Ceed"); Self { ptr: ptr_clone } } } diff --git a/rust/libceed/src/operator.rs b/rust/libceed/src/operator.rs index a1a5116ec2..6f2cb11409 100644 --- a/rust/libceed/src/operator.rs +++ b/rust/libceed/src/operator.rs @@ -33,22 +33,21 @@ impl<'a> OperatorField<'a> { ) -> crate::Result { let vector = { let mut vector_ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedOperatorFieldGetVector(ptr, &mut vector_ptr) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { + bind_ceed::CeedOperatorFieldGetVector(ptr, &mut vector_ptr) + })?; crate::Vector::from_raw(vector_ptr)? }; let elem_restriction = { let mut elem_restriction_ptr = std::ptr::null_mut(); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedOperatorFieldGetElemRestriction(ptr, &mut elem_restriction_ptr) - }; - ceed.check_error(ierr)?; + })?; crate::ElemRestriction::from_raw(elem_restriction_ptr)? }; let basis = { let mut basis_ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedOperatorFieldGetBasis(ptr, &mut basis_ptr) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { bind_ceed::CeedOperatorFieldGetBasis(ptr, &mut basis_ptr) })?; crate::Basis::from_raw(basis_ptr)? }; Ok(Self { @@ -453,88 +452,80 @@ impl<'a> OperatorCore<'a> { // Common implementations pub fn check(&self) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedOperatorCheckReady(self.ptr) }; - self.check_error(ierr) + self.check_error(unsafe { bind_ceed::CeedOperatorCheckReady(self.ptr) }) } pub fn name(&self, name: &str) -> crate::Result { let name_c = CString::new(name).expect("CString::new failed"); - let ierr = unsafe { bind_ceed::CeedOperatorSetName(self.ptr, name_c.as_ptr()) }; - self.check_error(ierr) + self.check_error(unsafe { bind_ceed::CeedOperatorSetName(self.ptr, name_c.as_ptr()) }) } pub fn apply(&self, input: &Vector, output: &mut Vector) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorApply( self.ptr, input.ptr, output.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn apply_add(&self, input: &Vector, output: &mut Vector) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorApplyAdd( self.ptr, input.ptr, output.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn linear_assemble_diagonal(&self, assembled: &mut Vector) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorLinearAssembleDiagonal( self.ptr, assembled.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn linear_assemble_add_diagonal(&self, assembled: &mut Vector) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorLinearAssembleAddDiagonal( self.ptr, assembled.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn linear_assemble_point_block_diagonal( &self, assembled: &mut Vector, ) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorLinearAssemblePointBlockDiagonal( self.ptr, assembled.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } pub fn linear_assemble_add_point_block_diagonal( &self, assembled: &mut Vector, ) -> crate::Result { - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedOperatorLinearAssembleAddPointBlockDiagonal( self.ptr, assembled.ptr, bind_ceed::CEED_REQUEST_IMMEDIATE, ) - }; - self.check_error(ierr) + }) } } @@ -550,7 +541,7 @@ impl<'a> Operator<'a> { dqfT: impl Into>, ) -> crate::Result { let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedOperatorCreate( ceed.ptr, qf.into().to_raw(), @@ -558,8 +549,7 @@ impl<'a> Operator<'a> { dqfT.into().to_raw(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { op_core: OperatorCore { ptr, @@ -814,7 +804,7 @@ impl<'a> Operator<'a> { ) -> crate::Result { let fieldname = CString::new(fieldname).expect("CString::new failed"); let fieldname = fieldname.as_ptr() as *const i8; - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorSetField( self.op_core.ptr, fieldname, @@ -822,8 +812,7 @@ impl<'a> Operator<'a> { b.into().to_raw(), v.into().to_raw(), ) - }; - self.op_core.check_error(ierr)?; + })?; Ok(self) } @@ -866,7 +855,7 @@ impl<'a> Operator<'a> { // Get array of raw C pointers for inputs let mut num_inputs = 0; let mut inputs_ptr = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorGetFields( self.op_core.ptr, &mut num_inputs, @@ -874,8 +863,7 @@ impl<'a> Operator<'a> { std::ptr::null_mut() as *mut bind_ceed::CeedInt, std::ptr::null_mut() as *mut *mut bind_ceed::CeedOperatorField, ) - }; - self.op_core.check_error(ierr)?; + })?; // Convert raw C pointers to fixed length slice let inputs_slice = unsafe { std::slice::from_raw_parts( @@ -885,11 +873,10 @@ impl<'a> Operator<'a> { }; // And finally build vec let ceed = { + let ceed_raw = self.op_core.ceed(); let mut ptr = std::ptr::null_mut(); - let mut ptr_copy = std::ptr::null_mut(); unsafe { - bind_ceed::CeedOperatorGetCeed(self.op_core.ptr, &mut ptr); - bind_ceed::CeedReferenceCopy(ptr, &mut ptr_copy); // refcount + bind_ceed::CeedReferenceCopy(ceed_raw, &mut ptr); // refcount } crate::Ceed { ptr } }; @@ -938,7 +925,7 @@ impl<'a> Operator<'a> { // Get array of raw C pointers for outputs let mut num_outputs = 0; let mut outputs_ptr = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorGetFields( self.op_core.ptr, std::ptr::null_mut() as *mut bind_ceed::CeedInt, @@ -946,8 +933,7 @@ impl<'a> Operator<'a> { &mut num_outputs, &mut outputs_ptr, ) - }; - self.op_core.check_error(ierr)?; + })?; // Convert raw C pointers to fixed length slice let outputs_slice = unsafe { std::slice::from_raw_parts( @@ -957,11 +943,10 @@ impl<'a> Operator<'a> { }; // And finally build vec let ceed = { + let ceed_raw = self.op_core.ceed(); let mut ptr = std::ptr::null_mut(); - let mut ptr_copy = std::ptr::null_mut(); unsafe { - bind_ceed::CeedOperatorGetCeed(self.op_core.ptr, &mut ptr); - bind_ceed::CeedReferenceCopy(ptr, &mut ptr_copy); // refcount + bind_ceed::CeedReferenceCopy(ceed_raw, &mut ptr); // refcount } crate::Ceed { ptr } }; @@ -1724,7 +1709,7 @@ impl<'a> Operator<'a> { let mut ptr_coarse = std::ptr::null_mut(); let mut ptr_prolong = std::ptr::null_mut(); let mut ptr_restrict = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorMultigridLevelCreate( self.op_core.ptr, p_mult_fine.ptr, @@ -1734,8 +1719,7 @@ impl<'a> Operator<'a> { &mut ptr_prolong, &mut ptr_restrict, ) - }; - self.op_core.check_error(ierr)?; + })?; let op_coarse = Operator::from_raw(ptr_coarse)?; let op_prolong = Operator::from_raw(ptr_prolong)?; let op_restrict = Operator::from_raw(ptr_restrict)?; @@ -1914,7 +1898,7 @@ impl<'a> Operator<'a> { let mut ptr_coarse = std::ptr::null_mut(); let mut ptr_prolong = std::ptr::null_mut(); let mut ptr_restrict = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorMultigridLevelCreateTensorH1( self.op_core.ptr, p_mult_fine.ptr, @@ -1925,8 +1909,7 @@ impl<'a> Operator<'a> { &mut ptr_prolong, &mut ptr_restrict, ) - }; - self.op_core.check_error(ierr)?; + })?; let op_coarse = Operator::from_raw(ptr_coarse)?; let op_prolong = Operator::from_raw(ptr_prolong)?; let op_restrict = Operator::from_raw(ptr_restrict)?; @@ -2105,7 +2088,7 @@ impl<'a> Operator<'a> { let mut ptr_coarse = std::ptr::null_mut(); let mut ptr_prolong = std::ptr::null_mut(); let mut ptr_restrict = std::ptr::null_mut(); - let ierr = unsafe { + self.op_core.check_error(unsafe { bind_ceed::CeedOperatorMultigridLevelCreateH1( self.op_core.ptr, p_mult_fine.ptr, @@ -2116,8 +2099,7 @@ impl<'a> Operator<'a> { &mut ptr_prolong, &mut ptr_restrict, ) - }; - self.op_core.check_error(ierr)?; + })?; let op_coarse = Operator::from_raw(ptr_coarse)?; let op_prolong = Operator::from_raw(ptr_prolong)?; let op_restrict = Operator::from_raw(ptr_restrict)?; @@ -2132,8 +2114,7 @@ impl<'a> CompositeOperator<'a> { // Constructor pub fn create(ceed: &crate::Ceed) -> crate::Result { let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedCompositeOperatorCreate(ceed.ptr, &mut ptr) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { bind_ceed::CeedCompositeOperatorCreate(ceed.ptr, &mut ptr) })?; Ok(Self { op_core: OperatorCore { ptr, @@ -2414,9 +2395,9 @@ impl<'a> CompositeOperator<'a> { /// ``` #[allow(unused_mut)] pub fn sub_operator(mut self, subop: &Operator) -> crate::Result { - let ierr = - unsafe { bind_ceed::CeedCompositeOperatorAddSub(self.op_core.ptr, subop.op_core.ptr) }; - self.op_core.check_error(ierr)?; + self.op_core.check_error(unsafe { + bind_ceed::CeedCompositeOperatorAddSub(self.op_core.ptr, subop.op_core.ptr) + })?; Ok(self) } diff --git a/rust/libceed/src/qfunction.rs b/rust/libceed/src/qfunction.rs index 48c27e781a..f5b746eefd 100644 --- a/rust/libceed/src/qfunction.rs +++ b/rust/libceed/src/qfunction.rs @@ -462,17 +462,16 @@ impl<'a> QFunctionCore<'a> { v_c[i] = v[i].ptr; } let Q = i32::try_from(Q).unwrap(); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedQFunctionApply(self.ptr, Q, u_c.as_mut_ptr(), v_c.as_mut_ptr()) - }; - self.check_error(ierr) + }) } pub fn inputs(&self) -> crate::Result<&[crate::QFunctionField]> { // Get array of raw C pointers for inputs let mut num_inputs = 0; let mut inputs_ptr = std::ptr::null_mut(); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedQFunctionGetFields( self.ptr, &mut num_inputs, @@ -480,8 +479,7 @@ impl<'a> QFunctionCore<'a> { std::ptr::null_mut() as *mut bind_ceed::CeedInt, std::ptr::null_mut() as *mut *mut bind_ceed::CeedQFunctionField, ) - }; - self.check_error(ierr)?; + })?; // Convert raw C pointers to fixed length slice let inputs_slice = unsafe { std::slice::from_raw_parts( @@ -496,7 +494,7 @@ impl<'a> QFunctionCore<'a> { // Get array of raw C pointers for outputs let mut num_outputs = 0; let mut outputs_ptr = std::ptr::null_mut(); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedQFunctionGetFields( self.ptr, std::ptr::null_mut() as *mut bind_ceed::CeedInt, @@ -504,8 +502,7 @@ impl<'a> QFunctionCore<'a> { &mut num_outputs, &mut outputs_ptr, ) - }; - self.check_error(ierr)?; + })?; // Convert raw C pointers to fixed length slice let outputs_slice = unsafe { std::slice::from_raw_parts( @@ -611,7 +608,7 @@ impl<'a> QFunction<'a> { // Create QFunction let vlength = i32::try_from(vlength).unwrap(); - let mut ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedQFunctionCreateInterior( ceed.ptr, vlength, @@ -619,14 +616,14 @@ impl<'a> QFunction<'a> { source_c.as_ptr(), &mut ptr, ) - }; - ceed.check_error(ierr)?; + })?; // Set closure let mut qf_ctx_ptr = std::ptr::null_mut(); - ierr = unsafe { bind_ceed::CeedQFunctionContextCreate(ceed.ptr, &mut qf_ctx_ptr) }; - ceed.check_error(ierr)?; - ierr = unsafe { + ceed.check_error(unsafe { + bind_ceed::CeedQFunctionContextCreate(ceed.ptr, &mut qf_ctx_ptr) + })?; + ceed.check_error(unsafe { bind_ceed::CeedQFunctionContextSetData( qf_ctx_ptr, crate::MemType::Host as bind_ceed::CeedMemType, @@ -634,20 +631,16 @@ impl<'a> QFunction<'a> { std::mem::size_of::(), std::mem::transmute(trampoline_data.as_ref()), ) - }; - ceed.check_error(ierr)?; - ierr = unsafe { + })?; + ceed.check_error(unsafe { bind_ceed::CeedQFunctionContextSetDataDestroy( qf_ctx_ptr, crate::MemType::Host as bind_ceed::CeedMemType, Some(destroy_trampoline), ) - }; - ceed.check_error(ierr)?; - ierr = unsafe { bind_ceed::CeedQFunctionSetContext(ptr, qf_ctx_ptr) }; - ceed.check_error(ierr)?; - ierr = unsafe { bind_ceed::CeedQFunctionContextDestroy(&mut qf_ctx_ptr) }; - ceed.check_error(ierr)?; + })?; + ceed.check_error(unsafe { bind_ceed::CeedQFunctionSetContext(ptr, qf_ctx_ptr) })?; + ceed.check_error(unsafe { bind_ceed::CeedQFunctionContextDestroy(&mut qf_ctx_ptr) })?; Ok(Self { qf_core: QFunctionCore { ptr, @@ -767,10 +760,9 @@ impl<'a> QFunction<'a> { i32::try_from(size).unwrap(), emode as bind_ceed::CeedEvalMode, ); - let ierr = unsafe { + self.qf_core.check_error(unsafe { bind_ceed::CeedQFunctionAddInput(self.qf_core.ptr, name_c.as_ptr(), size, emode) - }; - self.qf_core.check_error(ierr)?; + })?; Ok(self) } @@ -817,10 +809,9 @@ impl<'a> QFunction<'a> { i32::try_from(size).unwrap(), emode as bind_ceed::CeedEvalMode, ); - let ierr = unsafe { + self.qf_core.check_error(unsafe { bind_ceed::CeedQFunctionAddOutput(self.qf_core.ptr, name_c.as_ptr(), size, emode) - }; - self.qf_core.check_error(ierr)?; + })?; Ok(self) } @@ -894,10 +885,9 @@ impl<'a> QFunctionByName<'a> { pub fn create(ceed: &crate::Ceed, name: &str) -> crate::Result { let name_c = CString::new(name).expect("CString::new failed"); let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { + ceed.check_error(unsafe { bind_ceed::CeedQFunctionCreateInteriorByName(ceed.ptr, name_c.as_ptr(), &mut ptr) - }; - ceed.check_error(ierr)?; + })?; Ok(Self { qf_core: QFunctionCore { ptr, diff --git a/rust/libceed/src/vector.rs b/rust/libceed/src/vector.rs index 0ffe84dfac..a6fd181907 100644 --- a/rust/libceed/src/vector.rs +++ b/rust/libceed/src/vector.rs @@ -157,16 +157,14 @@ impl<'a> VectorSliceWrapper<'a> { crate::MemType::Host as bind_ceed::CeedMemType, crate::CopyMode::UsePointer as bind_ceed::CeedCopyMode, ); - let ierr = unsafe { + vec.check_error(unsafe { bind_ceed::CeedVectorSetArray( vec.ptr, host, copy_mode, slice.as_ptr() as *mut crate::Scalar, ) - }; - vec.check_error(ierr)?; - + })?; Ok(Self { vector: crate::Vector::from_raw(vec.ptr_copy_mut()?)?, _slice: slice, @@ -247,8 +245,7 @@ impl<'a> Vector<'a> { pub fn create(ceed: &crate::Ceed, n: usize) -> crate::Result { let n = isize::try_from(n).unwrap(); let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedVectorCreate(ceed.ptr, n, &mut ptr) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { bind_ceed::CeedVectorCreate(ceed.ptr, n, &mut ptr) })?; Ok(Self { ptr, _lifeline: PhantomData, @@ -264,8 +261,7 @@ impl<'a> Vector<'a> { fn ptr_copy_mut(&mut self) -> crate::Result { let mut ptr_copy = std::ptr::null_mut(); - let ierr = unsafe { bind_ceed::CeedVectorReferenceCopy(self.ptr, &mut ptr_copy) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorReferenceCopy(self.ptr, &mut ptr_copy) })?; Ok(ptr_copy) } @@ -291,8 +287,7 @@ impl<'a> Vector<'a> { /// ``` /// ``` pub fn copy_from(&mut self, vec_source: &crate::Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorCopy(vec_source.ptr, self.ptr) }; - self.check_error(ierr) + self.check_error(unsafe { bind_ceed::CeedVectorCopy(vec_source.ptr, self.ptr) }) } /// Create a Vector from a slice @@ -340,8 +335,7 @@ impl<'a> Vector<'a> { crate::CopyMode::UsePointer as bind_ceed::CeedCopyMode, ); let v = v.as_ptr() as *mut crate::Scalar; - let ierr = unsafe { bind_ceed::CeedVectorSetArray(x.ptr, host, user_pointer, v) }; - ceed.check_error(ierr)?; + ceed.check_error(unsafe { bind_ceed::CeedVectorSetArray(x.ptr, host, user_pointer, v) })?; Ok(x) } @@ -414,8 +408,7 @@ impl<'a> Vector<'a> { /// # } /// ``` pub fn set_value(&mut self, value: crate::Scalar) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorSetValue(self.ptr, value) }; - self.check_error(ierr) + self.check_error(unsafe { bind_ceed::CeedVectorSetValue(self.ptr, value) }) } /// Set values from a slice of the same length @@ -443,15 +436,14 @@ impl<'a> Vector<'a> { crate::MemType::Host as bind_ceed::CeedMemType, crate::CopyMode::CopyValues as bind_ceed::CeedCopyMode, ); - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedVectorSetArray( self.ptr, host, copy_mode, slice.as_ptr() as *mut crate::Scalar, ) - }; - self.check_error(ierr) + }) } /// Wrap a mutable slice in a Vector of the same length @@ -530,9 +522,9 @@ impl<'a> Vector<'a> { /// # } /// ``` pub fn sync(&self, mtype: crate::MemType) -> crate::Result { - let ierr = - unsafe { bind_ceed::CeedVectorSyncArray(self.ptr, mtype as bind_ceed::CeedMemType) }; - self.check_error(ierr) + self.check_error(unsafe { + bind_ceed::CeedVectorSyncArray(self.ptr, mtype as bind_ceed::CeedMemType) + }) } /// Create an immutable view @@ -603,10 +595,9 @@ impl<'a> Vector<'a> { /// ``` pub fn norm(&self, ntype: crate::NormType) -> crate::Result { let mut res: crate::Scalar = 0.0; - let ierr = unsafe { + self.check_error(unsafe { bind_ceed::CeedVectorNorm(self.ptr, ntype as bind_ceed::CeedNormType, &mut res) - }; - self.check_error(ierr)?; + })?; Ok(res) } @@ -631,8 +622,7 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn scale(mut self, alpha: crate::Scalar) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorScale(self.ptr, alpha) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorScale(self.ptr, alpha) })?; Ok(self) } @@ -659,8 +649,7 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn axpy(mut self, alpha: crate::Scalar, x: &crate::Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorAXPY(self.ptr, alpha, x.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorAXPY(self.ptr, alpha, x.ptr) })?; Ok(self) } @@ -693,8 +682,7 @@ impl<'a> Vector<'a> { beta: crate::Scalar, x: &crate::Vector, ) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorAXPBY(self.ptr, alpha, beta, x.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorAXPBY(self.ptr, alpha, beta, x.ptr) })?; Ok(self) } @@ -722,8 +710,7 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn pointwise_mult(mut self, x: &crate::Vector, y: &crate::Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, x.ptr, y.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, x.ptr, y.ptr) })?; Ok(self) } @@ -749,8 +736,7 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn pointwise_scale(mut self, x: &crate::Vector) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, self.ptr, x.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, self.ptr, x.ptr) })?; Ok(self) } @@ -771,8 +757,9 @@ impl<'a> Vector<'a> { /// ``` #[allow(unused_mut)] pub fn pointwise_square(mut self) -> crate::Result { - let ierr = unsafe { bind_ceed::CeedVectorPointwiseMult(self.ptr, self.ptr, self.ptr) }; - self.check_error(ierr)?; + self.check_error(unsafe { + bind_ceed::CeedVectorPointwiseMult(self.ptr, self.ptr, self.ptr) + })?; Ok(self) } } @@ -793,14 +780,13 @@ impl<'a> VectorView<'a> { /// Construct a VectorView from a Vector reference fn new(vec: &'a Vector) -> crate::Result { let mut array = std::ptr::null(); - let ierr = unsafe { + vec.check_error(unsafe { bind_ceed::CeedVectorGetArrayRead( vec.ptr, crate::MemType::Host as bind_ceed::CeedMemType, &mut array, ) - }; - vec.check_error(ierr)?; + })?; Ok(Self { vec: vec, array: array, @@ -846,14 +832,13 @@ impl<'a> VectorViewMut<'a> { /// Construct a VectorViewMut from a Vector reference fn new(vec: &'a mut Vector) -> crate::Result { let mut ptr = std::ptr::null_mut(); - let ierr = unsafe { + vec.check_error(unsafe { bind_ceed::CeedVectorGetArray( vec.ptr, crate::MemType::Host as bind_ceed::CeedMemType, &mut ptr, ) - }; - vec.check_error(ierr)?; + })?; Ok(Self { vec: vec, array: ptr,