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.
This commit is contained in:
Lynn 2024-10-17 00:21:01 +02:00 committed by GitHub
parent 76744bf58c
commit b4e04f9d9f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 2 deletions

View file

@ -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))]
@ -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> {}
@ -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

View file

@ -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(),
}
}
}