use super::*; /// A container with a main and cross component. #[derive(Default, Copy, Clone, Eq, PartialEq, Hash)] pub struct Gen { /// The main component. pub cross: T, /// The cross component. pub main: T, } impl Gen { /// Create a new instance from the two components. pub const fn new(cross: T, main: T) -> Self { Self { cross, main } } /// Create a new instance with two equal components. pub fn splat(value: T) -> Self where T: Clone, { Self { cross: value.clone(), main: value } } /// Maps the individual fields with `f`. pub fn map(self, mut f: F) -> Gen where F: FnMut(T) -> U, { Gen { cross: f(self.cross), main: f(self.main) } } /// Convert to the specific representation, given the current main axis. pub fn to_spec(self, main: SpecAxis) -> Spec { match main { SpecAxis::Horizontal => Spec::new(self.main, self.cross), SpecAxis::Vertical => Spec::new(self.cross, self.main), } } } impl Gen { /// The zero value. pub fn zero() -> Self { Self { cross: Length::zero(), main: Length::zero(), } } /// Convert to a point. pub fn to_point(self, main: SpecAxis) -> Point { self.to_spec(main).to_point() } } impl Get for Gen { type Component = T; fn get(self, axis: GenAxis) -> T { match axis { GenAxis::Cross => self.cross, GenAxis::Main => self.main, } } fn get_mut(&mut self, axis: GenAxis) -> &mut T { match axis { GenAxis::Cross => &mut self.cross, GenAxis::Main => &mut self.main, } } } impl Debug for Gen { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "Gen({:?}, {:?})", self.cross, self.main) } } /// Two generic axes of a container. #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum GenAxis { /// The minor / inline axis. Cross, /// The major / block axis. Main, } impl GenAxis { /// The other axis. pub fn other(self) -> Self { match self { Self::Cross => Self::Main, Self::Main => Self::Cross, } } }