qir_backend/
result_bool.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3#![deny(clippy::all, clippy::pedantic)]
4
5use qir_stdlib::strings::__quantum__rt__string_create;
6use std::ffi::{CString, c_char, c_void};
7
8#[unsafe(no_mangle)]
9pub extern "C" fn __quantum__rt__result_get_zero() -> *mut c_void {
10    std::ptr::null_mut()
11}
12
13#[unsafe(no_mangle)]
14#[allow(clippy::manual_dangling_ptr)]
15pub extern "C" fn __quantum__rt__result_get_one() -> *mut c_void {
16    1 as *mut c_void
17}
18
19#[unsafe(no_mangle)]
20pub extern "C" fn __quantum__rt__result_equal(r1: *mut c_void, r2: *mut c_void) -> bool {
21    r1 == r2
22}
23
24#[unsafe(no_mangle)]
25pub extern "C" fn __quantum__rt__result_update_reference_count(_res: *mut c_void, _update: i32) {
26    // no-op
27}
28
29/// # Panics
30/// This function panics if the memory cannot be allocated for the result string.
31#[unsafe(no_mangle)]
32pub extern "C" fn __quantum__rt__result_to_string(res: *mut c_void) -> *const CString {
33    unsafe {
34        __quantum__rt__string_create(
35            CString::new(
36                if __quantum__rt__result_equal(res, __quantum__rt__result_get_one()) {
37                    "One"
38                } else {
39                    "Zero"
40                },
41            )
42            .expect("Failed to allocate memory for result string.")
43            .as_bytes_with_nul()
44            .as_ptr() as *mut c_char,
45        )
46    }
47}