From 10a2d17b487fb6a8690d807d7698f04da4267129 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Mon, 17 Mar 2025 00:46:47 +0200 Subject: [PATCH] 0.2.0: constrain map --- Cargo.toml | 2 +- output/src/op_iter.rs | 70 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0f8b521..741205a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace.package] -version = "0.1.2" +version = "0.2.0" [workspace] resolver = "2" diff --git a/output/src/op_iter.rs b/output/src/op_iter.rs index f793a62..639812b 100644 --- a/output/src/op_iter.rs +++ b/output/src/op_iter.rs @@ -23,22 +23,70 @@ use crate::*; Push::x(item_offset, Align::w(Fixed::x(item_width, Fill::y(item)))) } -pub struct Map<'a, A, B, I, F, G>(pub PhantomData<&'a()>, pub F, pub G) where +pub struct Map< + 'a, + E, + A, + B, I: Iterator + Send + Sync, F: Fn() -> I + Send + Sync + 'a, - G: Fn(A, usize)->B + Send + Sync; + G, +> { + __: PhantomData<&'a (E, B)>, + /// Function that returns iterator over stacked components + get_iterator: F, + /// Function that returns each stacked component + get_item: G, +} -impl<'a, A, B, I, F, G> Map<'a, A, B, I, F, G> where +impl<'a, E, A, B, I, F, G> Map<'a, E, A, B, I, F, G> where I: Iterator + Send + Sync, F: Fn() -> I + Send + Sync + 'a, - G: Fn(A, usize)->B + Send + Sync { - pub fn new (f: F, g: G) -> Self { - Self(Default::default(), f, g) + pub fn new (get_iterator: F, get_item: G) -> Self { + Self { + __: Default::default(), + get_iterator, + get_item + } } } -impl<'a, E, A, B, I, F, G> Content for Map<'a, A, B, I, F, G> where +//impl<'a, E, A, B, I, F> Map<'a, E, A, B, I, F, ()> where + //E: Output, + //B: Content, + //I: Iterator + Send + Sync, + //F: Fn() -> I + Send + Sync + 'a, +//{ + //pub fn south ( + //get_iterator: F, + //get_content: impl Fn(A, usize)->B + Send + Sync, + //) -> Map<'a, E, A, B, I, F, G> where + //G: Fn(A, usize)->Push>>> + Send + Sync + //{ + //let mut offset = 0; + //Map::new(get_iterator, move|item, index|map_south( + //0.into(), + //0.into(), + //get_content(item, index) + //)) + //} + //pub fn east ( + //get_iterator: F, + //get_content: impl Fn(A, usize)->B + Send + Sync, + //) -> Map<'a, E, A, B, I, F, G> where + //G: Fn(A, usize)->Push>>> + Send + Sync + //{ + //let mut offset = 0; + //Map::new(get_iterator, move|item, index|map_east( + //0.into(), + //0.into(), + //get_content(item, index) + //)) + //} +//} + +impl<'a, E, A, B, I, F, G> Content for Map<'a, E, A, B, I, F, G> where E: Output, B: Render, I: Iterator + Send + Sync, @@ -46,12 +94,12 @@ impl<'a, E, A, B, I, F, G> Content for Map<'a, A, B, I, F, G> where G: Fn(A, usize)->B + Send + Sync { fn layout (&self, area: E::Area) -> E::Area { - let Self(_, get_iterator, callback) = self; + let Self { get_iterator, get_item, .. } = self; let mut index = 0; let [mut min_x, mut min_y] = area.center(); let [mut max_x, mut max_y] = area.center(); for item in get_iterator() { - let [x,y,w,h] = callback(item, index).layout(area).xywh(); + let [x,y,w,h] = get_item(item, index).layout(area).xywh(); min_x = min_x.min(x.into()); min_y = min_y.min(y.into()); max_x = max_x.max((x + w).into()); @@ -64,11 +112,11 @@ impl<'a, E, A, B, I, F, G> Content for Map<'a, A, B, I, F, G> where area.center_xy([w.into(), h.into()].into()).into() } fn render (&self, to: &mut E) { - let Self(_, get_iterator, callback) = self; + let Self { get_iterator, get_item, .. } = self; let mut index = 0; let area = Content::layout(self, to.area()); for item in get_iterator() { - let item = callback(item, index); + let item = get_item(item, index); //to.place(area.into(), &item); to.place(item.layout(area), &item); index += 1;