summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2022-10-26 22:57:54 -0400
committerBotahamec <botahamec@outlook.com>2022-10-26 22:57:54 -0400
commit9a014a0afa5dd1536ed1cc5f3cdb71304385b471 (patch)
tree07d57411cf3b6b3053e106cdff93b4788699d8b0 /src/lib.rs
parenta3ff79eb391843b6a6eb144992bd6782bf080019 (diff)
Add a drop implementation
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 614820f..0922f35 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -22,6 +22,18 @@ impl Debug for ThreadKey {
}
}
+impl Drop for ThreadKey {
+ fn drop(&mut self) {
+ KEY.with(|thread_lock| {
+ let mut key = thread_lock.lock();
+
+ // safety: this ThreadKey is about to be destroyed, so there'll
+ // still only be one ThreadKey
+ *key = Some(unsafe { Self::new() });
+ })
+ }
+}
+
impl ThreadKey {
/// Create a new `ThreadKey`.
///
@@ -38,7 +50,7 @@ impl ThreadKey {
///
/// The first time this is called, it will successfully return a
/// `ThreadKey`. However, future calls to this function will return
- /// [`None`], unless the key is unlocked first.
+ /// [`None`], unless the key is dropped or unlocked first.
pub fn lock() -> Option<Self> {
KEY.with(|thread_lock| thread_lock.lock().take())
}
@@ -47,10 +59,7 @@ impl ThreadKey {
///
/// After this method is called, a call to [`ThreadKey::lock`] will return
/// this `ThreadKey`.
- pub fn unlock(lock: ThreadKey) {
- KEY.with(|thread_lock| {
- let mut thread_lock = thread_lock.lock();
- *thread_lock = Some(lock);
- })
+ pub fn unlock(key: ThreadKey) {
+ drop(key)
}
}