Skip to content

Commit

Permalink
rust - from_raw is unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylt committed Oct 24, 2024
1 parent e3ae47f commit 2b671a0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion rust/libceed/src/basis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl<'a> Basis<'a> {
})
}

pub(crate) fn from_raw(ptr: bind_ceed::CeedBasis) -> crate::Result<Self> {
pub(crate) unsafe fn from_raw(ptr: bind_ceed::CeedBasis) -> crate::Result<Self> {
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand Down
10 changes: 5 additions & 5 deletions rust/libceed/src/elem_restriction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<'a> ElemRestriction<'a> {
})
}

pub(crate) fn from_raw(ptr: bind_ceed::CeedElemRestriction) -> crate::Result<Self> {
pub(crate) unsafe fn from_raw(ptr: bind_ceed::CeedElemRestriction) -> crate::Result<Self> {
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand Down Expand Up @@ -351,7 +351,7 @@ impl<'a> ElemRestriction<'a> {
self.check_error(unsafe {
bind_ceed::CeedElemRestrictionCreateVector(self.ptr, &mut ptr_lvector, null)
})?;
Vector::from_raw(ptr_lvector)
unsafe { Vector::from_raw(ptr_lvector) }
}

/// Create an Evector for an ElemRestriction
Expand Down Expand Up @@ -380,7 +380,7 @@ impl<'a> ElemRestriction<'a> {
self.check_error(unsafe {
bind_ceed::CeedElemRestrictionCreateVector(self.ptr, null, &mut ptr_evector)
})?;
Vector::from_raw(ptr_evector)
unsafe { Vector::from_raw(ptr_evector) }
}

/// Create Vectors for an ElemRestriction
Expand Down Expand Up @@ -410,8 +410,8 @@ impl<'a> ElemRestriction<'a> {
self.check_error(unsafe {
bind_ceed::CeedElemRestrictionCreateVector(self.ptr, &mut ptr_lvector, &mut ptr_evector)
})?;
let lvector = Vector::from_raw(ptr_lvector)?;
let evector = Vector::from_raw(ptr_evector)?;
let lvector = unsafe { Vector::from_raw(ptr_lvector)? };
let evector = unsafe { Vector::from_raw(ptr_evector)? };
Ok((lvector, evector))
}

Expand Down
39 changes: 19 additions & 20 deletions rust/libceed/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,26 @@ pub struct OperatorField<'a> {
// Implementations
// -----------------------------------------------------------------------------
impl<'a> OperatorField<'a> {
pub(crate) fn from_raw(
pub(crate) unsafe fn from_raw(
ptr: bind_ceed::CeedOperatorField,
ceed: crate::Ceed,
) -> crate::Result<Self> {
let vector = {
let mut vector_ptr = std::ptr::null_mut();
ceed.check_error(unsafe {
bind_ceed::CeedOperatorFieldGetVector(ptr, &mut vector_ptr)
})?;
ceed.check_error(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();
ceed.check_error(unsafe {
bind_ceed::CeedOperatorFieldGetElemRestriction(ptr, &mut elem_restriction_ptr)
})?;
ceed.check_error(bind_ceed::CeedOperatorFieldGetElemRestriction(
ptr,
&mut elem_restriction_ptr,
))?;
crate::ElemRestriction::from_raw(elem_restriction_ptr)?
};
let basis = {
let mut basis_ptr = std::ptr::null_mut();
ceed.check_error(unsafe { bind_ceed::CeedOperatorFieldGetBasis(ptr, &mut basis_ptr) })?;
ceed.check_error(bind_ceed::CeedOperatorFieldGetBasis(ptr, &mut basis_ptr))?;
crate::Basis::from_raw(basis_ptr)?
};
Ok(Self {
Expand Down Expand Up @@ -558,7 +557,7 @@ impl<'a> Operator<'a> {
})
}

fn from_raw(ptr: bind_ceed::CeedOperator) -> crate::Result<Self> {
unsafe fn from_raw(ptr: bind_ceed::CeedOperator) -> crate::Result<Self> {
Ok(Self {
op_core: OperatorCore {
ptr,
Expand Down Expand Up @@ -881,7 +880,7 @@ impl<'a> Operator<'a> {
crate::Ceed { ptr }
};
let inputs = (0..num_inputs as usize)
.map(|i| crate::OperatorField::from_raw(inputs_slice[i], ceed.clone()))
.map(|i| unsafe { crate::OperatorField::from_raw(inputs_slice[i], ceed.clone()) })
.collect::<crate::Result<Vec<_>>>()?;
Ok(inputs)
}
Expand Down Expand Up @@ -951,7 +950,7 @@ impl<'a> Operator<'a> {
crate::Ceed { ptr }
};
let outputs = (0..num_outputs as usize)
.map(|i| crate::OperatorField::from_raw(outputs_slice[i], ceed.clone()))
.map(|i| unsafe { crate::OperatorField::from_raw(outputs_slice[i], ceed.clone()) })
.collect::<crate::Result<Vec<_>>>()?;
Ok(outputs)
}
Expand Down Expand Up @@ -1720,9 +1719,9 @@ impl<'a> Operator<'a> {
&mut ptr_restrict,
)
})?;
let op_coarse = Operator::from_raw(ptr_coarse)?;
let op_prolong = Operator::from_raw(ptr_prolong)?;
let op_restrict = Operator::from_raw(ptr_restrict)?;
let op_coarse = unsafe { Operator::from_raw(ptr_coarse)? };
let op_prolong = unsafe { Operator::from_raw(ptr_prolong)? };
let op_restrict = unsafe { Operator::from_raw(ptr_restrict)? };
Ok((op_coarse, op_prolong, op_restrict))
}

Expand Down Expand Up @@ -1910,9 +1909,9 @@ impl<'a> Operator<'a> {
&mut ptr_restrict,
)
})?;
let op_coarse = Operator::from_raw(ptr_coarse)?;
let op_prolong = Operator::from_raw(ptr_prolong)?;
let op_restrict = Operator::from_raw(ptr_restrict)?;
let op_coarse = unsafe { Operator::from_raw(ptr_coarse)? };
let op_prolong = unsafe { Operator::from_raw(ptr_prolong)? };
let op_restrict = unsafe { Operator::from_raw(ptr_restrict)? };
Ok((op_coarse, op_prolong, op_restrict))
}

Expand Down Expand Up @@ -2100,9 +2099,9 @@ impl<'a> Operator<'a> {
&mut ptr_restrict,
)
})?;
let op_coarse = Operator::from_raw(ptr_coarse)?;
let op_prolong = Operator::from_raw(ptr_prolong)?;
let op_restrict = Operator::from_raw(ptr_restrict)?;
let op_coarse = unsafe { Operator::from_raw(ptr_coarse)? };
let op_prolong = unsafe { Operator::from_raw(ptr_prolong)? };
let op_restrict = unsafe { Operator::from_raw(ptr_restrict)? };
Ok((op_coarse, op_prolong, op_restrict))
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/libceed/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl<'a> VectorSliceWrapper<'a> {
)
})?;
Ok(Self {
vector: crate::Vector::from_raw(vec.ptr_copy_mut()?)?,
vector: unsafe { crate::Vector::from_raw(vec.ptr_copy_mut()?)? },
_slice: slice,
})
}
Expand Down Expand Up @@ -252,7 +252,7 @@ impl<'a> Vector<'a> {
})
}

pub(crate) fn from_raw(ptr: bind_ceed::CeedVector) -> crate::Result<Self> {
pub(crate) unsafe fn from_raw(ptr: bind_ceed::CeedVector) -> crate::Result<Self> {
Ok(Self {
ptr,
_lifeline: PhantomData,
Expand Down

0 comments on commit 2b671a0

Please sign in to comment.