Skip to content

Commit

Permalink
get rid of transmutes
Browse files Browse the repository at this point in the history
  • Loading branch information
rlidwka committed Aug 30, 2023
1 parent 23b4cc5 commit e14cf92
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
9 changes: 3 additions & 6 deletions physx/src/convex_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,12 @@ impl ConvexMesh {

/// Returns the vertices.
pub fn get_vertices(&self) -> &[PxVec3] {
let vertices = unsafe {
unsafe {
std::slice::from_raw_parts(
PxConvexMesh_getVertices(self.as_ptr()),
PxConvexMesh_getVertices(self.as_ptr()) as *const PxVec3,
self.get_nb_vertices() as usize,
)
};

// SAFETY: PxVec3 is repr(transparent) wrapper of physx_sys::PxVec3
unsafe { std::mem::transmute(vertices) }
}
}

/// Returns the index buffer.
Expand Down
3 changes: 1 addition & 2 deletions physx/src/height_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,8 @@ impl HeightField {
pub fn get_sample(&self, row: u32, column: u32) -> Option<&HeightFieldSample> {
// need to do bound checks, otherwise C++ code will crash with assertion error
if row < self.get_nb_rows() || column < self.get_nb_columns() {
// SAFETY: HeightFieldSample is repr(transparent) of PxHeightFieldSample
Some(unsafe {
std::mem::transmute(&*PxHeightField_getSample(self.as_ptr(), row, column))
&*(PxHeightField_getSample(self.as_ptr(), row, column) as *const HeightFieldSample)
})
} else {
None
Expand Down
20 changes: 7 additions & 13 deletions physx/src/triangle_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,22 @@ impl TriangleMesh {

/// Returns the vertices.
pub fn get_vertices(&self) -> &[PxVec3] {
let vertices = unsafe {
unsafe {
std::slice::from_raw_parts(
PxTriangleMesh_getVertices(self.as_ptr()),
PxTriangleMesh_getVertices(self.as_ptr()) as *const PxVec3,
self.get_nb_vertices() as usize,
)
};

// SAFETY: PxVec3 is repr(transparent) wrapper of physx_sys::PxVec3
unsafe { std::mem::transmute(vertices) }
}
}

/// Returns all mesh vertices for modification.
pub fn get_vertices_for_modification(&mut self) -> &mut [PxVec3] {
let vertices = unsafe {
pub fn get_vertices_mut(&mut self) -> &mut [PxVec3] {
unsafe {
std::slice::from_raw_parts_mut(
PxTriangleMesh_getVerticesForModification_mut(self.as_mut_ptr()),
PxTriangleMesh_getVerticesForModification_mut(self.as_mut_ptr()) as *mut PxVec3,
self.get_nb_vertices() as usize,
)
};

// SAFETY: PxVec3 is repr(transparent) wrapper of physx_sys::PxVec3
unsafe { std::mem::transmute(vertices) }
}
}

/// Refits BVH for mesh vertices.
Expand Down

0 comments on commit e14cf92

Please sign in to comment.