Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation of a StaticShape type #331

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ mod rounded_rect_radii;
mod shape;
pub mod simplify;
mod size;
mod static_shape;
mod stroke;
#[cfg(feature = "std")]
mod svg;
Expand All @@ -132,6 +133,7 @@ pub use crate::rounded_rect::*;
pub use crate::rounded_rect_radii::*;
pub use crate::shape::*;
pub use crate::size::*;
pub use crate::static_shape::*;
pub use crate::stroke::*;
#[cfg(feature = "std")]
pub use crate::svg::*;
Expand Down
174 changes: 174 additions & 0 deletions src/static_shape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Copyright 2024 the Kurbo Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![allow(unused_qualifications)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see a way to get this lint working, while leaving it enabled
it basically complains that we use crate::$it where $it evaluates to Rect because that is already in scope.
but the macro is called for other types which aren't in scope and for which the qualification isn't unnecessary.


use crate::{Point, Rect, Shape};
use alloc::boxed::Box;

mod _never_shape {
use super::*;
use crate::PathEl;
/// An uninhabited type that implements shape.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NeverShape {}
impl Shape for NeverShape {
type PathElementsIter<'a> = core::iter::Empty<PathEl>;
fn path_elements(&self, _: f64) -> Self::PathElementsIter<'_> {
unreachable!()
}
fn area(&self) -> f64 {
unreachable!()
}
fn perimeter(&self, _: f64) -> f64 {
unreachable!()
}
fn winding(&self, _: Point) -> i32 {
unreachable!()
}
fn bounding_box(&self) -> Rect {
unreachable!()
}
}
}

/// Because the `Shape` trait is not dyn safe, it can be difficult to store
/// Collections of `Shape` items in hetereogenuous collections.
///
/// It allows an external `Shape` impl to be provided as an extension point
/// for shape impls provided by external crates. This defaults to an
/// uninhabited type.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
pub enum StaticShape<External = _never_shape::NeverShape>
where
External: Shape,
{
/// Corresponds to a `PathSeg`
PathSeg(crate::PathSeg),
/// Corresponds to an `Arc`
Arc(crate::Arc),
/// Corresponds to a `BezPath`
BezPath(crate::BezPath),
/// Corresponds to a `Circle`
Circle(crate::Circle),
/// Corresponds to a `CircleSegment`
CircleSegment(crate::CircleSegment),
/// Corresponds to a `CubicBez`
CubicBez(crate::CubicBez),
/// Corresponds to an `Ellipse`
Ellipse(crate::Ellipse),
/// Corresponds to a `Line`
Line(crate::Line),
/// Corresponds to a `QuadBez`
QuadBez(crate::QuadBez),
/// Corresponds to a `Rect`
Rect(Rect),
/// Corresponds to a `RoundedRect`
RoundedRect(crate::RoundedRect),
/// A type implementing shape that may be defined by an external crate.
External(External),
}

macro_rules! from_shape {
($it: ident) => {
impl From<crate::$it> for StaticShape {
fn from(it: crate::$it) -> Self {
Self::$it(it)
}
}
};
}

from_shape!(PathSeg);
from_shape!(Arc);
from_shape!(BezPath);
from_shape!(Circle);
from_shape!(CircleSegment);
from_shape!(CubicBez);
from_shape!(Ellipse);
from_shape!(Line);
from_shape!(QuadBez);
from_shape!(Rect);
from_shape!(RoundedRect);

impl StaticShape {
/// Builds a static shape from an external shape implementation.
///
/// For a kurbo provided shape implementation, you would normally use the `from` impl instead.
pub fn from_external_shape<External>(shape: External) -> StaticShape<External>
where
External: Shape,
{
StaticShape::External(shape)
}
}

macro_rules! match_shape {
($x:ident, $it:ident, $e: expr) => {
match $x {
StaticShape::PathSeg($it) => $e,
StaticShape::Arc($it) => $e,
StaticShape::BezPath($it) => $e,
StaticShape::Circle($it) => $e,
StaticShape::CircleSegment($it) => $e,
StaticShape::CubicBez($it) => $e,
StaticShape::Ellipse($it) => $e,
StaticShape::Line($it) => $e,
StaticShape::QuadBez($it) => $e,
StaticShape::Rect($it) => $e,
StaticShape::RoundedRect($it) => $e,
StaticShape::External($it) => $e,
}
};
}

impl<External> Shape for StaticShape<External>
where
External: Shape,
{
type PathElementsIter<'iter> = Box<dyn Iterator<Item = crate::PathEl> + 'iter> where External: 'iter;
fn path_elements(&self, tol: f64) -> Box<dyn Iterator<Item = crate::PathEl> + '_> {
match_shape!(self, it, Box::new(it.path_elements(tol)))
}

fn perimeter(&self, acc: f64) -> f64 {
match_shape!(self, it, it.perimeter(acc))
}

fn area(&self) -> f64 {
match_shape!(self, it, it.area())
}

fn winding(&self, pt: Point) -> i32 {
match_shape!(self, it, it.winding(pt))
}

fn bounding_box(&self) -> Rect {
match_shape!(self, it, it.bounding_box())
}
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_collection() {
let r = crate::Rect::from_origin_size((0.0, 0.0), (1.0, 1.0));
let l = crate::Line::new((0.0, 0.0), (0.5, 0.5));
let shapes: Vec<StaticShape> = vec![r.into(), l.into()];
assert_eq!(shapes, vec![StaticShape::Rect(r), StaticShape::Line(l),]);
}
#[test]
fn test_external() {
let l = crate::Line::new((0.0, 0.0), (0.5, 0.5));
assert_eq!(
StaticShape::from_external_shape(l),
StaticShape::External(l)
);
}
}
Loading