dialectic_null/
lib.rs

1//! A "null" backend implementation for the [`dialectic`] crate which can only send and receive the unit type `()`.
2//!
3//! This backend is useful primarily only for benchmarking, as it does the absolute minimum amount
4//! of work, so that it is easier to isolate performance issues in Dialectic itself. You cannot
5//! implement most protocols using this backend, as it is limited to transporting the unit type `()`
6//! and cannot [`choose`](dialectic::Chan::choose) or [`offer!`](dialectic::offer) more than a
7//! single choice.
8
9#![allow(clippy::type_complexity)]
10#![warn(missing_docs)]
11#![warn(missing_copy_implementations, missing_debug_implementations)]
12#![warn(unused_qualifications, unused_results)]
13#![warn(future_incompatible)]
14#![warn(unused)]
15// Documentation configuration
16#![forbid(broken_intra_doc_links)]
17
18use dialectic::backend::{self, By, Choice, Mut, Ref, Val};
19use std::{convert::TryInto, future::Future, pin::Pin};
20
21/// Shorthand for a [`Chan`](dialectic::Chan) using a null [`Sender`] and [`Receiver`].
22///
23/// # Examples
24///
25/// ```
26/// use dialectic::prelude::*;
27/// use dialectic::types::Done;
28/// use dialectic_null as null;
29///
30/// let _: (null::Chan<Done>, null::Chan<Done>) =
31///     Done::channel(null::channel);
32/// ```
33pub type Chan<P> = dialectic::Chan<P, Sender, Receiver>;
34
35/// A receiver only capable of receiving the unit type `()`.
36#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
37pub struct Receiver {
38    _private: (),
39}
40
41/// A sender only capable of sending the unit type `()`.
42#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
43pub struct Sender {
44    _private: (),
45}
46
47/// Create a channel for transporting unit values `()` only.
48///
49/// # Examples
50///
51/// ```
52/// let (tx, rx) = dialectic_null::channel();
53/// ```
54pub fn channel() -> (Sender, Receiver) {
55    (Sender::default(), Receiver::default())
56}
57
58/// An error thrown while receiving from or sending to a null channel.
59///
60/// No such errors are possible, so this type cannot be constructed.
61#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
62pub struct Error {
63    _private: (),
64}
65
66impl std::fmt::Display for Error {
67    fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        Ok(())
69    }
70}
71
72impl std::error::Error for Error {}
73
74impl backend::Transmitter for Sender {
75    type Error = Error;
76
77    fn send_choice<'async_lifetime, const LENGTH: usize>(
78        &'async_lifetime mut self,
79        _choice: Choice<LENGTH>,
80    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_lifetime>> {
81        Box::pin(async { Ok(()) })
82    }
83}
84
85impl backend::Transmit<(), Val> for Sender {
86    fn send<'a, 'async_lifetime>(
87        &'async_lifetime mut self,
88        _message: <() as By<'a, Val>>::Type,
89    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_lifetime>>
90    where
91        (): By<'a, Val>,
92        'a: 'async_lifetime,
93    {
94        Box::pin(async { Ok(()) })
95    }
96}
97
98impl backend::Transmit<(), Ref> for Sender {
99    fn send<'a, 'async_lifetime>(
100        &'async_lifetime mut self,
101        _message: <() as By<'a, Ref>>::Type,
102    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_lifetime>>
103    where
104        (): By<'a, Ref>,
105        'a: 'async_lifetime,
106    {
107        Box::pin(async { Ok(()) })
108    }
109}
110
111impl backend::Transmit<(), Mut> for Sender {
112    fn send<'a, 'async_lifetime>(
113        &'async_lifetime mut self,
114        _message: <() as By<'a, Mut>>::Type,
115    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_lifetime>>
116    where
117        (): By<'a, Mut>,
118        'a: 'async_lifetime,
119    {
120        Box::pin(async { Ok(()) })
121    }
122}
123
124impl<const LENGTH: usize> backend::Transmit<Choice<LENGTH>> for Sender {
125    fn send<'a, 'async_lifetime>(
126        &'async_lifetime mut self,
127        _message: <Choice<LENGTH> as By<Val>>::Type,
128    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_lifetime>>
129    where
130        'a: 'async_lifetime,
131    {
132        Box::pin(async { Ok(()) })
133    }
134}
135
136impl backend::Receiver for Receiver {
137    type Error = Error;
138
139    fn recv_choice<'async_lifetime, const LENGTH: usize>(
140        &'async_lifetime mut self,
141    ) -> Pin<Box<dyn Future<Output = Result<Choice<LENGTH>, Self::Error>> + Send + 'async_lifetime>>
142    {
143        Box::pin(async { 0.try_into().map_err(|_| Error { _private: () }) })
144    }
145}
146
147impl backend::Receive<()> for Receiver {
148    fn recv<'async_lifetime>(
149        &'async_lifetime mut self,
150    ) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_lifetime>> {
151        Box::pin(async { Ok(()) })
152    }
153}
154
155impl<const LENGTH: usize> backend::Receive<Choice<LENGTH>> for Receiver {
156    fn recv<'async_lifetime>(
157        &'async_lifetime mut self,
158    ) -> Pin<Box<dyn Future<Output = Result<Choice<LENGTH>, Self::Error>> + Send + 'async_lifetime>>
159    {
160        Box::pin(async { 0.try_into().map_err(|_| Error { _private: () }) })
161    }
162}