Skip to content

Commit 2b1d109

Browse files
committed
mctp-estack: Add WakeOnDrop
Helper to implement async locks Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
1 parent b134e14 commit 2b1d109

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

mctp-estack/src/router.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
#[allow(unused)]
99
use crate::fmt::{debug, error, info, trace, warn};
10+
use crate::util::WakeOnDrop;
1011

1112
use core::cell::RefCell;
1213
use core::debug_assert;

mctp-estack/src/util.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use core::ops::{Deref, DerefMut};
2+
use core::task::Waker;
3+
14
/// Takes a `usize` from a build-time environment variable.
25
///
36
/// If unset, the default is used. Can be used in a const context.
@@ -114,6 +117,42 @@ impl VectorReader {
114117
#[derive(Debug)]
115118
pub struct VectorReaderError;
116119

120+
// TODO: Use DropGuard instead once it's stable.
121+
// That can wake _after_ T::drop()
122+
pub struct WakeOnDrop<T> {
123+
waker: Waker,
124+
value: T,
125+
}
126+
127+
impl<T> WakeOnDrop<T> {
128+
pub fn new(value: T, waker: &Waker) -> Self {
129+
Self {
130+
value,
131+
waker: waker.clone(),
132+
}
133+
}
134+
}
135+
136+
impl<T> Drop for WakeOnDrop<T> {
137+
fn drop(&mut self) {
138+
self.waker.wake_by_ref();
139+
}
140+
}
141+
142+
impl<T> Deref for WakeOnDrop<T> {
143+
type Target = T;
144+
145+
fn deref(&self) -> &T {
146+
&self.value
147+
}
148+
}
149+
150+
impl<T> DerefMut for WakeOnDrop<T> {
151+
fn deref_mut(&mut self) -> &mut T {
152+
&mut self.value
153+
}
154+
}
155+
117156
#[cfg(test)]
118157
mod tests {
119158
#[test]

0 commit comments

Comments
 (0)