dqcsim 0.0.4

DQCsim: Delft Quantum Classical Simulator
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
use super::*;

/// Creates a new `PluginDefinition` object.
///>
///> Plugin definitions contain the callback functions/closures that define the
///> functionality of a plugin. They also contain some metadata to identify the
///> implementation, in the form of a name, author, and version string, that
///> must be specified when the definition is constructed. The callback
///> functions/closures are initialized to sane defaults for the requested
///> plugin type, but obviously one or more of these should be overridden to
///> make the plugin do something.
///>
///> Once a definition object has been built, it can be used to spawn a plugin
///> thread or run a plugin in the main thread, given a DQCsim server URL for it
///> to connect to.
#[no_mangle]
pub extern "C" fn dqcs_pdef_new(
    typ: dqcs_plugin_type_t,
    name: *const c_char,
    author: *const c_char,
    version: *const c_char,
) -> dqcs_handle_t {
    api_return(0, || {
        let typ: Result<PluginType> = typ.into();
        Ok(insert(PluginDefinition::new(
            typ?,
            PluginMetadata::new(
                receive_optional_str(name)?
                    .filter(|x| !x.is_empty())
                    .ok_or_else(oe_inv_arg("plugin name is required"))?,
                receive_optional_str(author)?
                    .filter(|x| !x.is_empty())
                    .ok_or_else(oe_inv_arg("author name is required"))?,
                receive_optional_str(version)?
                    .filter(|x| !x.is_empty())
                    .ok_or_else(oe_inv_arg("version string is required"))?,
            ),
        )))
    })
}

/// Returns the plugin type for the given plugin definition object.
#[no_mangle]
pub extern "C" fn dqcs_pdef_type(pdef: dqcs_handle_t) -> dqcs_plugin_type_t {
    api_return(dqcs_plugin_type_t::DQCS_PTYPE_INVALID, || {
        resolve!(pdef as &PluginDefinition);
        Ok(pdef.get_type().into())
    })
}

/// Returns the plugin name for the given plugin definition object.
///>
///> On success, this **returns a newly allocated string containing the JSON
///> string. Free it with `free()` when you're done with it to avoid memory
///> leaks.** On failure, this returns `NULL`.
#[no_mangle]
pub extern "C" fn dqcs_pdef_name(pdef: dqcs_handle_t) -> *mut c_char {
    api_return_string(|| {
        resolve!(pdef as &PluginDefinition);
        Ok(pdef.get_metadata().get_name().to_string())
    })
}

/// Returns the plugin author for the given plugin definition object.
///>
///> On success, this **returns a newly allocated string containing the JSON
///> string. Free it with `free()` when you're done with it to avoid memory
///> leaks.** On failure, this returns `NULL`.
#[no_mangle]
pub extern "C" fn dqcs_pdef_author(pdef: dqcs_handle_t) -> *mut c_char {
    api_return_string(|| {
        resolve!(pdef as &PluginDefinition);
        Ok(pdef.get_metadata().get_author().to_string())
    })
}

/// Returns the plugin version for the given plugin definition object.
///>
///> On success, this **returns a newly allocated string containing the JSON
///> string. Free it with `free()` when you're done with it to avoid memory
///> leaks.** On failure, this returns `NULL`.
#[no_mangle]
pub extern "C" fn dqcs_pdef_version(pdef: dqcs_handle_t) -> *mut c_char {
    api_return_string(|| {
        resolve!(pdef as &PluginDefinition);
        Ok(pdef.get_metadata().get_version().to_string())
    })
}

/// Sets the user logic initialization callback.
///>
///> This is always called before any of the other callbacks are run. The
///> downstream plugin has already been initialized at this stage, so it is
///> legal to send it commands.
///>
///> The default behavior is no-op.
///>
///> Besides the common arguments, the callback receives a handle to an
///> `ArbCmd` queue (`dqcs_cq_*`, `dqcs_cmd_*`, and `dqcs_arb_*` interfaces)
///> containing user-defined initialization commands. This is a borrowed
///> handle; the caller will delete it.
///>
///> The callback can return an error by setting an error message using
///> `dqcs_error_set()` and returning `DQCS_FAILURE`. Otherwise, it should
///> return `DQCS_SUCCESS`.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_initialize_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            init_cmds: dqcs_handle_t,
        ) -> dqcs_return_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        pdef.initialize = Box::new(
            move |state: &mut PluginState, init_cmds: Vec<ArbCmd>| -> Result<()> {
                let init_cmds: ArbCmdQueue = init_cmds.into_iter().collect();
                let init_cmds = insert(init_cmds);
                let result = cb_return_none(callback(data.data(), state.into(), init_cmds));
                delete!(init_cmds);
                result
            },
        );
        Ok(())
    })
}

