callback/
lib.rs

1#![no_std]
2#![allow(clippy::type_complexity)]
3#![allow(clippy::too_many_arguments)]
4extern crate alloc;
5use alloc::boxed::Box;
6use alloc::sync::Arc;
7use alloc::vec::Vec;
8use core::{
9    future::Future,
10    pin::Pin,
11    task::{Context, Poll, Waker},
12};
13use spin::{Mutex, MutexGuard};
14
15pub enum CallbackHandler {
16    Callback0(Box<dyn FnMut() + Send + 'static>),
17    Callback1(Box<dyn FnMut(f64) + Send + 'static>),
18    Callback2(Box<dyn FnMut(f64, f64) + Send + 'static>),
19    Callback3(Box<dyn FnMut(f64, f64, f64) + Send + 'static>),
20    Callback4(Box<dyn FnMut(f64, f64, f64, f64) + Send + 'static>),
21    Callback5(Box<dyn FnMut(f64, f64, f64, f64, f64) + Send + 'static>),
22    Callback6(Box<dyn FnMut(f64, f64, f64, f64, f64, f64) + Send + 'static>),
23    Callback7(Box<dyn FnMut(f64, f64, f64, f64, f64, f64, f64) + Send + 'static>),
24    Callback8(Box<dyn FnMut(f64, f64, f64, f64, f64, f64, f64, f64) + Send + 'static>),
25    Callback9(Box<dyn FnMut(f64, f64, f64, f64, f64, f64, f64, f64, f64) + Send + 'static>),
26    Callback10(Box<dyn FnMut(f64, f64, f64, f64, f64, f64, f64, f64, f64, f64) + Send + 'static>),
27}
28
29type CallbackHandle = f64;
30
31pub struct CallbackManager {
32    cur_id: CallbackHandle,
33    pub keys: Vec<CallbackHandle>,
34    pub handlers: Vec<Arc<Mutex<CallbackHandler>>>,
35}
36
37fn get_callbacks() -> MutexGuard<'static, CallbackManager> {
38    static SINGLETON: Mutex<CallbackManager> = {
39        Mutex::new(CallbackManager {
40            cur_id: 0.0,
41            keys: Vec::new(),
42            handlers: Vec::new(),
43        })
44    };
45    SINGLETON.lock()
46}
47
48pub fn get_callback(id: CallbackHandle) -> Option<Arc<Mutex<CallbackHandler>>> {
49    let cbs = get_callbacks();
50    let index = cbs.keys.iter().position(|&r| r == id);
51    if let Some(i) = index {
52        let handler_ref = cbs.handlers.get(i).unwrap().clone();
53        core::mem::drop(cbs);
54        Some(handler_ref)
55    } else {
56        None
57    }
58}
59
60pub fn remove_callback(id: CallbackHandle) {
61    let mut cbs = get_callbacks();
62    let index = cbs.keys.iter().position(|&r| r == id);
63    if let Some(i) = index {
64        cbs.keys.remove(i);
65        cbs.handlers.remove(i);
66    }
67}
68
69fn create_callback(cb: CallbackHandler) -> f64 {
70    let mut h = get_callbacks();
71    h.cur_id += 1.0;
72    let id = h.cur_id;
73    h.keys.push(id);
74    h.handlers.push(Arc::new(Mutex::new(cb)));
75    id
76}
77
78pub fn create_callback_0(cb: impl FnMut() + Send + 'static) -> f64 {
79    create_callback(CallbackHandler::Callback0(Box::new(cb)))
80}
81
82pub fn create_callback_1(cb: impl FnMut(f64) + Send + 'static) -> f64 {
83    create_callback(CallbackHandler::Callback1(Box::new(cb)))
84}
85
86pub fn create_callback_2(cb: impl FnMut(f64, f64) + Send + 'static) -> f64 {
87    create_callback(CallbackHandler::Callback2(Box::new(cb)))
88}
89
90pub fn create_callback_3(cb: impl FnMut(f64, f64, f64) + Send + 'static) -> f64 {
91    create_callback(CallbackHandler::Callback3(Box::new(cb)))
92}
93
94pub fn create_callback_4(cb: impl FnMut(f64, f64, f64, f64) + Send + 'static) -> f64 {
95    create_callback(CallbackHandler::Callback4(Box::new(cb)))
96}
97
98pub fn create_callback_5(cb: impl FnMut(f64, f64, f64, f64, f64) + Send + 'static) -> f64 {
99    create_callback(CallbackHandler::Callback5(Box::new(cb)))
100}
101
102pub fn create_callback_6(cb: impl FnMut(f64, f64, f64, f64, f64, f64) + Send + 'static) -> f64 {
103    create_callback(CallbackHandler::Callback6(Box::new(cb)))
104}
105
106pub fn create_callback_7(
107    cb: impl FnMut(f64, f64, f64, f64, f64, f64, f64) + Send + 'static,
108) -> f64 {
109    create_callback(CallbackHandler::Callback7(Box::new(cb)))
110}
111
112pub fn create_callback_8(
113    cb: impl FnMut(f64, f64, f64, f64, f64, f64, f64, f64) + Send + 'static,
114) -> f64 {
115    create_callback(CallbackHandler::Callback8(Box::new(cb)))
116}
117
118pub fn create_callback_9(
119    cb: impl FnMut(f64, f64, f64, f64, f64, f64, f64, f64, f64) + Send + 'static,
120) -> f64 {
121    create_callback(CallbackHandler::Callback9(Box::new(cb)))
122}
123
124pub fn create_callback_10(
125    cb: impl FnMut(f64, f64, f64, f64, f64, f64, f64, f64, f64, f64) + Send + 'static,
126) -> f64 {
127    create_callback(CallbackHandler::Callback10(Box::new(cb)))
128}
129
130pub struct CallbackFuture {
131    shared_state: Arc<Mutex<SharedState>>,
132}
133
134/// Shared state between the future and the waiting thread
135struct SharedState {
136    completed: bool,
137    waker: Option<Waker>,
138    result: Option<f64>,
139}
140
141impl Future for CallbackFuture {
142    type Output = Option<f64>;
143    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
144        let mut shared_state = self.shared_state.lock();
145        if shared_state.completed {
146            Poll::Ready(shared_state.result)
147        } else {
148            shared_state.waker = Some(cx.waker().clone());
149            Poll::Pending
150        }
151    }
152}
153
154impl CallbackFuture {
155    pub fn new() -> (Self, f64) {
156        let shared_state = Arc::new(Mutex::new(SharedState {
157            completed: false,
158            waker: None,
159            result: None,
160        }));
161
162        let thread_shared_state = shared_state.clone();
163        let id = create_callback(CallbackHandler::Callback1(Box::new(move |v: f64| {
164            let mut shared_state = thread_shared_state.lock();
165            shared_state.completed = true;
166            shared_state.result = Some(v);
167            if let Some(waker) = shared_state.waker.take() {
168                core::mem::drop(shared_state);
169                waker.wake()
170            }
171        })));
172        (CallbackFuture { shared_state }, id as f64)
173    }
174}
175
176struct CallbackFuture0 {
177    shared_state: Arc<Mutex<SharedState0>>,
178}
179
180/// Shared state between the future and the waiting thread
181struct SharedState0 {
182    completed: bool,
183    waker: Option<Waker>,
184    result: (),
185}
186
187impl Future for CallbackFuture0 {
188    type Output = ();
189    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
190        let mut shared_state = self.shared_state.lock();
191        if shared_state.completed {
192            Poll::Ready(())
193        } else {
194            shared_state.waker = Some(cx.waker().clone());
195            Poll::Pending
196        }
197    }
198}
199
200impl CallbackFuture0 {
201    fn new() -> (Self, f64) {
202        let shared_state = Arc::new(Mutex::new(SharedState0 {
203            completed: false,
204            waker: None,
205            result: (),
206        }));
207
208        let thread_shared_state = shared_state.clone();
209        let id = create_callback(CallbackHandler::Callback0(Box::new(move || {
210            let mut shared_state = thread_shared_state.lock();
211            shared_state.completed = true;
212            shared_state.result = ();
213            if let Some(waker) = shared_state.waker.take() {
214                core::mem::drop(shared_state);
215                waker.wake()
216            }
217        })));
218        (CallbackFuture0 { shared_state }, id)
219    }
220}
221
222pub fn create_callback_future_0() -> (impl Future, f64) {
223    CallbackFuture0::new()
224}
225
226struct CallbackFuture1 {
227    shared_state: Arc<Mutex<SharedState1>>,
228}
229
230/// Shared state between the future and the waiting thread
231struct SharedState1 {
232    completed: bool,
233    waker: Option<Waker>,
234    result: f64,
235}
236
237impl Future for CallbackFuture1 {
238    type Output = f64;
239    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
240        let mut shared_state = self.shared_state.lock();
241        if shared_state.completed {
242            Poll::Ready(shared_state.result)
243        } else {
244            shared_state.waker = Some(cx.waker().clone());
245            Poll::Pending
246        }
247    }
248}
249
250impl CallbackFuture1 {
251    fn new() -> (Self, f64) {
252        let shared_state = Arc::new(Mutex::new(SharedState1 {
253            completed: false,
254            waker: None,
255            result: 0.0,
256        }));
257
258        let thread_shared_state = shared_state.clone();
259        let id = create_callback(CallbackHandler::Callback1(Box::new(move |v: f64| {
260            let mut shared_state = thread_shared_state.lock();
261            shared_state.completed = true;
262            shared_state.result = v;
263            if let Some(waker) = shared_state.waker.take() {
264                core::mem::drop(shared_state);
265                waker.wake()
266            }
267        })));
268        (CallbackFuture1 { shared_state }, id)
269    }
270}
271
272pub fn create_callback_future_1() -> (impl Future, f64) {
273    CallbackFuture1::new()
274}
275
276struct CallbackFuture2 {
277    shared_state: Arc<Mutex<SharedState2>>,
278}
279
280/// Shared state between the future and the waiting thread
281struct SharedState2 {
282    completed: bool,
283    waker: Option<Waker>,
284    result: (f64, f64),
285}
286
287impl Future for CallbackFuture2 {
288    type Output = (f64, f64);
289    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
290        let mut shared_state = self.shared_state.lock();
291        if shared_state.completed {
292            Poll::Ready(shared_state.result)
293        } else {
294            shared_state.waker = Some(cx.waker().clone());
295            Poll::Pending
296        }
297    }
298}
299
300impl CallbackFuture2 {
301    fn new() -> (Self, f64) {
302        let shared_state = Arc::new(Mutex::new(SharedState2 {
303            completed: false,
304            waker: None,
305            result: (0.0, 0.0),
306        }));
307
308        let thread_shared_state = shared_state.clone();
309        let id = create_callback(CallbackHandler::Callback2(Box::new(
310            move |a1: f64, a2: f64| {
311                let mut shared_state = thread_shared_state.lock();
312                shared_state.completed = true;
313                shared_state.result = (a1, a2);
314                if let Some(waker) = shared_state.waker.take() {
315                    core::mem::drop(shared_state);
316                    waker.wake()
317                }
318            },
319        )));
320        (CallbackFuture2 { shared_state }, id)
321    }
322}
323
324pub fn create_callback_future_2() -> (impl Future, f64) {
325    CallbackFuture2::new()
326}
327
328struct CallbackFuture3 {
329    shared_state: Arc<Mutex<SharedState3>>,
330}
331
332/// Shared state between the future and the waiting thread
333struct SharedState3 {
334    completed: bool,
335    waker: Option<Waker>,
336    result: (f64, f64, f64),
337}
338
339impl Future for CallbackFuture3 {
340    type Output = (f64, f64, f64);
341    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
342        let mut shared_state = self.shared_state.lock();
343        if shared_state.completed {
344            Poll::Ready(shared_state.result)
345        } else {
346            shared_state.waker = Some(cx.waker().clone());
347            Poll::Pending
348        }
349    }
350}
351
352impl CallbackFuture3 {
353    fn new() -> (Self, f64) {
354        let shared_state = Arc::new(Mutex::new(SharedState3 {
355            completed: false,
356            waker: None,
357            result: (0.0, 0.0, 0.0),
358        }));
359
360        let thread_shared_state = shared_state.clone();
361        let id = create_callback(CallbackHandler::Callback3(Box::new(
362            move |a1: f64, a2: f64, a3: f64| {
363                let mut shared_state = thread_shared_state.lock();
364                shared_state.completed = true;
365                shared_state.result = (a1, a2, a3);
366                if let Some(waker) = shared_state.waker.take() {
367                    core::mem::drop(shared_state);
368                    waker.wake()
369                }
370            },
371        )));
372        (CallbackFuture3 { shared_state }, id)
373    }
374}
375
376pub fn create_callback_future_3() -> (impl Future, f64) {
377    CallbackFuture3::new()
378}
379
380struct CallbackFuture4 {
381    shared_state: Arc<Mutex<SharedState4>>,
382}
383
384/// Shared state between the future and the waiting thread
385struct SharedState4 {
386    completed: bool,
387    waker: Option<Waker>,
388    result: (f64, f64, f64, f64),
389}
390
391impl Future for CallbackFuture4 {
392    type Output = (f64, f64, f64, f64);
393    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
394        let mut shared_state = self.shared_state.lock();
395        if shared_state.completed {
396            Poll::Ready(shared_state.result)
397        } else {
398            shared_state.waker = Some(cx.waker().clone());
399            Poll::Pending
400        }
401    }
402}
403
404impl CallbackFuture4 {
405    fn new() -> (Self, f64) {
406        let shared_state = Arc::new(Mutex::new(SharedState4 {
407            completed: false,
408            waker: None,
409            result: (0.0, 0.0, 0.0, 0.0),
410        }));
411
412        let thread_shared_state = shared_state.clone();
413        let id = create_callback(CallbackHandler::Callback4(Box::new(
414            move |a1: f64, a2: f64, a3: f64, a4: f64| {
415                let mut shared_state = thread_shared_state.lock();
416                shared_state.completed = true;
417                shared_state.result = (a1, a2, a3, a4);
418                if let Some(waker) = shared_state.waker.take() {
419                    core::mem::drop(shared_state);
420                    waker.wake()
421                }
422            },
423        )));
424        (CallbackFuture4 { shared_state }, id)
425    }
426}
427
428pub fn create_callback_future_4() -> (impl Future, f64) {
429    CallbackFuture4::new()
430}
431
432struct CallbackFuture5 {
433    shared_state: Arc<Mutex<SharedState5>>,
434}
435
436/// Shared state between the future and the waiting thread
437struct SharedState5 {
438    completed: bool,
439    waker: Option<Waker>,
440    result: (f64, f64, f64, f64, f64),
441}
442
443impl Future for CallbackFuture5 {
444    type Output = (f64, f64, f64, f64, f64);
445    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
446        let mut shared_state = self.shared_state.lock();
447        if shared_state.completed {
448            Poll::Ready(shared_state.result)
449        } else {
450            shared_state.waker = Some(cx.waker().clone());
451            Poll::Pending
452        }
453    }
454}
455
456impl CallbackFuture5 {
457    fn new() -> (Self, f64) {
458        let shared_state = Arc::new(Mutex::new(SharedState5 {
459            completed: false,
460            waker: None,
461            result: (0.0, 0.0, 0.0, 0.0, 0.0),
462        }));
463
464        let thread_shared_state = shared_state.clone();
465        let id = create_callback(CallbackHandler::Callback5(Box::new(
466            move |a1: f64, a2: f64, a3: f64, a4: f64, a5: f64| {
467                let mut shared_state = thread_shared_state.lock();
468                shared_state.completed = true;
469                shared_state.result = (a1, a2, a3, a4, a5);
470                if let Some(waker) = shared_state.waker.take() {
471                    core::mem::drop(shared_state);
472                    waker.wake()
473                }
474            },
475        )));
476        (CallbackFuture5 { shared_state }, id)
477    }
478}
479
480pub fn create_callback_future_5() -> (impl Future, f64) {
481    CallbackFuture5::new()
482}
483
484struct CallbackFuture6 {
485    shared_state: Arc<Mutex<SharedState6>>,
486}
487
488/// Shared state between the future and the waiting thread
489struct SharedState6 {
490    completed: bool,
491    waker: Option<Waker>,
492    result: (f64, f64, f64, f64, f64, f64),
493}
494
495impl Future for CallbackFuture6 {
496    type Output = (f64, f64, f64, f64, f64, f64);
497    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
498        let mut shared_state = self.shared_state.lock();
499        if shared_state.completed {
500            Poll::Ready(shared_state.result)
501        } else {
502            shared_state.waker = Some(cx.waker().clone());
503            Poll::Pending
504        }
505    }
506}
507
508impl CallbackFuture6 {
509    fn new() -> (Self, f64) {
510        let shared_state = Arc::new(Mutex::new(SharedState6 {
511            completed: false,
512            waker: None,
513            result: (0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
514        }));
515
516        let thread_shared_state = shared_state.clone();
517        let id = create_callback(CallbackHandler::Callback6(Box::new(
518            move |a1: f64, a2: f64, a3: f64, a4: f64, a5: f64, a6: f64| {
519                let mut shared_state = thread_shared_state.lock();
520                shared_state.completed = true;
521                shared_state.result = (a1, a2, a3, a4, a5, a6);
522                if let Some(waker) = shared_state.waker.take() {
523                    core::mem::drop(shared_state);
524                    waker.wake()
525                }
526            },
527        )));
528        (CallbackFuture6 { shared_state }, id)
529    }
530}
531
532pub fn create_callback_future_6() -> (impl Future, f64) {
533    CallbackFuture6::new()
534}
535
536struct CallbackFuture7 {
537    shared_state: Arc<Mutex<SharedState7>>,
538}
539
540/// Shared state between the future and the waiting thread
541struct SharedState7 {
542    completed: bool,
543    waker: Option<Waker>,
544    result: (f64, f64, f64, f64, f64, f64, f64),
545}
546
547impl Future for CallbackFuture7 {
548    type Output = (f64, f64, f64, f64, f64, f64, f64);
549    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
550        let mut shared_state = self.shared_state.lock();
551        if shared_state.completed {
552            Poll::Ready(shared_state.result)
553        } else {
554            shared_state.waker = Some(cx.waker().clone());
555            Poll::Pending
556        }
557    }
558}
559
560impl CallbackFuture7 {
561    fn new() -> (Self, f64) {
562        let shared_state = Arc::new(Mutex::new(SharedState7 {
563            completed: false,
564            waker: None,
565            result: (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
566        }));
567
568        let thread_shared_state = shared_state.clone();
569        let id = create_callback(CallbackHandler::Callback7(Box::new(
570            move |a1: f64, a2: f64, a3: f64, a4: f64, a5: f64, a6: f64, a7: f64| {
571                let mut shared_state = thread_shared_state.lock();
572                shared_state.completed = true;
573                shared_state.result = (a1, a2, a3, a4, a5, a6, a7);
574                if let Some(waker) = shared_state.waker.take() {
575                    core::mem::drop(shared_state);
576                    waker.wake()
577                }
578            },
579        )));
580        (CallbackFuture7 { shared_state }, id)
581    }
582}
583
584pub fn create_callback_future_7() -> (impl Future, f64) {
585    CallbackFuture7::new()
586}
587
588struct CallbackFuture8 {
589    shared_state: Arc<Mutex<SharedState8>>,
590}
591
592/// Shared state between the future and the waiting thread
593struct SharedState8 {
594    completed: bool,
595    waker: Option<Waker>,
596    result: (f64, f64, f64, f64, f64, f64, f64, f64),
597}
598
599impl Future for CallbackFuture8 {
600    type Output = (f64, f64, f64, f64, f64, f64, f64, f64);
601    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
602        let mut shared_state = self.shared_state.lock();
603        if shared_state.completed {
604            Poll::Ready(shared_state.result)
605        } else {
606            shared_state.waker = Some(cx.waker().clone());
607            Poll::Pending
608        }
609    }
610}
611
612impl CallbackFuture8 {
613    fn new() -> (Self, f64) {
614        let shared_state = Arc::new(Mutex::new(SharedState8 {
615            completed: false,
616            waker: None,
617            result: (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
618        }));
619
620        let thread_shared_state = shared_state.clone();
621        let id = create_callback(CallbackHandler::Callback8(Box::new(
622            move |a1: f64, a2: f64, a3: f64, a4: f64, a5: f64, a6: f64, a7: f64, a8: f64| {
623                let mut shared_state = thread_shared_state.lock();
624                shared_state.completed = true;
625                shared_state.result = (a1, a2, a3, a4, a5, a6, a7, a8);
626                if let Some(waker) = shared_state.waker.take() {
627                    core::mem::drop(shared_state);
628                    waker.wake()
629                }
630            },
631        )));
632        (CallbackFuture8 { shared_state }, id)
633    }
634}
635
636pub fn create_callback_future_8() -> (impl Future, f64) {
637    CallbackFuture8::new()
638}
639
640struct CallbackFuture9 {
641    shared_state: Arc<Mutex<SharedState9>>,
642}
643
644/// Shared state between the future and the waiting thread
645struct SharedState9 {
646    completed: bool,
647    waker: Option<Waker>,
648    result: (f64, f64, f64, f64, f64, f64, f64, f64, f64),
649}
650
651impl Future for CallbackFuture9 {
652    type Output = (f64, f64, f64, f64, f64, f64, f64, f64, f64);
653    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
654        let mut shared_state = self.shared_state.lock();
655        if shared_state.completed {
656            Poll::Ready(shared_state.result)
657        } else {
658            shared_state.waker = Some(cx.waker().clone());
659            Poll::Pending
660        }
661    }
662}
663
664impl CallbackFuture9 {
665    fn new() -> (Self, f64) {
666        let shared_state = Arc::new(Mutex::new(SharedState9 {
667            completed: false,
668            waker: None,
669            result: (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
670        }));
671
672        let thread_shared_state = shared_state.clone();
673        let id = create_callback(CallbackHandler::Callback9(Box::new(
674            move |a1: f64,
675                  a2: f64,
676                  a3: f64,
677                  a4: f64,
678                  a5: f64,
679                  a6: f64,
680                  a7: f64,
681                  a8: f64,
682                  a9: f64| {
683                let mut shared_state = thread_shared_state.lock();
684                shared_state.completed = true;
685                shared_state.result = (a1, a2, a3, a4, a5, a6, a7, a8, a9);
686                if let Some(waker) = shared_state.waker.take() {
687                    core::mem::drop(shared_state);
688                    waker.wake()
689                }
690            },
691        )));
692        (CallbackFuture9 { shared_state }, id)
693    }
694}
695
696pub fn create_callback_future_9() -> (impl Future, f64) {
697    CallbackFuture9::new()
698}
699
700struct CallbackFuture10 {
701    shared_state: Arc<Mutex<SharedState10>>,
702}
703
704/// Shared state between the future and the waiting thread
705struct SharedState10 {
706    completed: bool,
707    waker: Option<Waker>,
708    result: (f64, f64, f64, f64, f64, f64, f64, f64, f64, f64),
709}
710
711impl Future for CallbackFuture10 {
712    type Output = (f64, f64, f64, f64, f64, f64, f64, f64, f64, f64);
713    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
714        let mut shared_state = self.shared_state.lock();
715        if shared_state.completed {
716            Poll::Ready(shared_state.result)
717        } else {
718            shared_state.waker = Some(cx.waker().clone());
719            Poll::Pending
720        }
721    }
722}
723
724impl CallbackFuture10 {
725    fn new() -> (Self, f64) {
726        let shared_state = Arc::new(Mutex::new(SharedState10 {
727            completed: false,
728            waker: None,
729            result: (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0),
730        }));
731
732        let thread_shared_state = shared_state.clone();
733        let id = create_callback(CallbackHandler::Callback10(Box::new(
734            move |a1: f64,
735                  a2: f64,
736                  a3: f64,
737                  a4: f64,
738                  a5: f64,
739                  a6: f64,
740                  a7: f64,
741                  a8: f64,
742                  a9: f64,
743                  a10: f64| {
744                let mut shared_state = thread_shared_state.lock();
745                shared_state.completed = true;
746                shared_state.result = (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
747                if let Some(waker) = shared_state.waker.take() {
748                    core::mem::drop(shared_state);
749                    waker.wake()
750                }
751            },
752        )));
753        (CallbackFuture10 { shared_state }, id)
754    }
755}
756
757pub fn create_callback_future_10() -> (impl Future, f64) {
758    CallbackFuture10::new()
759}
760
761#[no_mangle]
762fn handle_callback(
763    id: f64,
764    a1: f64,
765    a2: f64,
766    a3: f64,
767    a4: f64,
768    a5: f64,
769    a6: f64,
770    a7: f64,
771    a8: f64,
772    a9: f64,
773    a10: f64,
774) {
775    let h = get_callback(id);
776    let handler_ref = h.unwrap();
777    let mut handler = handler_ref.lock();
778    match &mut *handler {
779        CallbackHandler::Callback0(c) => c(),
780        CallbackHandler::Callback1(c) => c(a1),
781        CallbackHandler::Callback2(c) => c(a1, a2),
782        CallbackHandler::Callback3(c) => c(a1, a2, a3),
783        CallbackHandler::Callback4(c) => c(a1, a2, a3, a4),
784        CallbackHandler::Callback5(c) => c(a1, a2, a3, a4, a5),
785        CallbackHandler::Callback6(c) => c(a1, a2, a3, a4, a5, a6),
786        CallbackHandler::Callback7(c) => c(a1, a2, a3, a4, a5, a6, a7),
787        CallbackHandler::Callback8(c) => c(a1, a2, a3, a4, a5, a6, a7, a8),
788        CallbackHandler::Callback9(c) => c(a1, a2, a3, a4, a5, a6, a7, a8, a9),
789        CallbackHandler::Callback10(c) => c(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10),
790    }
791}