libmqm_sys/
dlopen2.rs

1/*!
2Dynamic loading of the MQI library using dlopen2
3
4Example
5-------
6 Dynamically load the `libmqm_r` library and issue an `MQCONN`
7
8```no_run
9use dlopen2::wrapper::Container;
10use libmqm_sys::{self as mq, dlopen2::MqWrapper};
11
12# fn main() -> Result<(), dlopen2::Error> {
13#
14// Dynamically load the libmqm_r library
15let mq: Container<MqWrapper> = unsafe { Container::load("libmqm_r") }?;
16
17// Connect to MQ
18let mut hconn = mq::MQHC_DEF_HCONN;
19let mut comp_code = mq::MQCC_UNKNOWN;
20let mut reason = mq::MQRC_NONE;
21let mut qmgr: mq::MQCHAR48 = [32; 48]; // All spaces
22unsafe {
23   mq.MQCONN(
24     &qmgr,
25     &mut hconn,
26     &mut comp_code,
27     &mut reason,
28   );
29}
30#
31# Ok(())
32# }
33```
34*/
35
36use ::dlopen2::wrapper::Container;
37
38/// A dlopen2 [`WrapperApi`](::dlopen2::wrapper::WrapperApi) implementation for MQ function calls
39pub use super::generated::dlopen2::MqWrapper;
40
41/// Name of the platform dependent MQM dynamic library
42pub const MQM_LIB: &str = if cfg!(windows) { "mqm.dll" } else { "libmqm_r.so" };
43
44/// A [dlopen2] [Container] for the MQI library
45pub type MqmContainer = Container<MqWrapper>;
46
47/// Extension trait for [`MqmContainer`] to load the MQM library using dlopen2
48pub trait LoadMqmExt {
49    /// Loads the MQM library using the platform dependent search rules
50    ///
51    /// # Safety
52    /// Loading the dynamic library is inherently unsafe
53    ///
54    /// # Errors
55    /// Will return `Err` if the dynamic library could not be loaded
56    unsafe fn load_mqm_default() -> Result<Self, ::dlopen2::Error>
57    where
58        Self: std::marker::Sized;
59}
60
61impl LoadMqmExt for MqmContainer {
62    unsafe fn load_mqm_default() -> Result<Self, ::dlopen2::Error> {
63        unsafe { Self::load(MQM_LIB) }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use dlopen2::wrapper::Container;
70
71    use crate as mq;
72
73    use super::*;
74
75    #[test]
76    fn mqdist_load_default() {
77        let _ = unsafe { MqmContainer::load_mqm_default() }.expect("MQM library to be loaded");
78    }
79
80    #[test]
81    fn mqredist_load() -> Result<(), dlopen2::Error> {
82        // Dynamically load the mqm library
83        let mq: Container<MqWrapper> = unsafe { Container::load(MQM_LIB) }?;
84
85        let mut hconn = mq::MQHC_DEF_HCONN;
86        let mut comp_code = mq::MQCC_UNKNOWN;
87        let mut reason = mq::MQRC_NONE;
88        let qmgr: mq::MQCHAR48 = [32; 48]; // All spaces
89        unsafe {
90            mq.MQCONN(&qmgr, &mut hconn, &mut comp_code, &mut reason);
91        }
92
93        Ok(())
94    }
95}