1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::*;
use log::trace;
use runng_sys::*;
pub trait Ctx {
fn ctx(&self) -> nng_ctx;
}
#[derive(Debug)]
pub struct NngCtx {
ctx: nng_ctx,
}
impl NngCtx {
pub fn new(socket: NngSocket) -> Result<Self> {
let mut ctx = nng_ctx::default();
let res = unsafe { nng_ctx_open(&mut ctx, socket.nng_socket()) };
nng_int_to_result(res)?;
let ctx = Self { ctx };
Ok(ctx)
}
pub fn id(&self) -> i32 {
unsafe { nng_ctx_id(self.ctx) }
}
}
impl Ctx for NngCtx {
fn ctx(&self) -> nng_ctx {
self.ctx
}
}
impl Drop for NngCtx {
fn drop(&mut self) {
unsafe {
let id = self.id();
if id > 0 {
trace!("NngCtx.drop {:x}", id);
nng_ctx_close(self.ctx);
}
}
}
}