use super::*;
pub trait SingleCandidateHandler<OverloadId>: Send + Sync + 'static {
type Output;
fn open_once(self, candidate: StreamCandidate) -> Self::Output;
}
pub trait CandidateHandler<OverloadId>: SingleCandidateHandler<OverloadId> {
fn open(&self, candidate: StreamCandidate) -> Self::Output;
}
pub struct WriteOnly;
impl<OverloadId, Handler, F: FnOnce() -> Handler + Send + Sync + 'static>
SingleCandidateHandler<OverloadId> for F
where
Handler: IntoChannel<OverloadId, Inbound>,
Handler::Sender: Channel<OverloadId, Inbound>,
{
type Output = Stream<Handler::Receiver>;
fn open_once(self, candidate: StreamCandidate) -> Self::Output {
candidate.open(self())
}
}
impl<OverloadId, Handler, F: Fn() -> Handler + Send + Sync + 'static> CandidateHandler<OverloadId>
for F
where
Handler: IntoChannel<OverloadId, Inbound>,
Handler::Sender: Channel<OverloadId, Inbound>,
{
fn open(&self, candidate: StreamCandidate) -> Self::Output {
candidate.open(self())
}
}
impl SingleCandidateHandler<()> for WriteOnly {
type Output = Stream<()>;
fn open_once(self, candidate: StreamCandidate) -> Self::Output {
candidate.open_write_only()
}
}
impl CandidateHandler<()> for WriteOnly {
fn open(&self, candidate: StreamCandidate) -> Self::Output {
candidate.open_write_only()
}
}
impl SingleCandidateHandler<()> for () {
type Output = StreamCandidate;
fn open_once(self, candidate: StreamCandidate) -> Self::Output {
candidate
}
}
impl CandidateHandler<()> for () {
fn open(&self, candidate: StreamCandidate) -> Self::Output {
candidate
}
}
#[cfg(test)]
mod tests {
use super::*;
fn candidate_handlers() {
fn assert_single_handler<H: SingleCandidateHandler<Oid>, Oid>(value: H) {}
fn assert_handler<H: CandidateHandler<Oid>, Oid>(value: H) {
assert_single_handler(value);
}
assert_handler(crate::experimental::bus::WriteOnly);
assert_handler(());
assert_handler(|| std::sync::mpsc::channel());
assert_handler(|| tokio::sync::mpsc::unbounded_channel());
assert_handler(|| |candidate: Inbound| ());
}
}