diff options
| author | Botahamec <botahamec@outlook.com> | 2022-10-26 22:57:54 -0400 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2022-10-26 22:57:54 -0400 |
| commit | 9a014a0afa5dd1536ed1cc5f3cdb71304385b471 (patch) | |
| tree | 07d57411cf3b6b3053e106cdff93b4788699d8b0 /src/lib.rs | |
| parent | a3ff79eb391843b6a6eb144992bd6782bf080019 (diff) | |
Add a drop implementation
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 21 |
1 files changed, 15 insertions, 6 deletions
@@ -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) } } |
