path_offset/path/
point.rs

1#[derive(Debug, Clone, Copy)]
2pub struct Point(pub f64, pub f64);
3
4pub trait PointConvert {
5    // 定义一个泛型转换方法
6    fn use_as<T>(&self) -> T
7    where
8        // T 必须能从我们的标准 Point 转换而来
9        T: From<Point>,
10        // Self 必须能被转换成我们的标准 Point
11        Point: From<Self>,
12        Self: Copy; // 假设所有点类型都是 Copy
13}
14
15impl<P> PointConvert for P
16where
17    P: Copy,
18    Point: From<P>,
19{
20    fn use_as<T>(&self) -> T
21    where
22        T: From<Point>,
23    {
24        // 核心逻辑:通过我们的标准 Point 作为中介
25        let canonical_point = Point::from(*self);
26        T::from(canonical_point)
27    }
28}
29
30impl From<lyon::math::Point> for Point {
31    fn from(value: lyon::math::Point) -> Self {
32        Self(value.x as f64, value.y as f64)
33    }
34}
35impl From<Point> for lyon::math::Point {
36    fn from(point: Point) -> Self {
37        lyon::geom::euclid::point2(point.0 as f32, point.1 as f32)
38    }
39}
40
41impl From<flo_curves::bezier::Coord2> for Point {
42    fn from(value: flo_curves::bezier::Coord2) -> Self {
43        Self(value.0, value.1)
44    }
45}
46impl From<Point> for flo_curves::bezier::Coord2 {
47    fn from(point: Point) -> Self {
48        flo_curves::bezier::Coord2(point.0, point.1)
49    }
50}