Skip to content

Commit

Permalink
rust - directly pass ierr to check_error()
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylt committed Oct 23, 2024
1 parent 1154439 commit 656ef1e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 183 deletions.
36 changes: 16 additions & 20 deletions rust/libceed/src/basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -165,8 +165,7 @@ impl<'a> Basis<'a> {
qweight1d.as_ptr(),
&mut ptr,
)
};
ceed.check_error(ierr)?;
})?;
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -237,8 +235,7 @@ impl<'a> Basis<'a> {
qweight.as_ptr(),
&mut ptr,
)
};
ceed.check_error(ierr)?;
})?;
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand All @@ -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,
Expand All @@ -276,8 +273,7 @@ impl<'a> Basis<'a> {
qweight.as_ptr(),
&mut ptr,
)
};
ceed.check_error(ierr)?;
})?;
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand All @@ -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,
Expand All @@ -315,8 +311,7 @@ impl<'a> Basis<'a> {
qweight.as_ptr(),
&mut ptr,
)
};
ceed.check_error(ierr)?;
})?;
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -532,8 +527,9 @@ impl<'a> Basis<'a> {
/// ```
pub fn create_projection(&self, to: &Self) -> crate::Result<Self> {
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,
Expand Down
47 changes: 21 additions & 26 deletions rust/libceed/src/elem_restriction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -185,8 +185,7 @@ impl<'a> ElemRestriction<'a> {
offsets.as_ptr(),
&mut ptr,
)
};
ceed.check_error(ierr)?;
})?;
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand Down Expand Up @@ -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,
Expand All @@ -234,8 +233,7 @@ impl<'a> ElemRestriction<'a> {
orients.as_ptr(),
&mut ptr,
)
};
ceed.check_error(ierr)?;
})?;
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand All @@ -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,
Expand All @@ -276,8 +274,7 @@ impl<'a> ElemRestriction<'a> {
curlorients.as_ptr(),
&mut ptr,
)
};
ceed.check_error(ierr)?;
})?;
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand All @@ -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,
Expand All @@ -309,8 +306,7 @@ impl<'a> ElemRestriction<'a> {
strides.as_ptr(),
&mut ptr,
)
};
ceed.check_error(ierr)?;
})?;
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand Down Expand Up @@ -352,9 +348,9 @@ impl<'a> ElemRestriction<'a> {
pub fn create_lvector<'b>(&self) -> crate::Result<Vector<'b>> {
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)
}

Expand All @@ -381,9 +377,9 @@ impl<'a> ElemRestriction<'a> {
pub fn create_evector<'b>(&self) -> crate::Result<Vector<'b>> {
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)
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -460,16 +455,15 @@ impl<'a> ElemRestriction<'a> {
/// ```
pub fn apply(&self, tmode: TransposeMode, u: &Vector, ru: &mut Vector) -> crate::Result<i32> {
let tmode = tmode as bind_ceed::CeedTransposeMode;
let ierr = unsafe {
self.check_error(unsafe {
bind_ceed::CeedElemRestrictionApply(
self.ptr,
tmode,
u.ptr,
ru.ptr,
bind_ceed::CEED_REQUEST_IMMEDIATE,
)
};
self.check_error(ierr)
})
}

/// Returns the Lvector component stride
Expand Down Expand Up @@ -630,8 +624,9 @@ impl<'a> ElemRestriction<'a> {
/// # }
/// ```
pub fn multiplicity(&self, mult: &mut Vector) -> crate::Result<i32> {
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)
})
}
}

Expand Down
4 changes: 2 additions & 2 deletions rust/libceed/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
Expand Down
Loading

0 comments on commit 656ef1e

Please sign in to comment.