1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::rw_lock_cycler::RwLockCycler;
use crate::traits::*;
use parking_lot::RwLockWriteGuard;

#[cfg(feature = "unsafe_cleanup")]
use crate::static_ref_holder::StaticRefHolder;
#[cfg(feature = "unsafe_cleanup")]
use std::sync::Arc;

/// The writer to a data distributor
#[derive(Debug)]
pub struct RwLockCyclerWriter<T>
where
    T: 'static,
{
    pub(super) cycler: &'static RwLockCycler<T>,
    pub(super) writer: RwLockWriteGuard<'static, T>,
    pub(super) currently_writing: u8,
    #[allow(dead_code)]
    #[cfg(feature = "unsafe_cleanup")]
    pub(super) ref_holder: Arc<StaticRefHolder<RwLockCycler<T>>>,
}
impl<T> EnsureSend for RwLockCyclerWriter<T> where T: Send + Sync {}
impl<T> EnsureSync for RwLockCyclerWriter<T> where T: Send + Sync {}
impl<T> RwLockCyclerWriter<T> {
    // clone_fn!{
    //     "Changes the current writing block to another that is available copying from the last block using `clone_fn`.
    //      `clone_fn` is the same signature as `Clone::clone_from`, the first argument is what is being cloned to and the second being what is cloned from.
    //      This version takes an `impl FnOnce` so has no runtime cost for virtual lookup.",
    //     write_next_clone_fn,
    //     impl FnOnce(&mut T, &T)
    // }
    //
    // clone_fn!{
    //     "Changes the current writing block to another that is available copying from the last block using `clone_fn`.
    //      `clone_fn` is the same signature as `Clone::clone_from`, the first argument is what is being cloned to and the second being what is cloned from.
    //      This version takes a `&dyn Fn` allowing for dynamic functions",
    //     write_next_dyn_clone_fn,
    //     &dyn Fn(&mut T, &T)
    // }
    //
    // clone_fn!{
    //     "Changes the current writing block to another that is available copying from the last block using `clone_fn`.
    //      `clone_fn` is the same signature as `Clone::clone_from`, the first argument is what is being cloned to and the second being what is cloned from.
    //      This version takes a `&mut dyn FnMut` allowing for dynamic functions",
    //     write_next_dyn_mut_clone_fn,
    //     &mut dyn FnMut(&mut T, &T)
    // }
    //
    // clone_fn!{
    //     "Changes the current writing block to another that is available copying from the last block using `clone_fn`.
    //      `clone_fn` is the same signature as `Clone::clone_from`, the first argument is what is being cloned to and the second being what is cloned from.
    //      This version takes a `Box<dyn FnOnce>` allowing for dynamic functions",
    //     write_next_boxed_clone_fn,
    //     Box<dyn FnOnce(&mut T, &T)>
    // }
}
impl<T> ReadAccess for RwLockCyclerWriter<T>
where
    T: ReadAccess,
{
    type Read = T::Read;

    /// Gets a shared reference to the read data of the current block
    #[inline]
    fn read_data(&self) -> &Self::Read {
        self.writer.read_data()
    }
}
impl<T> WriteAccess for RwLockCyclerWriter<T>
where
    T: WriteAccess,
{
    type Write = T::Write;

    /// Gets a shared reference to the write data of the current block
    #[inline]
    fn write_data(&self) -> &Self::Write {
        self.writer.write_data()
    }

    /// Gets an exclusive reference to the write data of the current block
    #[inline]
    fn write_data_mut(&mut self) -> &mut Self::Write {
        self.writer.write_data_mut()
    }
}
impl<T> CyclerWriter<T> for RwLockCyclerWriter<T> where T: WriteAccess {}
impl<T> CyclerWriterFn<T> for RwLockCyclerWriter<T>
where
    T: WriteAccess,
{
    fn write_next_fn(&mut self, clone_fn: fn(&mut T, &T)) {
        rw_cycler_fn!(self, clone_fn);
    }

    fn write_next_fn_impl(&mut self, clone_fn: impl FnOnce(&mut T, &T))
    where
        Self: Sized,
    {
        rw_cycler_fn!(self, clone_fn);
    }

    fn write_next_fn_dyn(&mut self, clone_fn: &mut dyn FnMut(&mut T, &T)) {
        rw_cycler_fn!(self, clone_fn);
    }

    fn write_next_fn_dyn_boxed(&mut self, clone_fn: Box<dyn FnOnce(&mut T, &T)>) {
        rw_cycler_fn!(self, clone_fn);
    }
}
impl<T> CyclerWriterMutFn<T> for RwLockCyclerWriter<T>
where
    T: WriteAccess,
{
    fn write_next_mut_fn(&mut self, clone_fn: fn(&mut T, &mut T)) {
        rw_cycler_mut_fn!(self, clone_fn);
    }

    fn write_next_mut_fn_impl(&mut self, clone_fn: impl FnOnce(&mut T, &mut T))
    where
        Self: Sized,
    {
        rw_cycler_mut_fn!(self, clone_fn);
    }

    fn write_next_mut_fn_dyn(&mut self, clone_fn: &mut dyn FnMut(&mut T, &mut T)) {
        rw_cycler_mut_fn!(self, clone_fn);
    }

    fn write_next_mut_fn_dyn_boxed(&mut self, clone_fn: Box<dyn FnOnce(&mut T, &mut T)>) {
        rw_cycler_mut_fn!(self, clone_fn);
    }
}
impl<T> CyclerWriterDefault<T> for RwLockCyclerWriter<T>
where
    T: Clone + WriteAccess,
{
    fn write_next(&mut self) {
        self.write_next_fn(T::clone_from)
    }
}