-
Notifications
You must be signed in to change notification settings - Fork 77
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
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
09cabd6
Initial implementation of a StaticShape type
ratmice dff2864
Appease clippy
ratmice 14bd56c
Add copyright notice
ratmice bc9d1ce
macroize match statements as suggested
ratmice 5e6a4a4
appease rustfmt and clippy
ratmice 4a31d30
rename StaticShape to to PrimitiveShape
ratmice c60ab62
rustfmt
ratmice cd90bf4
Rename `ConcreteShape`
ratmice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
|
||
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) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 toRect
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.