summaryrefslogtreecommitdiff
path: root/src/library/maps.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-12-09 13:29:04 +0100
committerLaurenz <laurmaedje@gmail.com>2019-12-09 13:29:04 +0100
commit7e980224354880cfda1797136a1ff886d6642662 (patch)
treec0137dcca82526faa71fd1d980a90c68dac798c8 /src/library/maps.rs
parent64f938b449b7ff5e53b6a06ed943bf9dedc1014b (diff)
Bad stack layouter 🚑
Diffstat (limited to 'src/library/maps.rs')
-rw-r--r--src/library/maps.rs38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/library/maps.rs b/src/library/maps.rs
index 01bde38b..c89d46cb 100644
--- a/src/library/maps.rs
+++ b/src/library/maps.rs
@@ -72,14 +72,14 @@ impl<K, V> ConsistentMap<K, V> where K: Hash + Eq {
/// A map for storing extents along axes.
#[derive(Debug, Clone, PartialEq)]
-pub struct ExtentMap(ConsistentMap<AxisKey, Size>);
+pub struct ExtentMap<E: ExpressionKind + Copy>(ConsistentMap<AxisKey, E>);
-impl ExtentMap {
+impl<E: ExpressionKind + Copy> ExtentMap<E> {
/// Parse an extent map from the function args.
///
/// If `enforce` is true other arguments will create an error, otherwise
/// they are left intact.
- pub fn new(args: &mut FuncArgs, enforce: bool) -> ParseResult<ExtentMap> {
+ pub fn new(args: &mut FuncArgs, enforce: bool) -> ParseResult<ExtentMap<E>> {
let mut map = ConsistentMap::new();
for arg in args.keys() {
@@ -96,22 +96,38 @@ impl ExtentMap {
}
};
- let size = Size::from_expr(arg.v.value)?;
- map.add(key, size)?;
+ let e = E::from_expr(arg.v.value)?;
+ map.add(key, e)?;
}
Ok(ExtentMap(map))
}
- /// Map from any axis key to the specific axis kind.
- pub fn apply(&self, axes: LayoutAxes, dimensions: &mut Size2D) -> LayoutResult<()> {
- let map = self.0.dedup(|key, &val| Ok((key.specific(axes), val)))?;
-
- map.with(SpecificAxisKind::Horizontal, |&val| dimensions.x = val);
- map.with(SpecificAxisKind::Vertical, |&val| dimensions.y = val);
+ /// Apply the extents on the dimensions.
+ pub fn apply<F>(
+ &self,
+ axes: LayoutAxes,
+ dimensions: &mut Size2D,
+ size: F
+ ) -> LayoutResult<()> where F: Fn(&E) -> Size {
+ let map = self.dedup(axes)?;
+ map.with(SpecificAxisKind::Horizontal, |val| dimensions.x = size(val));
+ map.with(SpecificAxisKind::Vertical, |val| dimensions.y = size(val));
+ Ok(())
+ }
+ /// Map from any axis key to the specific axis kind.
+ pub fn apply_with<F>(&self, axes: LayoutAxes, mut f: F) -> LayoutResult<()>
+ where F: FnMut(SpecificAxisKind, &E) {
+ for (&key, value) in self.dedup(axes)?.iter() {
+ f(key, value);
+ }
Ok(())
}
+
+ fn dedup(&self, axes: LayoutAxes) -> LayoutResult<ConsistentMap<SpecificAxisKind, E>> {
+ self.0.dedup(|key, &val| Ok((key.specific(axes), val)))
+ }
}
/// A map for storing padding at sides.