/// Sets the user logic drop/cleanup callback.
///>
///> This is called when a plugin is gracefully terminated. It is not
///> recommended to execute any downstream instructions at this time, but it
///> is supported in case this is really necessary.
///>
///> The default behavior is no-op.
///>
///> The callback can return an error by setting an error message using
///> `dqcs_error_set()` and returning `DQCS_FAILURE`. Otherwise, it should
///> return `DQCS_SUCCESS`.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_drop_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(user_data: *mut c_void, state: dqcs_plugin_state_t) -> dqcs_return_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        pdef.drop = Box::new(move |state: &mut PluginState| -> Result<()> {
            cb_return_none(callback(data.data(), state.into()))
        });
        Ok(())
    })
}

/// Sets the run callback for frontends.
///>
///> This is called in response to a `start()` host API call. The return
///> value is returned through the `wait()` host API call.
///>
///> The default behavior is to fail with a "not implemented" error;
///> frontends backends should always override this. This callback is never
///> called for operator or backend plugins.
///>
///> Besides the common arguments, the callback receives a handle to an
///> `ArbData` object containing the data that the host passed to `start()`.
///> This is a borrowed handle; the caller will delete it.
///>
///> When the run callback is successful, it should return a valid `ArbData`
///> handle. This can be the same as the argument, but it can also be a new
///> object. This `ArbData` is returned to the host through `wait()`. This
///> `ArbData` object is deleted after the callback completes.
///>
///> The callback can return an error by setting an error message using
///> `dqcs_error_set()` and returning 0. Otherwise, it should return a
///> valid `ArbData` handle.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_run_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            args: dqcs_handle_t,
        ) -> dqcs_handle_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        if pdef.get_type() != PluginType::Frontend {
            return inv_op("the run() callback is only supported for frontends");
        }
        pdef.run = Box::new(
            move |state: &mut PluginState, args: ArbData| -> Result<ArbData> {
                let args = insert(args);
                let result =
                    cb_return(0, callback(data.data(), state.into(), args)).and_then(|arb| {
                        take!(arb as ArbData);
                        Ok(arb)
                    });
                delete!(args);
                result
            },
        );
        Ok(())
    })
}

/// Sets the qubit allocation callback for operators and backends.
///>
///> The default for operators is to pass through to
///> `dqcs_plugin_allocate()`. The default for backends is no-op. This
///> callback is never called for frontend plugins.
///>
///> Besides the common arguments, the callback receives a handle to a qubit
///> set containing the references that are to be used for the
///> to-be-allocated qubits and an `ArbCmd` queue containing user-defined
///> commands to optionally augment the behavior of the qubits. These are
///> borrowed handles; the caller will delete them.
///>
///> The callback can return an error by setting an error message using
///> `dqcs_error_set()` and returning `DQCS_FAILURE`. Otherwise, it should
///> return `DQCS_SUCCESS`.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_allocate_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            qubits: dqcs_handle_t,
            alloc_cmds: dqcs_handle_t,
        ) -> dqcs_return_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        if pdef.get_type() == PluginType::Frontend {
            return inv_op("the allocate() callback is not supported for frontends");
        }
        pdef.allocate = Box::new(
            move |state: &mut PluginState,
                  qubits: Vec<QubitRef>,
                  alloc_cmds: Vec<ArbCmd>|
                  -> Result<()> {
                let qubits: QubitReferenceSet = qubits.into_iter().collect();
                let qubits = insert(qubits);
                let alloc_cmds: ArbCmdQueue = alloc_cmds.into_iter().collect();
                let alloc_cmds = insert(alloc_cmds);
                let result =
                    cb_return_none(callback(data.data(), state.into(), qubits, alloc_cmds));
                delete!(qubits);
                delete!(alloc_cmds);
                result
            },
        );
        Ok(())
    })
}

/// Sets the qubit deallocation callback for operators and backends.
///>
///> The default for operators is to pass through to `dqcs_plugin_free()`.
///> The default for backends is no-op. This callback is never called for
///> frontend plugins.
///>
///> Besides the common arguments, the callback receives a handle to a qubit
///> set containing the qubits that are to be freed. This is a borrowed
///> handle; the caller will delete it.
///>
///> The callback can return an error by setting an error message using
///> `dqcs_error_set()` and returning `DQCS_FAILURE`. Otherwise, it should
///> return `DQCS_SUCCESS`.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_free_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            qubits: dqcs_handle_t,
        ) -> dqcs_return_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        if pdef.get_type() == PluginType::Frontend {
            return inv_op("the free() callback is not supported for frontends");
        }
        pdef.free = Box::new(
            move |state: &mut PluginState, qubits: Vec<QubitRef>| -> Result<()> {
                let qubits: QubitReferenceSet = qubits.into_iter().collect();
                let qubits = insert(qubits);
                let result = cb_return_none(callback(data.data(), state.into(), qubits));
                delete!(qubits);
                result
            },
        );
        Ok(())
    })
}

