use crate::geometry::Geometry;
use geometry_tag::GeometryCollectionTag;
pub trait GeometryCollection: Geometry<Kind = GeometryCollectionTag> {
type Item: Geometry;
fn items(&self) -> impl ExactSizeIterator<Item = &Self::Item>;
}
#[cfg(test)]
mod tests {
extern crate alloc;
use super::*;
use crate::point::Point;
use alloc::vec;
use alloc::vec::Vec;
use geometry_cs::Cartesian;
use geometry_tag::PointTag;
fn accepts_gc<G: GeometryCollection>() {}
struct Xy(f64, f64);
impl Geometry for Xy {
type Kind = PointTag;
type Point = Self;
}
impl Point for Xy {
type Scalar = f64;
type Cs = Cartesian;
const DIM: usize = 2;
fn get<const D: usize>(&self) -> f64 {
if D == 0 { self.0 } else { self.1 }
}
}
struct ManyPoints(Vec<Xy>);
impl Geometry for ManyPoints {
type Kind = GeometryCollectionTag;
type Point = Xy;
}
impl GeometryCollection for ManyPoints {
type Item = Xy;
fn items(&self) -> impl ExactSizeIterator<Item = &Xy> {
self.0.iter()
}
}
#[test]
fn collection_iterates_all_items() {
let m = ManyPoints(vec![Xy(0.0, 0.0), Xy(1.0, 1.0), Xy(2.0, 2.0)]);
accepts_gc::<ManyPoints>();
assert_eq!(m.items().len(), 3);
let xs: Vec<f64> = m.items().map(Xy::get::<0>).collect();
assert_eq!(xs, vec![0.0, 1.0, 2.0]);
}
}