neon-bindings / examples

A collection of examples of Neon
324 stars 43 forks source link

Async SQLite example: Implement Drop trait for Database struct #75

Closed c12i closed 3 years ago

c12i commented 3 years ago

This should send a close DbMessage to stop the worker thread.

kjvalencik commented 3 years ago

Implementing drop is unnecessary. When the Database struct is dropped, the sender will close. When there are no more senders, the receiving end of a channel returns an error. This is handled in the loop and will also stop the thread.

https://doc.rust-lang.org/std/sync/mpsc/struct.Receiver.html#method.recv https://github.com/neon-bindings/examples/blob/42564aa37d4d12d98e4f754bd263d71507d89e42/examples/async-sqlite/src/lib.rs#L70

The close method is only provided as a way to terminate the thread without waiting for the garbage collector to drop the boxed Database. In most cases, calling close is optional. However, in this example, it makes the unit test exit sooner.