standing_relations/core/
relation.rs

1use crate::core::{
2    context::{ContextTracker, TrackingIndex},
3    dirty::ReceiveBuilder,
4    pipes::CountSender,
5    Op_,
6};
7
8pub struct Relation<C: Op_> {
9    pub(super) context_tracker: ContextTracker,
10    pub(super) tracking_index: TrackingIndex,
11    pub(super) shown_index: TrackingIndex,
12    pub(super) dirty: ReceiveBuilder,
13    pub(super) inner: RelationInner<C>,
14}
15
16pub(super) struct RelationInner<C: ?Sized> {
17    pub counter: CountSender,
18    pub inner: C,
19}
20
21impl<C> RelationInner<C> {
22    pub fn new(inner: C, counter: CountSender) -> Self {
23        RelationInner { counter, inner }
24    }
25}
26
27impl<C: Op_> RelationInner<C> {
28    pub fn foreach(&mut self, f: impl FnMut(C::T)) {
29        self.inner.foreach(with_counter(&mut self.counter, f))
30    }
31    pub fn get_vec(&mut self) -> Vec<C::T> {
32        let mut result = Vec::new();
33        self.foreach(|x| result.push(x));
34        result
35    }
36}
37
38pub(super) fn with_counter<'a, T>(
39    counter: &'a mut CountSender,
40    mut f: impl FnMut(T) + 'a,
41) -> impl FnMut(T) + 'a {
42    move |x| {
43        counter.increment();
44        f(x)
45    }
46}
47
48impl<C: Op_> Relation<C> {
49    pub fn tracking_index(&self) -> TrackingIndex {
50        self.tracking_index
51    }
52    pub fn named(mut self, name: &str) -> Self {
53        self.context_tracker
54            .set_name(self.shown_index, name.to_string());
55        self
56    }
57    pub fn type_named(mut self, type_name: &str) -> Self {
58        self.context_tracker
59            .set_type_name(self.shown_index, type_name.to_string());
60        self
61    }
62    pub fn hidden(mut self) -> Self {
63        self.context_tracker.set_hidden(self.shown_index);
64        self.shown_index = self.context_tracker.find_shown_index(self.shown_index);
65        self
66    }
67}