Skip to content

Adds support for creating a Context from a non-owned pointer #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ edition = "2018"
maintenance = { status = "actively-maintained" }

[features]
default = ["zmq_has"]
default = ["zmq_has", "static"]
# zmq_has was added in zeromq 4.1. As we now require 4.1 or newer,
# this feature is a no-op and only present for backward-compatibility;
# it will be removed in the next API-breaking release.
zmq_has = []
static = ["zmq-sys/static"]

[dependencies]
bitflags = "1.0"
libc = "0.2.15"
zmq-sys = { version = "0.12.0", path = "zmq-sys" }
zmq-sys = { version = "0.12.0", path = "zmq-sys", default-features = false }

[dev-dependencies]
trybuild = { version = "1" }
Expand Down
17 changes: 14 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ pub fn version() -> (i32, i32, i32) {

struct RawContext {
ctx: *mut c_void,
owned: bool,
}

impl RawContext {
Expand All @@ -390,9 +391,11 @@ unsafe impl Sync for RawContext {}

impl Drop for RawContext {
fn drop(&mut self) {
let mut e = self.term();
while e == Err(Error::EINTR) {
e = self.term();
if self.owned {
let mut e = self.term();
while e == Err(Error::EINTR) {
e = self.term();
}
}
}
}
Expand Down Expand Up @@ -428,10 +431,18 @@ impl Context {
Context {
raw: Arc::new(RawContext {
ctx: unsafe { zmq_sys::zmq_ctx_new() },
owned: true,
}),
}
}

/// Create a Context from a raw context pointer.
pub unsafe fn from_raw(ctx: *mut c_void) -> Context {
Context {
raw: Arc::new(RawContext { ctx, owned: false }),
}
}

/// Get the size of the ØMQ thread pool to handle I/O operations.
pub fn get_io_threads(&self) -> Result<i32> {
let rc =
Expand Down
4 changes: 3 additions & 1 deletion zmq-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ build = "build/main.rs"
links = "zmq"

[features]
default = ["static"]
static = ["dep:zeromq-src"]

[dependencies]
libc = "0.2.15"

[build-dependencies]
system-deps = "6"
zeromq-src = { version = "0.2.1" }
zeromq-src = { version = "0.2.1", optional = true }

[package.metadata.system-deps]
libzmq = "4.1"
2 changes: 2 additions & 0 deletions zmq-sys/build/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "static")]
pub fn configure() {
println!("cargo:rerun-if-changed=build/main.rs");
println!("cargo:rerun-if-env-changed=PROFILE");
Expand All @@ -12,5 +13,6 @@ pub fn configure() {
}

fn main() {
#[cfg(feature = "static")]
configure()
}