remote_trait_object/service/id.rs
1use super::MethodId;
2use linkme::distributed_slice;
3use serde::{Deserialize, Serialize};
4use std::collections::{HashMap, HashSet};
5
6pub const ID_ORDERING: std::sync::atomic::Ordering = std::sync::atomic::Ordering::SeqCst;
7pub type MethodIdAtomic = std::sync::atomic::AtomicU32;
8
9// linkme crate smartly collects all the registrations generated by the proc-macro
10// into a sinlge array in the link time.
11// Note that too long linkme-related variable name would cause serious compiler error in MacOS
12// So we deliberately make it have a short name
13
14// Id of methods in services.
15// Note that here the two strings mean (trait name, method name)
16// Also you can skip calling this, then the method id will be set up for default value
17// decided by the order of declaration.
18type MethodIdentifierSetter = fn(id: MethodId);
19#[distributed_slice]
20pub static MID_REG: [(&'static str, &'static str, MethodIdentifierSetter)] = [..];
21
22/// This will be provided by the user who cares the compatability between already-compiled service traits.
23#[derive(PartialEq, Serialize, Deserialize, Debug, Clone)]
24pub struct IdMap {
25 // This is system-wide; All module will get same ones
26 pub method_map: Option<HashMap<(String, String), MethodId>>,
27}
28
29/// A special function that sets static & global identifiers for the methods.
30///
31/// It will be explained in more detail in the next version :)
32///
33/// This is supposed to be called only once during the entire lifetime of the process.
34/// However it is ok to call multiple times if the IdMap is identical, especially in the
35/// tests where each test share that static id list
36/// # Examples
37/// ```
38/// use remote_trait_object::macro_env::*;
39/// #[allow(non_upper_case_globals)]
40/// static ID_METHOD_MyTrait_mymethod: MethodIdAtomic = MethodIdAtomic::new(1);
41/// #[linkme::distributed_slice(MID_REG)]
42/// #[allow(non_upper_case_globals)]
43/// static ID_METHOD_ENTRY_MyTrait_mymethod: (&'static str, &'static str, fn(id: MethodId)) =
44/// ("MyTrait", "mymethod", id_method_setter_MyTrait_mymethod);
45/// #[allow(non_snake_case)]
46/// fn id_method_setter_MyTrait_mymethod(id: MethodId) {
47/// ID_METHOD_MyTrait_mymethod.store(id, ID_ORDERING);
48/// }
49/// #[test]
50/// fn setup() {
51/// let id_map: HashMap<(String, String), MethodId> =
52/// [(("MyTrait".to_owned(), "mymethod".to_owned()), 123)].iter().cloned().collect();
53/// let id_map = IdMap {
54/// method_map: Some(id_map),
55/// };
56/// setup_identifiers(&id_map);
57/// assert_eq!(ID_METHOD_MyTrait_mymethod.load(ID_ORDERING), 123);
58/// }
59/// ```
60pub fn setup_identifiers(descriptor: &IdMap) {
61 // distributed_slices integrity test
62 {
63 let mut bucket: HashSet<(String, String)> = HashSet::new();
64 for (ident1, ident2, _) in MID_REG {
65 bucket.insert(((*ident1).to_owned(), (*ident2).to_owned()));
66 }
67 assert_eq!(
68 bucket.len(),
69 MID_REG.len(),
70 "The service traits that this binary involved are not named;
71 You have provided multiple traits with an identical name"
72 );
73 }
74
75 // method ids have default values decided by the order, so it is ok to leave them in an ordinary case.
76 if let Some(map) = descriptor.method_map.as_ref() {
77 for (trait_name, method_name, setter) in MID_REG {
78 setter(
79 *map.get(&((*trait_name).to_owned(), (*method_name).to_owned()))
80 .expect("Invalid handle descriptor"),
81 );
82 }
83 }
84}