Skip to content

Commit 3f7cd2d

Browse files
committed
Update crate meta-data, docs, build-nng feature, patched cty
- build-nng feature from Nathan: https://gitlab.com/neachdainn/nng-rs/blob/master/nng-sys/build.rs
1 parent 1b54cc9 commit 3f7cd2d

File tree

6 files changed

+158
-75
lines changed

6 files changed

+158
-75
lines changed

Cargo.toml

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
[package]
2-
name = "runng-sys"
3-
version = "0.5.0"
4-
authors = ["Jake W <[email protected]>"]
2+
name = "nng-sys"
3+
version = "1.1.1-rc.1"
4+
authors = ["Nathan Kent <[email protected]>", "Jake W <[email protected]>"]
55

6-
description = "Bindings to nng (Nanomsg-Next-Generation) aka Nanomsg2"
7-
#keywords = ["nng", "nanomsg", "zeromq"]
8-
#categories = ["network-programming", "external-ffi-bindings", "os"]
6+
description = "Bindings to NNG (Nanomsg-Next-Generation) aka Nanomsg2"
7+
keywords = ["nng", "nanomsg", "zeromq"]
8+
categories = ["network-programming", "external-ffi-bindings", "os"]
99
license = "MIT"
10-
repository = "https://github.com/jeikabu/runng"
10+
repository = "https://github.com/jeikabu/nng-rust"
1111
readme = "README.md"
1212
edition = "2018"
1313

1414
[badges]
15-
appveyor = { repository = "jake-ruyi/runng", branch = "master", service = "github" }
16-
travis-ci = { repository = "jeikabu/runng", branch = "master" }
15+
appveyor = { repository = "jake-ruyi/nng-rust", branch = "master", service = "github" }
16+
travis-ci = { repository = "jeikabu/nng-rust", branch = "master" }
1717

1818
[features]
19-
default = ["nng-stats"]
19+
default = ["build-nng", "nng-stats"]
20+
build-nng = []
2021
# Cmake generators
2122
cmake-ninja = [] # Ninja
2223
cmake-vs2017 = [] # Visual Studio 15 2017
@@ -31,6 +32,9 @@ legacy-111-rc4 = []
3132
[dependencies]
3233
cty = "0.1"
3334

35+
[patch.crates-io]
36+
cty = { git = 'https://github.com/jeikabu/cty' }
37+
3438
[build-dependencies]
3539
cmake = "0.1"
3640
bindgen = "0.47"

LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License
2+
3+
Copyright 2018 Staysail Systems, Inc. <[email protected]>
4+
Copyright 2018 Capitar IT Group BV <[email protected]>
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"),
8+
to deal in the Software without restriction, including without limitation
9+
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10+
and/or sell copies of the Software, and to permit persons to whom
11+
the Software is furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included
14+
in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19+
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22+
IN THE SOFTWARE.

README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11

