use core::{
fmt::{self, Formatter, Debug},
ops::DerefMut,
cell::RefCell,
marker::PhantomData,
};
use alloc::{
boxed::Box,
sync::Arc,
rc::Rc,
};
#[cfg(feature = "std")]
use std::{
sync::{Mutex, RwLock},
};
use super::Entry;
pub trait Receiver<E>
where E: Entry {
fn receive(&mut self, new_value: &E::Data);
}
#[allow(clippy::module_name_repetitions)]
pub struct FnReceiver<E, F = Box<dyn FnMut(&<E as Entry>::Data)>>
where
E: Entry,
F: FnMut(&E::Data) {
_phantom: PhantomData<E>,
pub closure: F,
}
impl<E, F> FnReceiver<E, F>
where
E: Entry,
F: FnMut(&E::Data) {
#[inline(always)]
pub fn new(closure: F) -> Self {
Self {closure, _phantom: PhantomData}
}
}
impl<E, F> Receiver<E> for FnReceiver<E, F>
where
E: Entry,
F: FnMut(&E::Data) {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
(self.closure)(new_value)
}
}
impl<E, F> Receiver<E> for &FnReceiver<E, F>
where
E: Entry,
F: Fn(&E::Data) {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
(self.closure)(new_value)
}
}
impl<E, F> Clone for FnReceiver<E, F>
where
E: Entry,
F: Fn(&E::Data) + Clone {
#[inline(always)]
fn clone(&self) -> Self {
Self {closure: self.closure.clone(), _phantom: PhantomData}
}
}
impl<E, F> Copy for FnReceiver<E, F>
where
E: Entry,
F: Fn(&E::Data) + Copy {}
impl<E, F> Default for FnReceiver<E, F>
where
E: Entry,
F: Fn(&E::Data) + Default {
#[inline(always)]
fn default() -> Self {
Self {closure: F::default(), _phantom: PhantomData}
}
}
impl<E, F> Debug for FnReceiver<E, F>
where
E: Entry,
F: Fn(&E::Data) + Debug {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("FnReceiver")
.field("closure", &self.closure)
.finish()
}
}
#[allow(clippy::module_name_repetitions)]
pub struct IterReceiver<E, I>
where
E: Entry,
for<'a> &'a mut I: IntoIterator,
for<'a> <&'a mut I as IntoIterator>::Item: Receiver<E> {
pub iter: I,
_phantom: PhantomData<E>,
}
impl<E, I> Receiver<E> for IterReceiver<E, I>
where
E: Entry,
for<'a> &'a mut I: IntoIterator,
for<'a> <&'a mut I as IntoIterator>::Item: Receiver<E> {
#[inline]
fn receive(&mut self, new_value: &E::Data) {
for mut receiver in &mut self.iter {
receiver.receive(new_value);
}
}
}
impl<E, I> Receiver<E> for &IterReceiver<E, I>
where
E: Entry,
for<'a> &'a mut I: IntoIterator,
for<'a> &'a I: IntoIterator,
for<'a> <&'a mut I as IntoIterator>::Item: Receiver<E>,
for<'a> <&'a I as IntoIterator>::Item: Receiver<E> {
#[inline]
fn receive(&mut self, new_value: &E::Data) {
for mut receiver in &self.iter {
receiver.receive(new_value);
}
}
}
impl<E, I> IterReceiver<E, I>
where
E: Entry,
for<'a> &'a mut I: IntoIterator,
for<'a> <&'a mut I as IntoIterator>::Item: Receiver<E> {
#[inline(always)]
pub fn new(iter: I) -> Self {
Self {iter, _phantom: PhantomData}
}
}
impl<E, I> Clone for IterReceiver<E, I>
where
E: Entry,
I: Clone,
for<'a> &'a mut I: IntoIterator,
for<'a> <&'a mut I as IntoIterator>::Item: Receiver<E> {
#[inline(always)]
fn clone(&self) -> Self {
Self {iter: self.iter.clone(), _phantom: PhantomData}
}
}
impl<E, I> Copy for IterReceiver<E, I>
where
E: Entry,
I: Copy,
for<'a> &'a mut I: IntoIterator,
for<'a> <&'a mut I as IntoIterator>::Item: Receiver<E> {}
impl<E, I> Default for IterReceiver<E, I>
where
E: Entry,
I: Default,
for<'a> &'a mut I: IntoIterator,
for<'a> <&'a mut I as IntoIterator>::Item: Receiver<E> {
#[inline(always)]
fn default() -> Self {
Self {iter: I::default(), _phantom: PhantomData}
}
}
impl<E, I> Debug for IterReceiver<E, I>
where
E: Entry,
I: Debug,
for<'a> &'a mut I: IntoIterator,
for<'a> <&'a mut I as IntoIterator>::Item: Receiver<E> {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("IterReceiver")
.field("iter", &self.iter)
.finish()
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct EmptyReceiver;
impl EmptyReceiver {
#[inline(always)]
pub const fn new() -> Self { EmptyReceiver }
}
impl<E> Receiver<E> for EmptyReceiver
where E: Entry {
#[inline(always)]
fn receive(&mut self, _: &E::Data) {}
}
impl<E> Receiver<E> for &EmptyReceiver
where E: Entry {
#[inline(always)]
fn receive(&mut self, _: &E::Data) {}
}
impl<E, R> Receiver<E> for &mut R
where
E: Entry,
R: Receiver<E> + ?Sized {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
(*self).receive(new_value);
}
}
impl<E, R> Receiver<E> for Option<R>
where
E: Entry,
R: Receiver<E> {
#[inline]
fn receive(&mut self, new_value: &E::Data) {
if let Some(receiver) = self.as_mut() {
receiver.receive(new_value);
}
}
}
impl<E, R> Receiver<E> for &Option<R>
where
E: Entry,
for<'a> &'a R: Receiver<E> {
#[inline]
fn receive(&mut self, new_value: &E::Data) {
if let Some(mut receiver) = self.as_ref() {
receiver.receive(new_value);
}
}
}
impl<E, R> Receiver<E> for Box<R>
where
E: Entry,
R: Receiver<E> + ?Sized {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
self.deref_mut().receive(new_value);
}
}
impl<E, R> Receiver<E> for &Box<R>
where
E: Entry,
R: ?Sized,
for<'a> &'a R: Receiver<E> {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
(&***self).receive(new_value);
}
}
impl<E, R> Receiver<E> for Rc<R>
where
E: Entry,
R: ?Sized,
for<'a> &'a R: Receiver<E> {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
(&**self).receive(new_value);
}
}
impl<E, R> Receiver<E> for Arc<R>
where
E: Entry,
R: ?Sized,
for<'a> &'a R: Receiver<E> {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
(&**self).receive(new_value);
}
}
impl<E, R> Receiver<E> for RefCell<R>
where
E: Entry,
R: Receiver<E> + ?Sized {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
self.get_mut().receive(new_value);
}
}
impl<E, R> Receiver<E> for &RefCell<R>
where
E: Entry,
R: Receiver<E> + ?Sized {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
(*self.borrow_mut()).receive(new_value);
}
}
#[cfg(feature = "std")]
static POISONING_MSG: &str = "attempt to use a poisoned lock as a receiver";
#[cfg(feature = "std")]
impl<E, R> Receiver<E> for Mutex<R>
where
E: Entry,
R: Receiver<E> + ?Sized {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
self.get_mut().expect(POISONING_MSG).receive(new_value);
}
}
#[cfg(feature = "std")]
impl<E, R> Receiver<E> for &Mutex<R>
where
E: Entry,
R: Receiver<E> + ?Sized {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
self.lock().expect(POISONING_MSG).receive(new_value);
}
}
#[cfg(feature = "std")]
impl<E, R> Receiver<E> for RwLock<R>
where
E: Entry,
R: Receiver<E> + ?Sized {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
self.get_mut().expect(POISONING_MSG).receive(new_value);
}
}
#[cfg(feature = "std")]
impl<E, R> Receiver<E> for &RwLock<R>
where
E: Entry,
R: Receiver<E> + ?Sized {
#[inline(always)]
fn receive(&mut self, new_value: &E::Data) {
self.write().expect(POISONING_MSG).receive(new_value);
}
}