Skip to content

Commit 1900fda

Browse files
committed
s: Fix lint errors
If we like the deferred pattern and keep it we may want to align with axevent where it is implemented like below: ```rust struct Deferred { func: Option<Box<dyn FnOnce()>>, } impl Drop for Deferred { fn drop(&mut self) { let func = self.func.take().unwrap(); func(); } } impl Deferred { pub fn new<F>(func: F) -> Self where F: FnOnce() + 'static, { Self { func: Some(Box::new(func)), } } } ``` But I think I forgot to reason about the safety because now that I look at it I think we need to take care that F is Send. Another implementation that I experimented with is storing the callback as `Option<Box<dyn Any>>` and using `ptr::addr_of_mut!` to get a pointer for the FFI call. This feels nice because there is less indirection but: - It still doesn't capture the Send requirement. - I'm not sure if moving the Box makes it ub[^1] [^1]: rust-lang/rust#129090
1 parent e2cec25 commit 1900fda

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

crates/mdb/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ macro_rules! suppress_unwind {
2222

2323
pub struct Connection {
2424
ptr: *mut mdb_sys::mdb_connection_t,
25-
drop_on_error: Option<Deferred>,
25+
_on_error: Option<Deferred>,
2626
}
2727

2828
impl Connection {
@@ -52,7 +52,7 @@ impl Connection {
5252
Some(on_error) => Box::into_raw(Box::new(on_error)),
5353
};
5454
let drop_on_error = match on_error.is_null() {
55-
false => Some(Deferred::drop_box(on_error)),
55+
false => Some(Deferred::new(on_error)),
5656
true => None,
5757
};
5858
let ptr = mdb_sys::mdb_connection_create(
@@ -64,7 +64,10 @@ impl Connection {
6464
(false, false) => {
6565
panic!("mdb_connection_create returned both a connection and an error");
6666
}
67-
(false, true) => Ok(Self { ptr, drop_on_error }),
67+
(false, true) => Ok(Self {
68+
ptr,
69+
_on_error: drop_on_error,
70+
}),
6871
(true, false) => Err(OwnedError::new(error)),
6972
(true, true) => {
7073
panic!("mdb_connection_create returned neither a connection nor an error");
@@ -110,14 +113,14 @@ impl Drop for Deferred {
110113
}
111114

112115
impl Deferred {
113-
unsafe fn drop_box<T: 'static>(ptr: *mut T) -> Self {
116+
unsafe fn new<T: 'static>(ptr: *mut T) -> Self {
114117
Self(Some(Box::new(move || drop(Box::from_raw(ptr)))))
115118
}
116119
}
117120

118121
pub struct SubscriberConfig {
119122
ptr: *mut mdb_sys::mdb_subscriber_config_t,
120-
drop_on_message: Deferred,
123+
_on_message: Deferred,
121124
}
122125

123126
impl SubscriberConfig {
@@ -128,7 +131,7 @@ impl SubscriberConfig {
128131
debug!("Creating {}...", any::type_name::<Self>());
129132
unsafe {
130133
let on_message = Box::into_raw(Box::new(on_message));
131-
let drop_on_message = Deferred::drop_box(on_message);
134+
let drop_on_message = Deferred::new(on_message);
132135

133136
let mut error: *mut mdb_sys::mdb_error_t = std::ptr::null_mut();
134137
let ptr = mdb_sys::mdb_subscriber_config_create(
@@ -144,7 +147,7 @@ impl SubscriberConfig {
144147
}
145148
(false, true) => Ok(Self {
146149
ptr,
147-
drop_on_message,
150+
_on_message: drop_on_message,
148151
}),
149152
(true, false) => Err(OwnedError::new(error)),
150153
(true, true) => {
@@ -187,7 +190,7 @@ impl Drop for SubscriberConfig {
187190

188191
pub struct Subscriber<'a> {
189192
ptr: *mut mdb_sys::mdb_subscriber_t,
190-
drop_on_done: Deferred,
193+
_on_done: Deferred,
191194
_config: SubscriberConfig,
192195
_marker: PhantomData<&'a Connection>,
193196
}
@@ -204,7 +207,7 @@ impl<'a> Subscriber<'a> {
204207
debug!("Creating {}...", any::type_name::<Self>());
205208
unsafe {
206209
let on_done = Box::into_raw(Box::new(on_done));
207-
let drop_on_done = Deferred::drop_box(on_done);
210+
let drop_on_done = Deferred::new(on_done);
208211
let mut error: *mut mdb_sys::mdb_error_t = std::ptr::null_mut();
209212
let ptr = mdb_sys::mdb_subscriber_create_async(
210213
connection.ptr,
@@ -219,7 +222,7 @@ impl<'a> Subscriber<'a> {
219222
}
220223
(false, true) => Ok(Self {
221224
ptr,
222-
drop_on_done,
225+
_on_done: drop_on_done,
223226
_config: config,
224227
_marker: PhantomData,
225228
}),

0 commit comments

Comments
 (0)