/// Sets the gate execution callback for operators and backends.
///>
///> Besides the common arguments, the callback receives a handle to the
///> to-be-executed gate. This is a borrowed handle; the caller will delete
///> it.
///>
///> The callback must return one of the following things:
///>
///>  - a valid handle to a measurement set, created using
///>    `dqcs_mset_new()` (this object is automatically deleted after the
///>    callback returns);
///>  - a valid handle to a single qubit measurement, created using
///>    `dqcs_meas_new()` (this object is automatically deleted after the
///>    callback returns);
///>  - the handle to the supplied gate, a shortcut for not returning any
///>    measurements (this is less clear than returning an empty measurement
///>    set, but slightly faster); or
///>  - 0 to report an error, after calling the error string using
///>    `dqcs_set_error()`.
///>
///> Backend plugins must return a measurement result set containing exactly
///> those qubits specified in the measurement set. For operators, however,
///> the story is more complicated. Let's say we want to make a silly
///> operator that inverts all measurements. The trivial way to do
///> this would be to forward the gate, query all the measurement results
///> using `dqcs_plugin_get_measurement()`, invert them, stick them in a
///> measurement result set, and return that result set. However, this
///> approach is not very efficient, because `dqcs_plugin_get_measurement()`
///> has to wait for all downstream plugins to finish executing the gate,
///> forcing the OS to switch threads, etc. Instead, operators are allowed
///> to return only a subset (or none) of the measured qubits, as long as
///> they return the measurements as they arrive through the
///> `modify_measurement()` callback.
///>
///> The default implementation for this callback for operators is to pass
///> the gate through to the downstream plugin and return an empty set of
///> measurements. Combined with the default implementation of
///> `modify_measurement()`, this behavior is sane. Backends must override
///> this callback; the default is to return a not-implemented error.
///>
///> Note that for our silly example operator, the default behavior for this
///> function is sufficient; you'd only have to override
///> `modify_measurement()` to, well, modify the measurements.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_gate_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            gate: dqcs_handle_t,
        ) -> dqcs_handle_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        if pdef.get_type() == PluginType::Frontend {
            return inv_op("the gate() callback is not supported for frontends");
        }
        pdef.gate = Box::new(
            move |state: &mut PluginState, gate: Gate| -> Result<Vec<QubitMeasurementResult>> {
                let gate_handle = insert(gate);
                let result = cb_return(0, callback(data.data(), state.into(), gate_handle))
                    .and_then(|result| {
                        if result == gate_handle {
                            Ok(vec![])
                        } else {
                            take!(result as QubitMeasurementResultSet);
                            Ok(result.into_iter().map(|(_, m)| m).collect())
                        }
                    });
                delete!(gate_handle);
                result
            },
        );
        Ok(())
    })
}

/// Sets the measurement modification callback for operators.
///>
///> This callback is called for every measurement result received from the
///> downstream plugin, and returns the measurements that should be reported
///> to the upstream plugin. Note that the results from our plugin's
///> `dqcs_plugin_get_measurement()` and friends are consistent with the
///> results received from downstream; they are not affected by this
///> function.
///>
///> The callback takes a handle to a single qubit measurement object as an
///> argument, and must return one of the following things:
///>
///>  - a valid handle to a measurement set, created using
///>    `dqcs_mset_new()` (this object is automatically deleted after the
///>    callback returns);
///>  - a valid handle to a single qubit measurement object, which may or
///>    may not be the supplied one (this object is automatically deleted
///>    after the callback returns); or
///>  - 0 to report an error, after calling the error string using
///>    `dqcs_set_error()`.
///>
///> This callback is somewhat special in that it is not allowed to call
///> any plugin command other than logging and the pseudorandom number
///> generator functions. This is because this function is called
///> asynchronously with respect to the downstream functions, making the
///> timing of these calls non-deterministic based on operating system
///> scheduling.
///>
///> Note that while this function is called for only a single measurement
///> at a time, it is allowed to produce a vector of measurements. This
///> allows you to cancel propagation of the measurement by returning an
///> empty vector, to just modify the measurement data itself, or to
///> generate additional measurements from a single measurement. However,
///> if you need to modify the qubit references for operators that remap
///> qubits, take care to only send measurement data upstream when these
///> were explicitly requested through the associated upstream gate
///> function's `measured` list.
///>
///> The default behavior for this callback is to return the measurement
///> without modification.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_modify_measurement_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            meas: dqcs_handle_t,
        ) -> dqcs_handle_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        if pdef.get_type() != PluginType::Operator {
            return inv_op("the modify_measurement() callback is only supported for operators");
        }
        pdef.modify_measurement = Box::new(
            move |state: &mut PluginState,
                  meas: QubitMeasurementResult|
                  -> Result<Vec<QubitMeasurementResult>> {
                let meas_handle = insert(meas);
                let result = cb_return(0, callback(data.data(), state.into(), meas_handle))
                    .and_then(|result| {
                        take!(result as QubitMeasurementResultSet);
                        Ok(result.into_iter().map(|(_, m)| m).collect())
                    });
                delete!(meas_handle);
                result
            },
        );
        Ok(())
    })
}

