Skip to main content

simplicity_sys/c_jets/
mod.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! FFI related to jets
4// Typically, the only things in the sys crate are the
5// FFI bindings. The high level code should be in the
6// main crate.
7// We don't follow this practice in the library of as now
8
9pub mod c_env;
10pub mod c_frame;
11pub mod frame_ffi;
12#[rustfmt::skip] pub mod jets_ffi;
13#[rustfmt::skip] pub mod jets_wrapper;
14
15pub use c_env::elements;
16pub use c_frame::{byte_width, uword_width};
17pub use frame_ffi::CFrameItem;
18
19// The bindings use elements_ffi instead of jets_ffi.
20pub use jets_ffi as elements_ffi;
21
22#[cfg(feature = "test-utils")]
23pub mod exec_ffi;
24
25/// sanity checks for using the types.
26/// We are not using the internal representation of the types at all, but
27/// we do care about the size and alignments of the types.
28///
29/// This will also us detect whenever there is a change in the underlying C representation
30pub fn sanity_checks() -> bool {
31    sanity_check_report().is_empty()
32}
33
34#[must_use]
35pub fn sanity_check_report() -> Vec<String> {
36    let mut issues = Vec::new();
37
38    macro_rules! check_layout {
39        ($ty:ty, $c_size:path, $c_align:path, $name:expr) => {{
40            let rust_size = std::mem::size_of::<$ty>();
41            let rust_align = std::mem::align_of::<$ty>();
42            let (c_size, c_align) = unsafe { ($c_size, $c_align) };
43
44            if rust_size != c_size {
45                issues.push(format!(
46                    "{name}: size mismatch (rust={rust_size}, c={c_size})",
47                    name = $name
48                ));
49            }
50
51            if rust_align != c_align {
52                issues.push(format!(
53                    "{name}: align mismatch (rust={rust_align}, c={c_align})",
54                    name = $name
55                ));
56            }
57        }};
58    }
59
60    check_layout!(
61        crate::ffi::UWORD,
62        crate::ffi::c_sizeof_UWORD,
63        crate::ffi::c_alignof_UWORD,
64        "UWORD"
65    );
66    check_layout!(
67        CFrameItem,
68        frame_ffi::c_sizeof_frameItem,
69        frame_ffi::c_alignof_frameItem,
70        "CFrameItem"
71    );
72    check_layout!(
73        elements::CRawBuffer,
74        elements::c_sizeof_rawBuffer,
75        elements::c_alignof_rawBuffer,
76        "CRawBuffer"
77    );
78    check_layout!(
79        elements::CRawInput,
80        elements::c_sizeof_rawInput,
81        elements::c_alignof_rawInput,
82        "CRawInput"
83    );
84    check_layout!(
85        elements::CRawOutput,
86        elements::c_sizeof_rawOutput,
87        elements::c_alignof_rawOutput,
88        "CRawOutput"
89    );
90    check_layout!(
91        elements::CRawTransaction,
92        elements::c_sizeof_rawTransaction,
93        elements::c_alignof_rawTransaction,
94        "CRawTransaction"
95    );
96    check_layout!(
97        elements::CTxEnv,
98        elements::c_sizeof_txEnv,
99        elements::c_alignof_txEnv,
100        "CTxEnv"
101    );
102    check_layout!(
103        elements::CRawTapEnv,
104        elements::c_sizeof_rawTapEnv,
105        elements::c_alignof_rawTapEnv,
106        "CRawTapEnv"
107    );
108
109    issues
110}