Skip to content

Commit 0c0f433

Browse files
committed
Expose client capacity; expand docs
1 parent f6ee324 commit 0c0f433

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,23 @@ impl From<u8> for State {
152152

153153

154154
/// Do NOT implement this yourself! Use the macro `interchange!`.
155+
///
156+
/// At compile time, the client capacity is set by using the appropriate call
157+
/// to the `interchange!` macro. The application can then repeatedly call `claim`
158+
/// to obtain these clients.
155159
pub trait Interchange: Sized {
160+
const CLIENT_CAPACITY: usize;
156161
type REQUEST: Clone;
157162
type RESPONSE: Clone;
158163
/// This is the constructor for a `(Requester, Responder)` pair.
159164
///
160165
/// Returns singleton static instances until all that were allocated are
161166
/// used up, thereafter, `None` is returned.
162167
fn claim() -> Option<(Requester<Self>, Responder<Self>)>;
168+
169+
/// Method for debugging: how many allocated clients have not been claimed.
170+
fn available_clients() -> usize;
171+
163172
/// Method purely for testing - do not use in production
164173
///
165174
/// Rationale: In production, interchanges are supposed to be set up

src/macros.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ macro_rules! interchange {
8181
static LAST_CLAIMED: AtomicUsize = AtomicUsize::new(0);
8282
&LAST_CLAIMED
8383
}
84+
8485
}
8586

8687
impl $crate::Interchange for $Name {
88+
const CLIENT_CAPACITY: usize = $N;
89+
8790
type REQUEST = $REQUEST;
8891
type RESPONSE = $RESPONSE;
8992

@@ -107,6 +110,10 @@ macro_rules! interchange {
107110
}
108111
}
109112

113+
fn available_clients() -> usize {
114+
Self::CLIENT_CAPACITY - Self::last_claimed().load(core::sync::atomic::Ordering::SeqCst)
115+
}
116+
110117
unsafe fn rq(self) -> Self::REQUEST {
111118
match self {
112119
Self::Request(request) => {

0 commit comments

Comments
 (0)