1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::state_access::{CloneState, StateAccess};
use crate::hooks_state_functions::use_state;

/// call the provided function once and once only
/// returns a unmmunt which will allow the do_once
/// to repeat if .execute_if_activated() is called on the unmount.
/// Example
///
/// do_once(||{
///     println!("This will print only once");
/// });
#[topo::nested]
pub fn do_once<F: FnMut() -> ()>(mut func: F) -> StateAccess<bool> {
    let has_done = use_state(|| false);
    if !has_done.get() {
        func();
        has_done.set(true);
    }
    has_done
}



#[derive(Clone,Copy,Debug,PartialEq,Hash,Eq)]
pub struct Local( topo::Id );

impl std::fmt::Display for Local {

    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:#?}", self.0)
    }
}

impl Local{
    #[topo::nested]
    pub fn new() -> Local{
        Local( topo::Id::current())
    }
}

/// A value unique to the source location where it is created.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct CallSite {
    location: usize,
}

impl CallSite {
    /// Constructs a callsite whose value is unique to the source location at
    /// which it is called.
    #[track_caller]
    pub fn here() -> Self {
        Self {
            // the pointer value for a given location is enough to differentiate it from all others
            location: std::panic::Location::caller() as *const _ as usize,
        }
    }
}