Skip to content

Commit

Permalink
Remove write access to ConvexPolygon.vertices (bevyengine#15965)
Browse files Browse the repository at this point in the history
# Objective

- Fixes bevyengine#15963

## Solution

- Implement `TryFrom<Polygon<N> for ConvexPolygon<N>`
- Implement `From<ConvexPolygon<N>> for Polygon<N>`
- Remove `pub` from `vertices`
- Add `ConvexPolygon::vertices()` to get read only access to the
vertices of a convex polygon.
  • Loading branch information
lynn-lumen authored Oct 16, 2024
1 parent 76744bf commit b4e04f9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,14 @@ impl<const N: usize> Polygon<N> {
}
}

impl<const N: usize> From<ConvexPolygon<N>> for Polygon<N> {
fn from(val: ConvexPolygon<N>) -> Self {
Polygon {
vertices: val.vertices,
}
}
}

/// A convex polygon with `N` vertices.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -1603,7 +1611,7 @@ impl<const N: usize> Polygon<N> {
pub struct ConvexPolygon<const N: usize> {
/// The vertices of the [`ConvexPolygon`].
#[cfg_attr(feature = "serialize", serde(with = "super::serde::array"))]
pub vertices: [Vec2; N],
vertices: [Vec2; N],
}
impl<const N: usize> Primitive2d for ConvexPolygon<N> {}

Expand Down Expand Up @@ -1651,6 +1659,20 @@ impl<const N: usize> ConvexPolygon<N> {
pub fn new_unchecked(vertices: [Vec2; N]) -> Self {
Self { vertices }
}

/// Get the vertices of this polygon
#[inline(always)]
pub fn vertices(&self) -> &[Vec2; N] {
&self.vertices
}
}

impl<const N: usize> TryFrom<Polygon<N>> for ConvexPolygon<N> {
type Error = ConvexPolygonError;

fn try_from(val: Polygon<N>) -> Result<Self, Self::Error> {
ConvexPolygon::new(val.vertices)
}
}

/// A polygon with a variable number of vertices, allocated on the heap
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_mesh/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl<const N: usize> Meshable for ConvexPolygon<N> {

fn mesh(&self) -> Self::Output {
Self::Output {
vertices: self.vertices,
vertices: *self.vertices(),
}
}
}
Expand Down

0 comments on commit b4e04f9

Please sign in to comment.