wplot/abs/
identity.rs

1/// Internal namespace.
2pub( crate ) mod private
3{
4  use crate::protected::*;
5  use once_cell::sync::Lazy;
6  use std::sync::Mutex;
7  use core::hash::Hash;
8  // use core::any::TypeId;
9
10  static mut COUNTER : Lazy< Mutex< i64 > > = Lazy::new( ||
11  {
12    Mutex::new( 0 )
13  });
14
15  /// ID interface.
16  pub trait IdInterface
17  where
18    Self :
19      fmt::Debug +
20      Clone +
21      Copy +
22      PartialEq +
23      Eq +
24      Hash +
25    ,
26  {
27  }
28
29  /// Has id.
30  pub trait HasIdInterface
31  where
32    Self :
33      fmt::Debug +
34  {
35    /// Get id.
36    fn id( &self ) -> Id;
37  }
38
39  /// Reference on context.
40  #[ derive( Clone, Copy, PartialEq, Eq, Hash ) ]
41  pub struct Id
42  {
43    // #[ allow( dead_code ) ]
44    // tp_id : core::any::TypeId,
45    #[ allow( dead_code ) ]
46    in_id : i64,
47  }
48
49  impl Id
50  {
51    /// Construct a new id increasing counter.
52    pub fn new< T >() -> Self
53    where
54      T : core::any::Any,
55    {
56      // SAFETY : mutex guard it
57      let mut c = unsafe { COUNTER.lock().unwrap() };
58      *c += 1;
59      Self
60      {
61        in_id : *c,
62      }
63    }
64  }
65
66  impl IdInterface for Id
67  {
68  }
69
70  impl fmt::Debug for Id
71  {
72    fn fmt( &self, f : &mut fmt::Formatter<'_> ) -> fmt::Result
73    {
74      f.write_fmt( format_args!( "id::{:?}", self.in_id ) )
75    }
76  }
77
78}
79
80crate::mod_interface!
81{
82
83  exposed use Id;
84  prelude use { IdInterface, HasIdInterface };
85
86}