diff --git a/Cargo.toml b/Cargo.toml index 02e6c23c..8852e66b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/src/lib.rs b/src/lib.rs index 17dc0ccd..cd7f8930 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -376,6 +376,7 @@ pub fn version() -> (i32, i32, i32) { struct RawContext { ctx: *mut c_void, + owned: bool, } impl RawContext { @@ -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(); + } } } } @@ -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 { let rc = diff --git a/zmq-sys/Cargo.toml b/zmq-sys/Cargo.toml index 14507b86..0217c6e8 100644 --- a/zmq-sys/Cargo.toml +++ b/zmq-sys/Cargo.toml @@ -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" diff --git a/zmq-sys/build/main.rs b/zmq-sys/build/main.rs index d6c229bc..3f2c1da2 100644 --- a/zmq-sys/build/main.rs +++ b/zmq-sys/build/main.rs @@ -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"); @@ -12,5 +13,6 @@ pub fn configure() { } fn main() { + #[cfg(feature = "static")] configure() }