Skip to content

Commit

Permalink
Drop tokens without releasing them
Browse files Browse the repository at this point in the history
This is useful when dealing with the helper thread interface but wanting to
release the tokens via release_raw at a later point.
  • Loading branch information
Mark-Simulacrum committed Jan 17, 2020
1 parent 89ed27c commit f239e4d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ pub struct Client {
pub struct Acquired {
client: Arc<imp::Client>,
data: imp::Acquired,
disabled: bool,
}

impl Acquired {
/// This drops the `Acquired` token without releasing the associated token.
///
/// This is not generally useful, but can be helpful if you do not have the
/// ability to store an Acquired token but need to not yet release it.
pub fn drop_without_releasing(mut self) {
self.disabled = true;
}
}

#[derive(Default, Debug)]
Expand Down Expand Up @@ -245,6 +256,7 @@ impl Client {
Ok(Acquired {
client: self.inner.clone(),
data: data,
disabled: false,
})
}

Expand Down Expand Up @@ -386,7 +398,9 @@ impl Client {

impl Drop for Acquired {
fn drop(&mut self) {
drop(self.client.release(Some(&self.data)));
if !self.disabled {
drop(self.client.release(Some(&self.data)));
}
}
}

Expand Down Expand Up @@ -1002,6 +1016,7 @@ mod imp {
WAIT_OBJECT_1 => f(Ok(::Acquired {
client: client.inner.clone(),
data: Acquired,
disabled: false,
})),
_ => f(Err(io::Error::last_os_error())),
}
Expand Down

0 comments on commit f239e4d

Please sign in to comment.