rillrate_protocol/manifest/
binder.rs

1use crate::manifest::descriptions_list::DescriptionsListTracer;
2use derive_more::{Deref, DerefMut};
3use once_cell::sync::Lazy;
4use rill_engine::tracers::tracer::Tracer;
5use rill_protocol::flow::core::Flow;
6use rill_protocol::io::provider::Description;
7use std::ops::Deref;
8
9static DESCRIPTIONS: Lazy<DescriptionsListTracer> = Lazy::new(DescriptionsListTracer::new);
10
11/// `Binded` wraps a tracer to automatically track it in the global `DescriptionFlow`.
12#[derive(Deref, DerefMut, Debug, Clone)]
13pub struct Binded<T> {
14    #[deref]
15    #[deref_mut]
16    tracer: T,
17    description: Description,
18}
19
20impl<T> Binded<T> {
21    pub fn new<F>(tracer: T) -> Self
22    where
23        F: Flow,
24        T: Deref<Target = Tracer<F>>,
25    {
26        let description = tracer.description().clone();
27        let this = Self {
28            tracer,
29            description,
30        };
31        this.register();
32        this
33    }
34
35    fn register(&self) {
36        let path = self.description.path.clone();
37        DESCRIPTIONS.add_record(path, self.description.clone());
38    }
39
40    fn unregister(&self) {
41        let path = self.description.path.clone();
42        DESCRIPTIONS.remove_record(path);
43    }
44}
45
46impl<T> Drop for Binded<T> {
47    fn drop(&mut self) {
48        self.unregister();
49    }
50}