Skip to content

Commit b4e04f9

Browse files
authored
Remove write access to ConvexPolygon.vertices (#15965)
# Objective - Fixes #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.
1 parent 76744bf commit b4e04f9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

crates/bevy_math/src/primitives/dim2.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,14 @@ impl<const N: usize> Polygon<N> {
15921592
}
15931593
}
15941594

1595+
impl<const N: usize> From<ConvexPolygon<N>> for Polygon<N> {
1596+
fn from(val: ConvexPolygon<N>) -> Self {
1597+
Polygon {
1598+
vertices: val.vertices,
1599+
}
1600+
}
1601+
}
1602+
15951603
/// A convex polygon with `N` vertices.
15961604
#[derive(Clone, Debug, PartialEq)]
15971605
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
@@ -1603,7 +1611,7 @@ impl<const N: usize> Polygon<N> {
16031611
pub struct ConvexPolygon<const N: usize> {
16041612
/// The vertices of the [`ConvexPolygon`].
16051613
#[cfg_attr(feature = "serialize", serde(with = "super::serde::array"))]
1606-
pub vertices: [Vec2; N],
1614+
vertices: [Vec2; N],
16071615
}
16081616
impl<const N: usize> Primitive2d for ConvexPolygon<N> {}
16091617

@@ -1651,6 +1659,20 @@ impl<const N: usize> ConvexPolygon<N> {
16511659
pub fn new_unchecked(vertices: [Vec2; N]) -> Self {
16521660
Self { vertices }
16531661
}
1662+
1663+
/// Get the vertices of this polygon
1664+
#[inline(always)]
1665+
pub fn vertices(&self) -> &[Vec2; N] {
1666+
&self.vertices
1667+
}
1668+
}
1669+
1670+
impl<const N: usize> TryFrom<Polygon<N>> for ConvexPolygon<N> {
1671+
type Error = ConvexPolygonError;
1672+
1673+
fn try_from(val: Polygon<N>) -> Result<Self, Self::Error> {
1674+
ConvexPolygon::new(val.vertices)
1675+
}
16541676
}
16551677

16561678
/// A polygon with a variable number of vertices, allocated on the heap

crates/bevy_mesh/src/primitives/dim2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl<const N: usize> Meshable for ConvexPolygon<N> {
408408

409409
fn mesh(&self) -> Self::Output {
410410
Self::Output {
411-
vertices: self.vertices,
411+
vertices: *self.vertices(),
412412
}
413413
}
414414
}

0 commit comments

Comments
 (0)