coolprop_sys/lib.rs
1//! [<img alt="GitHub" src="https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="22">](https://github.com/portyanikhin/rfluids)
2//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="22">](https://docs.rs/coolprop-sys)
3//! [<img alt="crates.io" src="https://img.shields.io/crates/v/coolprop-sys?style=for-the-badge&logo=rust&labelColor=555555&color=fc8d62" height="22">](https://crates.io/crates/coolprop-sys)
4//! [<img alt="CI" src="https://img.shields.io/github/actions/workflow/status/portyanikhin/rfluids/ci.yml?style=for-the-badge&logo=githubactions&logoColor=ffffff&label=ci&labelColor=555555" height="22">](https://github.com/portyanikhin/rfluids/actions/workflows/ci.yml)
5//!
6//! Raw FFI bindings to [`CoolProp`](https://coolprop.org)
7//!
8//! ## Supported platforms
9//!
10//! - `Linux x86-64`
11//! - `macOS AArch64`
12//! - `macOS x86-64`
13//! - `Windows AArch64`
14//! - `Windows x86-64`
15//!
16//! ## MSRV
17//!
18//! `coolprop-sys` requires `rustc` 1.85.0 or later.
19//!
20//! ## How to install
21//!
22//! Add this to your `Cargo.toml`:
23//!
24//! ```toml
25//! [dependencies]
26//! coolprop-sys = "7"
27//! ```
28//!
29//! Or via command line:
30//!
31//! ```shell
32//! cargo add coolprop-sys
33//! ```
34//!
35//! 🎁 It comes with native `CoolProp` dynamic libraries for supported platforms. The library
36//! required for your platform will be automatically copied to the target directory during build.
37//!
38//! It also includes pre-generated FFI bindings, so `libclang` is not required for normal builds.
39//!
40//! ### Regenerating bindings
41//!
42//! If you need to regenerate the FFI bindings (requires `libclang`), enable the
43//! **`regen-bindings`** feature.
44//!
45//! Add this to your `Cargo.toml`:
46//!
47//! ```toml
48//! [dependencies]
49//! coolprop-sys = { version = "7", features = ["regen-bindings"] }
50//! ```
51//!
52//! Or via command line:
53//!
54//! ```shell
55//! cargo add coolprop-sys --features regen-bindings
56//! ```
57//!
58//! #### License
59//!
60//! <sup>
61//! This project is licensed under
62//! <a href="https://github.com/portyanikhin/rfluids/blob/main/LICENSE">MIT License</a>.
63//! </sup>
64
65use std::sync::{LazyLock, Mutex};
66
67pub mod bindings;
68
69/// `CoolProp` dynamic library absolute path.
70#[cfg(all(target_os = "windows", target_arch = "x86_64"))]
71pub const COOLPROP_PATH: &str = coolprop_sys_windows_x86_64::COOLPROP_PATH;
72#[cfg(all(target_os = "windows", target_arch = "aarch64"))]
73pub const COOLPROP_PATH: &str = coolprop_sys_windows_aarch64::COOLPROP_PATH;
74#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
75pub const COOLPROP_PATH: &str = coolprop_sys_linux_x86_64::COOLPROP_PATH;
76#[cfg(all(target_os = "macos", target_arch = "x86_64"))]
77pub const COOLPROP_PATH: &str = coolprop_sys_macos_x86_64::COOLPROP_PATH;
78#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
79pub const COOLPROP_PATH: &str = coolprop_sys_macos_aarch64::COOLPROP_PATH;
80
81/// Global instance of the `CoolProp` dynamic library.
82///
83/// Provides thread-safe access to a single `CoolProp` instance across the entire application.
84/// The library is loaded lazily on first access using [`LazyLock`].
85///
86/// Access to the library is protected by a [`Mutex`], ensuring thread safety.
87/// To use the library, acquire the lock:
88///
89/// ```no_run
90/// use coolprop_sys::COOLPROP;
91///
92/// let coolprop = COOLPROP.lock().unwrap();
93/// // Use CoolProp methods here
94/// ```
95///
96/// # Panics
97///
98/// Panics on initialization if the `CoolProp` dynamic library cannot be loaded
99/// (e.g., if the library file is missing or corrupted).
100///
101/// # Safety
102///
103/// Internally uses `unsafe` to load the dynamic library via FFI.
104/// Safety is ensured because:
105/// - The library is loaded from the verified [`COOLPROP_PATH`]
106/// - Loading occurs once during initialization
107/// - All subsequent accesses work with the already loaded library
108///
109/// # See Also
110///
111/// - [`CoolPropLib.h` Reference](https://coolprop.org/_static/doxygen/html/_cool_prop_lib_8h.html)
112pub static COOLPROP: LazyLock<Mutex<bindings::CoolProp>> = LazyLock::new(|| {
113 Mutex::new(
114 unsafe { bindings::CoolProp::new(COOLPROP_PATH) }
115 .expect("Unable to load CoolProp dynamic library!"),
116 )
117});