Skip to content

Commit

Permalink
Merge pull request #21 from Mark-Simulacrum/drop-no-release
Browse files Browse the repository at this point in the history
Drop tokens without releasing them
  • Loading branch information
alexcrichton authored Jan 17, 2020
2 parents 89ed27c + f239e4d commit e133af5
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 e133af5

Please sign in to comment.