Skip to main content

lox/util/
mod.rs

1//! Various helper traits and types.
2
3use lina::Point3;
4
5use crate::hsize;
6
7
8mod list;
9mod num;
10mod prop;
11
12pub use self::{
13    list::{TriList, DiList, TriListIntoIter, DiListIntoIter, TriListIter, DiListIter},
14    num::{CastFromPrimitive, CastIntoPrimitive, PrimitiveNum, PrimitiveCast, PrimitiveFloat},
15    prop::{ColorLike, PrimitiveColorChannel, Pos3Like, Vec3Like},
16};
17
18
19// ===========================================================================
20// ===== Extension traits
21// ===========================================================================
22
23/// Extension trait to add some useful methods to any type implementing
24/// `Iterator`.
25pub trait IteratorExt: Sized + Iterator {
26    fn into_vec(self) -> Vec<Self::Item> {
27        self.collect()
28    }
29
30    fn centroid(self) -> Option<Self::Item>
31    where
32        Self::Item: Pos3Like,
33    {
34        Point3::centroid(self.map(|item| item.to_point3())).map(|p| p.convert())
35    }
36}
37
38impl<I: Iterator> IteratorExt for I {}
39
40
41/// Extension trait to add a few useful methods to `hsize`.
42pub trait HSizeExt {
43    /// Returns a new index.
44    ///
45    /// When the index space has been exhausted and there is no new index, this
46    /// function either panics or returns an old index. In debug mode, this
47    /// function is guaranteed to panic in this case.
48    fn next(self) -> Self;
49}
50
51impl HSizeExt for hsize {
52    #[inline(always)]
53    fn next(self) -> Self {
54        self + 1
55    }
56}