nng_sys/
lib.rs

1/*!
2
3## Examples
4
5```rust
6use nng_sys::*;
7use std::{ffi::CString, os::raw::c_char, ptr::null_mut};
8
9fn example() {
10    unsafe {
11        let url = CString::new("inproc://nng_sys/tests/example").unwrap();
12        let url = url.as_bytes_with_nul().as_ptr() as *const c_char;
13
14        // Reply socket
15        let mut rep_socket = nng_socket::default();
16        nng_rep0_open(&mut rep_socket);
17        nng_listen(rep_socket, url, null_mut(), 0);
18
19        // Request socket
20        let mut req_socket = nng_socket::default();
21        nng_req0_open(&mut req_socket);
22        nng_dial(req_socket, url, null_mut(), 0);
23
24        // Send message
25        let mut req_msg: *mut nng_msg = null_mut();
26        nng_msg_alloc(&mut req_msg, 0);
27        // Add a value to the body of the message
28        let val = 0x12345678;
29        nng_msg_append_u32(req_msg, val);
30        nng_sendmsg(req_socket, req_msg, 0);
31
32        // Receive it
33        let mut recv_msg: *mut nng_msg = null_mut();
34        nng_recvmsg(rep_socket, &mut recv_msg, 0);
35        // Remove our value from the body of the received message
36        let mut recv_val: u32 = 0;
37        nng_msg_trim_u32(recv_msg, &mut recv_val);
38        assert_eq!(val, recv_val);
39        // Can't do this because nng uses network order (big-endian)
40        //assert_eq!(val, *(nng_msg_body(recv_msg) as *const u32));
41
42        nng_close(req_socket);
43        nng_close(rep_socket);
44    }
45}
46```
47 */
48
49// Don't use std unless we're allowed
50#![cfg_attr(not(feature = "std"), no_std)]
51// Suppress the flurry of warnings caused by using "C" naming conventions
52#![allow(non_upper_case_globals)]
53#![allow(non_camel_case_types)]
54#![allow(non_snake_case)]
55// Disable clippy since this is all bindgen generated code
56#![allow(clippy::all)]
57
58// Either bindgen generated source, or the static copy
59mod bindings {
60    include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
61}
62
63#[cfg(try_from)]
64use core::convert::TryFrom;
65
66pub use crate::bindings::*;
67
68impl nng_pipe {
69    pub const NNG_PIPE_INITIALIZER: nng_pipe = nng_pipe {
70        _bindgen_opaque_blob: 0,
71    };
72}
73
74impl nng_socket {
75    pub const NNG_SOCKET_INITIALIZER: nng_socket = nng_socket {
76        _bindgen_opaque_blob: 0,
77    };
78}
79
80impl nng_dialer {
81    pub const NNG_DIALER_INITIALIZER: nng_dialer = nng_dialer {
82        _bindgen_opaque_blob: 0,
83    };
84}
85
86impl nng_listener {
87    pub const NNG_LISTENER_INITIALIZER: nng_listener = nng_listener {
88        _bindgen_opaque_blob: 0,
89    };
90}
91
92impl nng_ctx {
93    pub const NNG_CTX_INITIALIZER: nng_ctx = nng_ctx {
94        _bindgen_opaque_blob: 0,
95    };
96}
97
98/// The error type returned when unable to convert an integer to an enum value.
99#[derive(Debug, Copy, Clone, PartialEq, Eq)]
100#[cfg(try_from)]
101pub struct EnumFromIntError(pub i32);
102
103#[cfg(all(try_from, feature = "std"))]
104impl std::fmt::Display for EnumFromIntError {
105    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
106        write!(fmt, "EnumFromIntError({})", self.0)
107    }
108}
109
110impl nng_stat_type_enum {
111    /// Converts value returned by [nng_stat_type](https://nanomsg.github.io/nng/man/v1.1.0/nng_stat_type.3) into `nng_stat_type_enum`.
112    pub fn try_convert_from(value: i32) -> Option<Self> {
113        use crate::nng_stat_type_enum::*;
114        match value {
115            value if value == NNG_STAT_SCOPE as i32 => Some(NNG_STAT_SCOPE),
116            value if value == NNG_STAT_LEVEL as i32 => Some(NNG_STAT_LEVEL),
117            value if value == NNG_STAT_COUNTER as i32 => Some(NNG_STAT_COUNTER),
118            value if value == NNG_STAT_STRING as i32 => Some(NNG_STAT_STRING),
119            value if value == NNG_STAT_BOOLEAN as i32 => Some(NNG_STAT_BOOLEAN),
120            value if value == NNG_STAT_ID as i32 => Some(NNG_STAT_ID),
121            _ => None,
122        }
123    }
124}
125
126#[cfg(try_from)]
127impl TryFrom<i32> for nng_stat_type_enum {
128    type Error = EnumFromIntError;
129    fn try_from(value: i32) -> Result<Self, Self::Error> {
130        nng_stat_type_enum::try_convert_from(value).ok_or(EnumFromIntError(value))
131    }
132}
133
134impl nng_unit_enum {
135    /// Converts value returned by [nng_stat_unit](https://nanomsg.github.io/nng/man/v1.1.0/nng_stat_unit.3) into `nng_unit_enum`.
136    pub fn try_convert_from(value: i32) -> Option<Self> {
137        use crate::nng_unit_enum::*;
138        match value {
139            value if value == NNG_UNIT_NONE as i32 => Some(NNG_UNIT_NONE),
140            value if value == NNG_UNIT_BYTES as i32 => Some(NNG_UNIT_BYTES),
141            value if value == NNG_UNIT_MESSAGES as i32 => Some(NNG_UNIT_MESSAGES),
142            value if value == NNG_UNIT_MILLIS as i32 => Some(NNG_UNIT_MILLIS),
143            value if value == NNG_UNIT_EVENTS as i32 => Some(NNG_UNIT_EVENTS),
144            _ => None,
145        }
146    }
147}
148
149#[cfg(try_from)]
150impl TryFrom<i32> for nng_unit_enum {
151    type Error = EnumFromIntError;
152    fn try_from(value: i32) -> Result<Self, Self::Error> {
153        nng_unit_enum::try_convert_from(value).ok_or(EnumFromIntError(value))
154    }
155}
156
157impl nng_sockaddr_family {
158    pub fn try_convert_from(value: i32) -> Option<Self> {
159        use crate::nng_sockaddr_family::*;
160        match value {
161            value if value == NNG_AF_UNSPEC as i32 => Some(NNG_AF_UNSPEC),
162            value if value == NNG_AF_INPROC as i32 => Some(NNG_AF_INPROC),
163            value if value == NNG_AF_IPC as i32 => Some(NNG_AF_IPC),
164            value if value == NNG_AF_INET as i32 => Some(NNG_AF_INET),
165            value if value == NNG_AF_INET6 as i32 => Some(NNG_AF_INET6),
166            value if value == NNG_AF_ZT as i32 => Some(NNG_AF_ZT),
167            _ => None,
168        }
169    }
170}
171
172#[cfg(try_from)]
173impl TryFrom<i32> for nng_sockaddr_family {
174    type Error = EnumFromIntError;
175    fn try_from(value: i32) -> Result<Self, Self::Error> {
176        nng_sockaddr_family::try_convert_from(value).ok_or(EnumFromIntError(value))
177    }
178}