Use updated Service trait (#450)

This commit is contained in:
Nikolay Kim 2024-11-02 17:44:35 +05:00 committed by GitHub
parent 011e9cdfea
commit 8288fc0364
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 243 additions and 82 deletions

View file

@ -26,13 +26,17 @@ impl Counter {
}
/// Get counter guard.
pub fn get(&self) -> CounterGuard {
pub(crate) fn get(&self) -> CounterGuard {
CounterGuard::new(self.0.clone())
}
pub(crate) fn is_available(&self) -> bool {
self.0.count.get() < self.0.capacity
}
/// Check if counter is not at capacity. If counter at capacity
/// it registers notification for current task.
pub async fn available(&self) {
pub(crate) async fn available(&self) {
poll_fn(|cx| {
if self.poll_available(cx) {
task::Poll::Ready(())
@ -43,15 +47,21 @@ impl Counter {
.await
}
/// Check if counter is not at capacity. If counter at capacity
/// it registers notification for current task.
pub fn poll_available(&self, cx: &mut task::Context<'_>) -> bool {
self.0.available(cx)
pub(crate) async fn unavailable(&self) {
poll_fn(|cx| {
if self.poll_available(cx) {
task::Poll::Pending
} else {
task::Poll::Ready(())
}
})
.await
}
/// Get total number of acquired counts
pub fn total(&self) -> usize {
self.0.count.get()
/// Check if counter is not at capacity. If counter at capacity
/// it registers notification for current task.
fn poll_available(&self, cx: &mut task::Context<'_>) -> bool {
self.0.available(cx)
}
}
@ -75,7 +85,11 @@ impl Drop for CounterGuard {
impl CounterInner {
fn inc(&self) {
self.count.set(self.count.get() + 1);
let num = self.count.get() + 1;
self.count.set(num);
if num == self.capacity {
self.task.wake();
}
}
fn dec(&self) {
@ -87,10 +101,10 @@ impl CounterInner {
}
fn available(&self, cx: &mut task::Context<'_>) -> bool {
self.task.register(cx.waker());
if self.count.get() < self.capacity {
true
} else {
self.task.register(cx.waker());
false
}
}