2-
# runng-sys
3-
4-
Rust FFI bindings to [NNG](https://github.com/nanomsg/nng) (generated with [bindgen](https://rust-lang.github.io/rust-bindgen/introduction.html)):
2+
Rust FFI bindings to [NNG](https://github.com/nanomsg/nng):
53

64
> NNG, like its predecessors nanomsg (and to some extent ZeroMQ), is a lightweight, broker-less library, offering a simple API to solve common recurring messaging problems, such as publish/subscribe, RPC-style request/reply, or service discovery. The API frees the programmer from worrying about details like connection management, retries, and other common considerations, so that they can focus on the application instead of the plumbing.
75
6+
[![docs.rs](https://docs.rs/nng-sys/badge.svg)](https://docs.rs/nng-sys)
7+
[![crates.io](http://img.shields.io/crates/v/nng-sys.svg)](http://crates.io/crates/nng-sys)
8+
![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)
9+
![Rustc 1.31+](https://img.shields.io/badge/rustc-1.31+-lightgray.svg)
10+
[![travis](https://travis-ci.org/jeikabu/nng-rust.svg?branch=master)](https://travis-ci.org/jeikabu/nng-rust)
11+
[![appveyor](https://ci.appveyor.com/api/projects/status/96x8onexj6vyxgjp/branch/master?svg=true)](https://ci.appveyor.com/project/jake-ruyi/nng-rust/branch/master)
812

913
## Usage
1014

1115
Version of this crate tracks NNG: `<NNG_version>-rc.<crate_version>` (e.g. `1.1.1-rc.2`).
1216

13-
To use the __latest crate__ for the most recent __stable version of NNG__ (1.1.1), in `Cargo.toml`:
17+
To use the __latest crate__ for the most recent __stable version of NNG__ (1.1.1), in `Cargo.toml`:
1418
```toml
15-
runng-sys = "1.1.1-rc"
19+
nng-sys = "1.1.1-rc"
1620
```
1721

1822
Requirements:
@@ -21,33 +25,31 @@ Requirements:
2125
- On Windows: default generator is [ninja](https://ninja-build.org/) and must also be in `PATH`
2226
- [libclang](https://rust-lang.github.io/rust-bindgen/requirements.html)
2327

24-
For a more ergonomic API to NNG see [runng](https://crates.io/crates/runng).
25-
2628
## Features
2729

30+
- `build-nng`: use cmake to build NNG from source (enabled by default)
2831
- `cmake-ninja`: use cmake generator "Ninja"
2932
- `cmake-vs2017`: use cmake generator "Visual Studio 15 2017"
3033
- `cmake-vs2017-win64`: use cmake generator "Visual Studio 15 2017 Win64"
3134
- `nng-stats`: enable NNG stats `NNG_ENABLE_STATS` (enabled by default)
32-
- `nng-tls`: enable TLS `NNG_ENABLE_TLS` (requires mbedTLS, disabled by default)
35+
- `nng-tls`: enable TLS `NNG_ENABLE_TLS` (requires mbedTLS)
3336

34-
For example, to disable stats and use Ninja cmake generator:
37+
For example, to disable stats and use Ninja cmake generator, in your `Cargo.toml`:
3538
```toml
36-
[dependencies.runng-sys]
39+
[dependencies.nng-sys]
3740
version = "1.1.1-rc"
3841
default-features = false
3942
features = ["cmake-ninja"]
4043
```
4144

4245
## Examples
4346
```rust
44-
use runng_sys::*;
47+
use nng_sys::*;
4548
use std::{ffi::CString, ptr::null_mut};
4649

47-
#[test]
4850
fn example() {
4951
unsafe {
50-
let url = CString::new("inproc://test").unwrap();
52+
let url = CString::new("inproc://nng_sys/tests/example").unwrap();
5153
let url = url.as_bytes_with_nul().as_ptr() as *const std::os::raw::c_char;
5254

5355
// Reply socket

build.rs

+56-48
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,59 @@ use cmake::Config;
22
use std::{env, path::PathBuf};
33

44
fn main() {
5+
if cfg!(feature = "build-nng") {
6+
let dst = cmake();
7+
// Check output of `cargo build --verbose`, should see something like:
8+
// -L native=/path/runng/target/debug/build/runng-sys-abc1234/out
9+
// That contains output from cmake
10+
println!(
11+
"cargo:rustc-link-search=native={}",
12+
dst.join("lib").display()
13+
);
14+
println!(
15+
"cargo:rustc-link-search=native={}",
16+
dst.join("lib64").display()
17+
);
18+
19+
// Tell rustc to use nng static library
20+
println!("cargo:rustc-link-lib=static=nng");
21+
} else {
22+
println!("cargo:rustc-link-lib=dylib=nng");
23+
}
24+
25+
// https://rust-lang-nursery.github.io/rust-bindgen
26+
// https://docs.rs/bindgen
27+
let mut builder = bindgen::Builder::default()
28+
// This is needed if use `#include <nng.h>` instead of `#include "path/nng.h"` in wrapper.h
29+
//.clang_arg("-Inng/src/")
30+
.header("wrapper.h");
31+
32+
if !cfg!(feature = "legacy-111-rc4") {
33+
builder = builder
34+
.whitelist_type("nng_.*")
35+
.whitelist_function("nng_.*")
36+
.whitelist_var("NNG_.*")
37+
// Generate `pub const NNG_UNIT_EVENTS` instead of `nng_unit_enum_NNG_UNIT_EVENTS`
38+
.prepend_enum_name(false)
39+
// Generate `pub enum ...` instead of multiple `pub const ...`
40+
.rustified_enum("nng_.*_enum")
41+
// Enum special cases:
42+
.rustified_enum("nng_pipe_ev")
43+
.rustified_enum("nng_sockaddr_family")
44+
.rustified_enum("nng_zt_status")
45+
// no_std support
46+
// https://rust-embedded.github.io/book/interoperability/c-with-rust.html#automatically-generating-the-interface
47+
.ctypes_prefix("cty")
48+
.use_core();
49+
}
50+
let bindings = builder.generate().expect("Unable to generate bindings");
51+
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
52+
bindings
53+
.write_to_file(out_path.join("bindings.rs"))
54+
.expect("Couldn't write bindings");
55+
}
56+
57+
fn cmake() -> PathBuf {
558
// Compile-time features
659
let generator = if cfg!(feature = "cmake-ninja") {
760
"Ninja"
@@ -10,6 +63,7 @@ fn main() {
1063
} else if cfg!(feature = "cmake-vs2017-win64") {
1164
"Visual Studio 15 2017 Win64"
1265
} else {
66+
// Default generators
1367
if cfg!(target_family = "unix") {
1468
"Unix Makefiles"
1569
} else {
@@ -28,58 +82,12 @@ fn main() {
2882
};
2983

3084
// Run cmake to build nng
31-
let dst = Config::new("nng")
85+
Config::new("nng")
3286
.generator(generator)
3387
.define("CMAKE_BUILD_TYPE", "Release")
3488
.define("NNG_TESTS", "OFF")
3589
.define("NNG_TOOLS", "OFF")
3690
.define("NNG_ENABLE_STATS", stats)
3791
.define("NNG_ENABLE_TLS", tls)
38-
.build();
39-
40-
// Check output of `cargo build --verbose`, should see something like:
41-
// -L native=/path/runng/target/debug/build/runng-sys-abc1234/out
42-
// That contains output from cmake
43-
println!(
44-
"cargo:rustc-link-search=native={}",
45-
dst.join("lib").display()
46-
);
47-
println!(
48-
"cargo:rustc-link-search=native={}",
49-
dst.join("lib64").display()
50-
);
51-
52-
// Tell rustc to use nng static library
53-
println!("cargo:rustc-link-lib=static=nng");
54-
55-
// https://rust-lang-nursery.github.io/rust-bindgen
56-
// https://docs.rs/bindgen
57-
let mut builder = bindgen::Builder::default()
58-
.header("wrapper.h")
59-
// This is needed if use `#include <nng.h>` instead of `#include "path/nng.h"`
60-
//.clang_arg("-Inng/src/")
61-
;
62-
if !cfg!(feature = "legacy-111-rc4") {
63-
builder = builder
64-
.whitelist_type("nng_.*")
65-
.whitelist_function("nng_.*")
66-
.whitelist_var("NNG_.*")
67-
// Generate `pub const NNG_UNIT_EVENTS` instead of `nng_unit_enum_NNG_UNIT_EVENTS`
68-
.prepend_enum_name(false)
69-
// Generate `pub enum ...` instead of multiple `pub const ...`
70-
.rustified_enum("nng_.*_enum")
71-
// Enum special cases:
72-
.rustified_enum("nng_pipe_ev")
73-
.rustified_enum("nng_sockaddr_family")
74-
.rustified_enum("nng_zt_status")
75-
// nostd support
76-
// https://rust-embedded.github.io/book/interoperability/c-with-rust.html#automatically-generating-the-interface
77-
.ctypes_prefix("cty")
78-
.use_core();
79-
}
80-
let bindings = builder.generate().expect("Unable to generate bindings");
81-
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
82-
bindings
83-
.write_to_file(out_path.join("bindings.rs"))
84-
.expect("Couldn't write bindings");
92+
.build()
8593
}

src/lib.rs

+48
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,51 @@
1+
/*!
2+
3+
## Examples
4+
5+
```rust
6+
use nng_sys::*;
7+
use std::{ffi::CString, ptr::null_mut};
8+
9+
fn 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 std::os::raw::c_char;
13+
14+
// Reply socket
15+
let mut rep_socket = nng_socket { id: 0 };
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 { id: 0 };
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+
149
// Suppress the flurry of warnings caused by using "C" naming conventions
250
#![allow(non_upper_case_globals)]
351
#![allow(non_camel_case_types)]

tests/tests.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#[cfg(test)]
22
mod tests {
33

4-
use runng_sys::*;
4+
use nng_sys::*;
55
use std::{ffi::CString, ptr::null_mut};
66

7-
#[test]
87
fn example() {
98
unsafe {
10-
let url = CString::new("inproc://runng-sys/tests/example").unwrap();
9+
let url = CString::new("inproc://nng_sys/tests/example").unwrap();
1110
let url = url.as_bytes_with_nul().as_ptr() as *const std::os::raw::c_char;
1211

1312
// Reply socket
@@ -46,7 +45,7 @@ mod tests {
4645
#[test]
4746
fn basic() {
4847
unsafe {
49-
let url = CString::new("inproc://runng-sys/tests/basic").unwrap();
48+
let url = CString::new("inproc://nng_sys/tests/basic").unwrap();
5049
let url = url.as_bytes_with_nul().as_ptr() as *const std::os::raw::c_char;
5150

5251
// Reply socket

0 commit comments

Comments
 (0)