wrapped_mono 0.4.0

`wrapped_mono` is a safe, lightweight wrapper around the mono library. It allows embedding of the mono runtime inside a rust project. Inside this embedded runtime code written in languages supporting the .NET framework, such as C# and F#, can be run. This allows usage of libraries written in those languages, and using them as a scripting language. The mono runtime is used by many game engines, and this wrapper allows using it with projects written in Rust too.
Documentation
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
use crate::binds::{MonoProfiler, MonoProfilerCallContext, MonoProfilerHandle, _MonoProfiler};
use crate::interop::InteropRecive;
use crate::{Domain, Method, Object};
// Profiler is not finished so it has some dead code that will fixed addressed in the future TODO: fix profiler dead code
struct _Profiler<T> {
    handle: MonoProfilerHandle,
    rtime_init_cb: Option<fn(profiler: &mut Profiler<T>)>,
    rtime_shutdown_begin_cb: Option<fn(profiler: &mut Profiler<T>)>,
    rtime_shutdown_end_cb: Option<fn(profiler: &mut Profiler<T>)>,
    context_loaded_cb: Option<fn(profiler: &mut Profiler<T>)>,
    cleanup_cb: Option<fn(profiler: &mut Profiler<T>)>,
    context_unloaded_cb: Option<fn(profiler: &mut Profiler<T>)>,
    domain_loading_cb: Option<fn(profiler: &mut Profiler<T>, dom: &mut Domain)>,
    domain_loaded_cb: Option<fn(profiler: &mut Profiler<T>, dom: &mut Domain)>,
    domain_unloading_cb: Option<fn(profiler: &mut Profiler<T>, dom: &mut Domain)>,
    domain_unloaded_cb: Option<fn(profiler: &mut Profiler<T>, dom: &mut Domain)>,
    domain_set_name_cb: Option<fn(profiler: &mut Profiler<T>, dom: &mut Domain, &str)>,
    //TODO: fix jit_negin_cb: main method has either type Method<Object,iszie,isize,isize> or Method<String> signature.
    #[allow(clippy::type_complexity)]
    jit_begin_cb: Option<fn(profiler: &mut Profiler<T>, &Method<(String,)>)>,
    pub data: T,
}
pub struct ProfilerCallContext {
    ptr: *mut MonoProfilerCallContext,
}
impl ProfilerCallContext {
    pub fn get_this(&self) -> Option<Object> {
        unsafe {
            Object::from_ptr(crate::binds::mono_profiler_call_context_get_this(self.ptr)
                as *mut crate::binds::MonoObject)
        }
    }
    ///Returns *index* argument of current call context. *index* must be within argument count of current method. Type must match argument.
    pub fn get_arg<T: InteropRecive>(&self, index: u32) -> T {
        let ptr = unsafe { crate::binds::mono_profiler_call_context_get_argument(self.ptr, index) }
            as *const <T as InteropRecive>::SourceType;
        let src: <T as InteropRecive>::SourceType = unsafe { *(ptr) };
        <T as InteropRecive>::get_rust_rep(src)
    }
    ///Return local argument from current call contex at *index*. Index must be valid and type must match.
    pub fn get_local<T: InteropRecive>(&self, index: u32) -> T {
        let ptr = unsafe { crate::binds::mono_profiler_call_context_get_local(self.ptr, index) }
            as *const <T as InteropRecive>::SourceType;
        let src: <T as InteropRecive>::SourceType = unsafe { *(ptr) };
        <T as InteropRecive>::get_rust_rep(src)
    }
}
use std::alloc::{alloc, dealloc, Layout};
impl<T> _Profiler<T> {
    pub fn create(mut data: T) -> *mut Self {
        unsafe {
            let ptr = alloc(Layout::new::<Self>());
            for i in 0..std::mem::size_of::<Self>() {
                *((ptr as usize + i) as *mut u8) = 0;
            }
            let ptr = ptr as *mut Self;
            (*ptr).handle = crate::binds::mono_profiler_create(ptr as *mut MonoProfiler);
            let src: &mut T = &mut data;
            let dst: &mut T = &mut (*ptr).data;
            std::mem::swap(src, dst);
            std::mem::forget(data);
            ptr
        }
    }
    pub fn destroy(profiler: *mut Self) {
        let prof = unsafe { &mut *profiler };
        Self::remove_rtime_init_cb(prof);
        Self::remove_jit_begin_cb(prof);
        Self::remove_domain_loaded_cb(prof);
        Self::remove_domain_loading_cb(prof);
        Self::remove_domain_name_cb(prof);
        unsafe { dealloc(profiler as *mut u8, Layout::new::<Self>()) };
    }
    //#####################################################################
    //Runtime init
    unsafe extern "C" fn runtime_init_callback(profiler: *mut _Profiler<T>) {
        let this = &mut *(profiler);
        match this.rtime_init_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof);
                println!("Finished calling runtime init callback!");
            }
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_rtime_init_cb(&mut self) {
        //Check if another callback has been registered and if so, remove it.
        unsafe {
            crate::binds::mono_profiler_set_runtime_initialized_callback(self.handle, None);
            self.rtime_init_cb = None;
        }
    }
    pub fn add_rtime_init_cb(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_runtime_initialized_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>),
                    Option<unsafe extern "C" fn(*mut _MonoProfiler)>,
                >(
                    Self::runtime_init_callback as unsafe extern "C" fn(*mut _Profiler<T>)
                ),
            );
            self.rtime_init_cb = Some(cb);
        }
    }
    //TODO: Check why cleanup callback colides with runtime_init callback and renable it.
    /*
    //################################################################
    //Cleanup
    unsafe extern "C" fn cleanup_callback(profiler:*mut _Profiler<T>){
        let this = &mut *(profiler);
        println!("Clenup callback preparation!");
        match this.cleanup_cb{
            Some(cb)=>{
                cb(&mut Profiler::<T>::from_ptr(profiler as *mut MonoProfiler));
            }
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
        println!("Clenup callback finished!");
    }
    pub fn remove_cleanup_cb(&mut self){
        //Check if another callback has been registered and if so, renove it.
        unsafe{
            crate::binds::mono_profiler_set_cleanup_callback(self.handle,None);
            self.cleanup_cb = None;
        }
    }
    pub fn add_cleanup_cb(&mut self,cb:fn (profiler:&mut Profiler<T>)){
        unsafe{
            crate::binds::mono_profiler_set_cleanup_callback(self.handle,
                std::mem::transmute::<unsafe extern "C" fn(*mut _Profiler<T>),Option<unsafe extern "C" fn(*mut _MonoProfiler)>>
                (Self::cleanup_callback as unsafe extern "C" fn(*mut _Profiler<T>)));
            self.cleanup_cb = Some(cb);
        }
    }*/
    //##################################################
    //Runtime shutdown begin callback.
    unsafe extern "C" fn runtime_shutown_begin_callback(profiler: *mut _Profiler<T>) {
        let this = &mut *(profiler);
        match this.rtime_shutdown_begin_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_runtime_shutdown_begin_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_runtime_shutdown_begin_callback(self.handle, None);
            self.rtime_shutdown_begin_cb = None;
        }
    }
    pub fn add_runtime_shutdown_begin_cb(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_runtime_shutdown_begin_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>),
                    Option<unsafe extern "C" fn(*mut _MonoProfiler)>,
                >(
                    Self::runtime_shutown_begin_callback as unsafe extern "C" fn(*mut _Profiler<T>)
                ),
            );
            self.rtime_shutdown_begin_cb = Some(cb);
        }
    }
    //##################################################
    //Runtime shutdown end callback.
    unsafe extern "C" fn runtime_shutown_end_callback(profiler: *mut _Profiler<T>) {
        let this = &mut *(profiler);
        match this.rtime_shutdown_end_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_runtime_shutdown_end_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_runtime_shutdown_end_callback(self.handle, None);
            self.rtime_shutdown_end_cb = None;
        }
    }
    pub fn add_runtime_shutdown_end_cb(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_runtime_shutdown_end_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>),
                    Option<unsafe extern "C" fn(*mut _MonoProfiler)>,
                >(
                    Self::runtime_shutown_end_callback as unsafe extern "C" fn(*mut _Profiler<T>)
                ),
            );
            self.rtime_shutdown_end_cb = Some(cb);
        }
    }
    //##################################################
    //Context loaded callback
    unsafe extern "C" fn context_loaded_callback(
        profiler: *mut _Profiler<T>,
        _: *mut crate::binds::MonoAppContext,
    ) {
        let this = &mut *(profiler);
        match this.context_loaded_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_context_loaded_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_context_loaded_callback(self.handle, None);
            self.context_loaded_cb = None;
        }
    }
    pub fn add_context_loaded_cb(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_context_loaded_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoAppContext),
                    Option<
                        unsafe extern "C" fn(*mut _MonoProfiler, *mut crate::binds::MonoAppContext),
                    >,
                >(
                    Self::context_loaded_callback
                        as unsafe extern "C" fn(
                            *mut _Profiler<T>,
                            *mut crate::binds::MonoAppContext,
                        ),
                ),
            );
            self.context_loaded_cb = Some(cb);
        }
    }
    //##################################################
    //Context unloaded callback
    unsafe extern "C" fn context_unloaded_callback(
        profiler: *mut _Profiler<T>,
        _: *mut crate::binds::MonoAppContext,
    ) {
        let this = &mut *(profiler);
        match this.context_unloaded_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_context_unloaded_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_context_unloaded_callback(self.handle, None);
            self.context_unloaded_cb = None;
        }
    }
    pub fn add_context_unloaded_cb(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_context_unloaded_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoAppContext),
                    Option<
                        unsafe extern "C" fn(*mut _MonoProfiler, *mut crate::binds::MonoAppContext),
                    >,
                >(
                    Self::context_unloaded_callback
                        as unsafe extern "C" fn(
                            *mut _Profiler<T>,
                            *mut crate::binds::MonoAppContext,
                        ),
                ),
            );
            self.context_unloaded_cb = Some(cb);
        }
    }
    //##################################################
    //Domain loading callback
    unsafe extern "C" fn domain_loading_callback(
        profiler: *mut _Profiler<T>,
        dom: *mut crate::binds::MonoDomain,
    ) {
        let this = &mut *(profiler);
        let mut dom = Domain::from_ptr(dom);
        match this.domain_loading_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof,&mut dom);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_domain_loading_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_loading_callback(self.handle, None);
            self.domain_loading_cb = None;
        }
    }
    pub fn add_domain_loading_cb(&mut self, cb: fn(profiler: &mut Profiler<T>, dom: &mut Domain)) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_loading_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoDomain),
                    Option<unsafe extern "C" fn(*mut _MonoProfiler, *mut crate::binds::MonoDomain)>,
                >(
                    Self::domain_loading_callback
                        as unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoDomain),
                ),
            );
            self.domain_loading_cb = Some(cb);
        }
    }
    //##################################################
    //Domain loaded callback
    unsafe extern "C" fn domain_loaded_callback(
        profiler: *mut _Profiler<T>,
        dom: *mut crate::binds::MonoDomain,
    ) {
        let this = &mut *(profiler);
        let mut dom = Domain::from_ptr(dom);
        match this.domain_loaded_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof,&mut dom);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_domain_loaded_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_loaded_callback(self.handle, None);
            self.domain_loaded_cb = None;
        }
    }
    pub fn add_domain_loaded_cb(&mut self, cb: fn(profiler: &mut Profiler<T>, dom: &mut Domain)) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_loaded_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoDomain),
                    Option<unsafe extern "C" fn(*mut _MonoProfiler, *mut crate::binds::MonoDomain)>,
                >(
                    Self::domain_loaded_callback
                        as unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoDomain),
                ),
            );
            self.domain_loaded_cb = Some(cb);
        }
    }
    //##################################################
    //Domain unloading callback
    unsafe extern "C" fn domain_unloading_callback(
        profiler: *mut _Profiler<T>,
        dom: *mut crate::binds::MonoDomain,
    ) {
        let this = &mut *(profiler);
        let mut dom = Domain::from_ptr(dom);
        match this.domain_unloading_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof,&mut dom);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_domain_unloading_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_unloading_callback(self.handle, None);
            self.domain_unloading_cb = None;
        }
    }
    pub fn add_domain_unloading_cb(
        &mut self,
        cb: fn(profiler: &mut Profiler<T>, dom: &mut Domain),
    ) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_unloading_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoDomain),
                    Option<unsafe extern "C" fn(*mut _MonoProfiler, *mut crate::binds::MonoDomain)>,
                >(
                    Self::domain_unloading_callback
                        as unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoDomain),
                ),
            );
            self.domain_unloading_cb = Some(cb);
        }
    }
    //##################################################
    //Domain unloaded callback
    unsafe extern "C" fn domain_unloaded_callback(
        profiler: *mut _Profiler<T>,
        dom: *mut crate::binds::MonoDomain,
    ) {
        let this = &mut *(profiler);
        let mut dom = Domain::from_ptr(dom);
        match this.domain_unloaded_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof,&mut dom);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
    }
    pub fn remove_domain_unloaded_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_unloaded_callback(self.handle, None);
            self.domain_unloaded_cb = None;
        }
    }
    pub fn add_domain_unloaded_cb(&mut self, cb: fn(profiler: &mut Profiler<T>, dom: &mut Domain)) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_unloaded_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoDomain),
                    Option<unsafe extern "C" fn(*mut _MonoProfiler, *mut crate::binds::MonoDomain)>,
                >(
                    Self::domain_unloaded_callback
                        as unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoDomain),
                ),
            );
            self.domain_unloaded_cb = Some(cb);
        }
    }
    //##################################################
    //Domain set name callback
    unsafe extern "C" fn domain_name_callback(
        profiler: *mut _Profiler<T>,
        dom: *mut crate::binds::MonoDomain,
        str_ptr: *const i8,
    ) {
        let this = &mut *(profiler);
        let cstr = CString::from_raw(str_ptr as *mut i8);
        use std::ffi::CString;
        let st = cstr.to_str().expect("Could not create String!").to_owned();
        let mut dom = Domain::from_ptr(dom);
        match this.domain_set_name_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof,&mut dom,&st);
            },
            None=>panic!("Invalid callback registration state. Callback registered for handler, yet handler has no callback function to call!"),
        }
        let _ = cstr.into_raw();
    }
    pub fn remove_domain_name_cb(&mut self) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_name_callback(self.handle, None);
            self.domain_set_name_cb = None;
        }
    }
    pub fn add_domain_name_cb(
        &mut self,
        cb: fn(profiler: &mut Profiler<T>, dom: &mut Domain, name: &str),
    ) {
        //Check if another callback has been registered and if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_domain_name_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(
                        *mut _Profiler<T>,
                        *mut crate::binds::MonoDomain,
                        *const i8,
                    ),
                    Option<
                        unsafe extern "C" fn(
                            *mut _MonoProfiler,
                            *mut crate::binds::MonoDomain,
                            *const i8,
                        ),
                    >,
                >(
                    Self::domain_name_callback
                        as unsafe extern "C" fn(
                            *mut _Profiler<T>,
                            *mut crate::binds::MonoDomain,
                            *const i8,
                        ),
                ),
            );
            self.domain_set_name_cb = Some(cb);
        }
    }
    //##################################################
    //Domain set jit begin
    unsafe extern "C" fn jit_begin_callback(
        profiler: *mut _Profiler<T>,
        met: *mut crate::binds::MonoMethod,
    ) {
        let this = &mut *(profiler);
        let method:Method<(String,)> = Method::from_ptr(met).expect("Could not get jit main method while executing jit begin. This is an internal profiler error.");
        match this.jit_begin_cb{
            Some(cb)=>{
                let mut prof = Profiler::<T>::from_ptr(profiler as *mut MonoProfiler);
                cb(&mut prof,&method);
            },
            None=>panic!("Invalid callback registration state. Callback registered for hindler, yet hindler has no callback function to call!"),
        }
    }
    pub fn remove_jit_begin_cb(&mut self) {
        //Check if another callback has been registered ind if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_jit_begin_callback(self.handle, None);
            self.jit_begin_cb = None;
        }
    }
    pub fn add_jit_begin_cb(&mut self, cb: fn(profiler: &mut Profiler<T>, &Method<(String,)>)) {
        //Check if another callback has been registered ind if so, renove it.
        unsafe {
            crate::binds::mono_profiler_set_jit_begin_callback(
                self.handle,
                std::mem::transmute::<
                    unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoMethod),
                    Option<unsafe extern "C" fn(*mut _MonoProfiler, *mut crate::binds::MonoMethod)>,
                >(
                    Self::jit_begin_callback
                        as unsafe extern "C" fn(*mut _Profiler<T>, *mut crate::binds::MonoMethod),
                ),
            );
            self.jit_begin_cb = Some(cb);
        }
    }
}
//impliment mono_profiler_set_coverage_filter_callback
/// A structure representing a profiler with custom user data. This structure will be passed when callbacks are called. No more than one callback per profiler can be registered.
pub struct Profiler<T> {
    ptr: *mut _Profiler<T>,
}
impl<T> Profiler<T> {
    fn from_ptr(profiler: *mut MonoProfiler) -> Self {
        Self {
            ptr: profiler as *mut _Profiler<T>,
        }
    }
    ///Creates a new profiler with *data*. Data is user defined struct used to pass data to callbacks.
    pub fn create(data: T) -> Profiler<T> {
        Self {
            ptr: _Profiler::<T>::create(data),
        }
    }
    /// Destroys the profiler.
    /// # Safety
    /// makes all of copies of Profiler invalid
    pub fn destroy(self) {
        _Profiler::<T>::destroy(self.ptr);
    }
    ///Returns reference to internal data.
    pub fn get_internal_data(&mut self) -> &mut T {
        unsafe { &mut (*self.ptr).data }
    }
    ///  Removes callback added by [`fn@add_runtime_initialized_callback()`]
    pub fn remove_runtime_initialized_callback(&mut self) {
        unsafe { (*self.ptr).remove_rtime_init_cb() };
    }
    /// Add scallback to be called when runtime is started.
    pub fn add_runtime_initialized_callback(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        unsafe { (*self.ptr).add_rtime_init_cb(cb) };
    }
    /*
    /// Removes callback added by [`add_cleanup_callback`]
    pub fn remove_cleanup_callback(&mut self){
        unsafe{(*self.ptr).remove_rtime_init_cb()};
    }
    /// Addscallback to be called when runtime is started.
    pub fn add_cleanup_callback(&mut self,cb: fn (profiler:&mut Profiler<T>)){
       unsafe{(*self.ptr).add_rtime_init_cb(cb)};
    }*/
    /// Removes callback added by [`fn@add_runtime_shutown_begin_callback()`]
    pub fn remove_runtime_shutown_begin_callback(&mut self) {
        unsafe { (*self.ptr).remove_runtime_shutdown_begin_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_runtime_shutown_begin_callback(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        unsafe { (*self.ptr).add_runtime_shutdown_begin_cb(cb) };
    }
    /// Removes callback added by [`fn@add_runtime_shutown_end_callback()`]
    pub fn remove_runtime_shutown_end_callback(&mut self) {
        unsafe { (*self.ptr).remove_runtime_shutdown_end_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_runtime_shutown_end_callback(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        unsafe { (*self.ptr).add_runtime_shutdown_end_cb(cb) };
    }
    /// Removes callback added by [`fn@add_context_loaded()`]
    pub fn remove_context_loaded(&mut self) {
        unsafe { (*self.ptr).remove_context_loaded_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_context_loaded(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        unsafe { (*self.ptr).add_context_loaded_cb(cb) };
    }
    /// Removes callback added by [`fn@add_context_unloaded()`]
    pub fn remove_context_unloaded(&mut self) {
        unsafe { (*self.ptr).remove_context_unloaded_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_context_unloaded(&mut self, cb: fn(profiler: &mut Profiler<T>)) {
        unsafe { (*self.ptr).add_context_unloaded_cb(cb) };
    }
    /// Removes callback added by [`fn@add_domain_loading()`]
    pub fn remove_domain_loading(&mut self) {
        unsafe { (*self.ptr).remove_domain_loading_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_domain_loading(&mut self, cb: fn(profiler: &mut Profiler<T>, &mut Domain)) {
        unsafe { (*self.ptr).add_domain_loading_cb(cb) };
    }
    /// Removes callback added by [`fn@add_domain_loaded()`]
    pub fn remove_domain_loaded(&mut self) {
        unsafe { (*self.ptr).remove_domain_loading_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_domain_loaded(&mut self, cb: fn(profiler: &mut Profiler<T>, &mut Domain)) {
        unsafe { (*self.ptr).add_domain_loading_cb(cb) };
    }
    /// Removes callback added by [`fn@add_domain_unloading()`]
    pub fn remove_domain_unloading(&mut self) {
        unsafe { (*self.ptr).remove_domain_unloading_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_domain_unloading(&mut self, cb: fn(profiler: &mut Profiler<T>, &mut Domain)) {
        unsafe { (*self.ptr).add_domain_unloading_cb(cb) };
    }
    /// Removes callback added by [`fn@add_domain_unloaded()`]
    pub fn remove_domain_unloaded(&mut self) {
        unsafe { (*self.ptr).remove_domain_unloading_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_domain_unloaded(&mut self, cb: fn(profiler: &mut Profiler<T>, &mut Domain)) {
        unsafe { (*self.ptr).add_domain_unloading_cb(cb) };
    }
    /// Removes callback added by [`fn@add_domain_name()`]
    pub fn remove_domain_name(&mut self) {
        unsafe { (*self.ptr).remove_domain_name_cb() };
    }
    /// Adds callback to be called when runtime is started.
    pub fn add_domain_name(&mut self, cb: fn(profiler: &mut Profiler<T>, &mut Domain, &str)) {
        unsafe { (*self.ptr).add_domain_name_cb(cb) };
    }
    /// Removes callback added by [fn@add_jit_begin()]
    pub fn remove_jit_begin(&mut self) {
        unsafe { (*self.ptr).remove_jit_begin_cb() };
    }
    #[doc(hidden)]
    /// Adds callback to be called when runtime is started.
    pub fn add_jit_begin(&mut self, cb: fn(profiler: &mut Profiler<T>, &Method<(String,)>)) {
        unsafe { (*self.ptr).add_jit_begin_cb(cb) };
    }
}