MCPcopy Create free account
hub / github.com/MegEngine/MegFlow / feature_expand

Function feature_expand

flow-derive/src/internal.rs:68–131  ·  view source on GitHub ↗
(mut input: FeatureDeclare)

Source from the content-addressed store, hash-verified

66}
67
68pub fn feature_expand(mut input: FeatureDeclare) -> TokenStream {
69 let name = &input.name;
70 let args_type = lit::ident(format!("__args{}", name));
71 let args_name = lit::ident(format!("{}_args", name));
72 let init = lit::ident(format!("__init{}", name));
73 let fields = &mut input.fields;
74 fields.iter_mut().for_each(|field| {
75 field.vis = Visibility::Public(VisPublic {
76 pub_token: Token![pub](field.span()),
77 });
78 });
79 quote! {
80 #[derive(serde::Deserialize, Default)]
81 pub struct #args_type {
82 #fields
83 }
84 pub static #name: feature::Feature = feature::Feature::new();
85 lazy_static::lazy_static! {
86 pub static ref #args_name: std::sync::RwLock<std::collections::HashMap<usize, #args_type>>
87 = std::sync::RwLock::new(std::collections::HashMap::new());
88 }
89 #[flow_rs::ctor]
90 fn #init() {
91 flow_rs::registry::__submit_only_in_ctor(stringify!(#name), feature::FeatureCommand {
92 start: Box::new(|k, json| {
93 #name.notify();
94 let mut args = #args_name.write().unwrap();
95 args.insert(k, serde_json::from_value(json).unwrap());
96 }),
97 stop: Box::new(|k| {
98 let mut args = #args_name.write().unwrap();
99 if let Some(_) = args.remove(&k) {
100 let into_object = |json| match json {
101 serde_json::Value::Object(json) => json,
102 _ => unreachable!(),
103 };
104
105 let args = into_object(serde_json::json!({
106 "seq_id": k,
107 }));
108
109 let others = into_object(
110 serde_json::to_value(protocol::EventMessage {
111 event: protocol::EVENT_STOP.to_owned(),
112 args,
113 })
114 .unwrap(),
115 );
116 server::PORT.0.try_send(protocol::ProtocolMessage {
117 ty: protocol::TYPE_EVENT.to_owned(),
118 others,
119 }).ok();
120 }
121 if args.is_empty() {
122 #name.disable();
123 }
124 }),
125 disable: Box::new(|| {

Callers 1

featureFunction · 0.85

Calls 2

identFunction · 0.85
for_eachMethod · 0.80

Tested by

no test coverage detected