wasmer_c_api/wasm_c_api/version.rs
1use std::os::raw::c_char;
2use std::sync::LazyLock;
3
4const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");
5const VERSION_PRE: &str = concat!(env!("CARGO_PKG_VERSION_PRE"), "\0");
6
7static VERSION_MAJOR: LazyLock<u8> = LazyLock::new(|| {
8 env!("CARGO_PKG_VERSION_MAJOR")
9 .parse()
10 .expect("Failed to parse value for `VERSION_MAJOR` from `CARGO_PKG_VERSION_MAJOR`")
11});
12static VERSION_MINOR: LazyLock<u8> = LazyLock::new(|| {
13 env!("CARGO_PKG_VERSION_MINOR")
14 .parse()
15 .expect("Failed to parse value for `VERSION_MINOR` from `CARGO_PKG_VERSION_MINOR`")
16});
17static VERSION_PATCH: LazyLock<u8> = LazyLock::new(|| {
18 env!("CARGO_PKG_VERSION_PATCH")
19 .parse()
20 .expect("Failed to parse value for `VERSION_PATCH` from `CARGO_PKG_VERSION_PATCH`")
21});
22
23/// Get the version of the Wasmer C API.
24///
25/// The `.h` files already define variables like `WASMER_VERSION*`,
26/// but if this file is unreachable, one can use this function to
27/// retrieve the full semver version of the Wasmer C API.
28///
29/// The returned string is statically allocated. It must _not_ be
30/// freed!
31///
32/// # Example
33///
34/// See the module's documentation.
35#[unsafe(no_mangle)]
36pub unsafe extern "C" fn wasmer_version() -> *const c_char {
37 VERSION.as_ptr() as *const _
38}
39
40/// Get the major version of the Wasmer C API.
41///
42/// See [`wasmer_version`] to learn more.
43///
44/// # Example
45///
46/// ```rust
47/// # use inline_c::assert_c;
48/// # fn main() {
49/// # (assert_c! {
50/// # #include "tests/wasmer.h"
51/// #
52/// int main() {
53/// // Get and check the version components.
54/// uint8_t version_major = wasmer_version_major();
55/// uint8_t version_minor = wasmer_version_minor();
56/// uint8_t version_patch = wasmer_version_patch();
57///
58/// assert(version_major == WASMER_VERSION_MAJOR);
59/// assert(version_minor == WASMER_VERSION_MINOR);
60/// assert(version_patch == WASMER_VERSION_PATCH);
61///
62/// return 0;
63/// }
64/// # })
65/// # .success();
66/// # }
67/// ```
68#[unsafe(no_mangle)]
69pub unsafe extern "C" fn wasmer_version_major() -> u8 {
70 *VERSION_MAJOR
71}
72
73/// Get the minor version of the Wasmer C API.
74///
75/// See [`wasmer_version_major`] to learn more and get an example.
76#[unsafe(no_mangle)]
77pub unsafe extern "C" fn wasmer_version_minor() -> u8 {
78 *VERSION_MINOR
79}
80
81/// Get the patch version of the Wasmer C API.
82///
83/// See [`wasmer_version_major`] to learn more and get an example.
84#[unsafe(no_mangle)]
85pub unsafe extern "C" fn wasmer_version_patch() -> u8 {
86 *VERSION_PATCH
87}
88
89/// Get the minor version of the Wasmer C API.
90///
91/// See [`wasmer_version_major`] to learn more.
92///
93/// The returned string is statically allocated. It must _not_ be
94/// freed!
95///
96/// # Example
97///
98/// ```rust
99/// # use inline_c::assert_c;
100/// # fn main() {
101/// # (assert_c! {
102/// # #include "tests/wasmer.h"
103/// # #include <string.h>
104/// #
105/// int main() {
106/// // Get and check the pre version.
107/// const char* version_pre = wasmer_version_pre();
108/// assert(strcmp(version_pre, WASMER_VERSION_PRE) == 0);
109///
110/// // No need to free the string. It's statically allocated on
111/// // the Rust side.
112///
113/// return 0;
114/// }
115/// # })
116/// # .success();
117/// # }
118/// ```
119#[unsafe(no_mangle)]
120pub unsafe extern "C" fn wasmer_version_pre() -> *const c_char {
121 VERSION_PRE.as_ptr() as *const _
122}