/// Sets the callback for advancing time for operators and backends.
///>
///> The default behavior for operators is to pass through to
///> `dqcs_plugin_advance()`. The default for backends is no-op. This
///> callback is never called for frontend plugins.
///>
///> Besides the common arguments, the callback receives an unsigned integer
///> specifying the number of cycles to advance by.
///>
///> The callback can return an error by setting an error message using
///> `dqcs_error_set()` and returning `DQCS_FAILURE`. Otherwise, it should
///> return `DQCS_SUCCESS`.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_advance_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            cycles: dqcs_cycle_t,
        ) -> dqcs_return_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        if pdef.get_type() == PluginType::Frontend {
            return inv_op("the advance() callback is not supported for frontends");
        }
        pdef.advance = Box::new(move |state: &mut PluginState, cycles: u64| -> Result<()> {
            cb_return_none(callback(data.data(), state.into(), cycles as dqcs_cycle_t))
        });
        Ok(())
    })
}

/// Sets the callback function for handling an arb from upstream for
/// operators and backends.
///>
///> The default behavior for operators is to pass through to
///> `dqcs_plugin_arb()`; operators that do not support the requested
///> interface should always do this. The default for backends is no-op.
///> This callback is never called for frontend plugins.
///>
///> Besides the common arguments, the callback receives a handle to the
///> `ArbCmd` object representing the request. It must return a valid
///> `ArbData` handle containing the response. Both objects are deleted
///> automatically after invocation.
///>
///> The callback can return an error by setting an error message using
///> `dqcs_error_set()` and returning 0. Otherwise, it should return a valid
///> `ArbData` handle.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_upstream_arb_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            cmd: dqcs_handle_t,
        ) -> dqcs_handle_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        if pdef.get_type() == PluginType::Frontend {
            return inv_op("the upstream_arb() callback is not supported for frontends");
        }
        pdef.upstream_arb = Box::new(
            move |state: &mut PluginState, cmd: ArbCmd| -> Result<ArbData> {
                let cmd_handle = insert(cmd);
                let result = cb_return(0, callback(data.data(), state.into(), cmd_handle))
                    .and_then(|result| {
                        take!(result as ArbData);
                        Ok(result)
                    });
                delete!(cmd_handle);
                result
            },
        );
        Ok(())
    })
}

/// Sets the callback function function for handling an arb from the host.
///>
///> The default behavior for this is no-op.
///>
///> Besides the common arguments, the callback receives a handle to the
///> `ArbCmd` object representing the request. It must return a valid
///> `ArbData` handle containing the response. Both objects are deleted
///> automatically after invocation.
///>
///> The callback can return an error by setting an error message using
///> `dqcs_error_set()` and returning 0. Otherwise, it should return a valid
///> `ArbData` handle.
#[no_mangle]
pub extern "C" fn dqcs_pdef_set_host_arb_cb(
    pdef: dqcs_handle_t,
    callback: Option<
        extern "C" fn(
            user_data: *mut c_void,
            state: dqcs_plugin_state_t,
            cmd: dqcs_handle_t,
        ) -> dqcs_handle_t,
    >,
    user_free: Option<extern "C" fn(user_data: *mut c_void)>,
    user_data: *mut c_void,
) -> dqcs_return_t {
    api_return_none(|| {
        let data = CallbackUserData::new(user_free, user_data);
        let callback = callback.ok_or_else(oe_inv_arg("callback cannot be null"))?;
        resolve!(pdef as &mut PluginDefinition);
        pdef.host_arb = Box::new(
            move |state: &mut PluginState, cmd: ArbCmd| -> Result<ArbData> {
                let cmd_handle = insert(cmd);
                let result = cb_return(0, callback(data.data(), state.into(), cmd_handle))
                    .and_then(|result| {
                        take!(result as ArbData);
                        Ok(result)
                    });
                delete!(cmd_handle);
                result
            },
        );
        Ok(())
    })
}