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
//! Define stateful method that interact with database.
use crate::{
basic::{Value, EID},
Prop,
};
/// The merge method
///
/// It is designed to apply the change to a value.
/// Note that the sled database require the function parameter EID needs its ownship. Since it's Copy, it won't take some difficulty to convert but ugly.
///
/// # Args
/// - `eid: EID`: Target EID, useful if the merge method is mutable (like FnMut closure).
/// - `old: Option<Value>`: The value that exists in database before merge. Option::None if the value does not exist.
/// - `delta: Delta`: The incoming delta, which needs to be merged with the current value.
///
/// # Return
/// - `Option<Value>`: The new value after merge. Option::None if the value is deleted.
///
pub trait MergeFn<V>: Fn(EID, Option<V>, V) -> Option<V> + 'static
where
V: Value,
{
}
impl<T, V> MergeFn<V> for T
where
T: Fn(EID, Option<V>, V) -> Option<V> + Send + Sync + 'static,
V: Value,
{
}
/// Tick method
///
/// Designed to apply the change to a value.
///
/// # Behavior
/// Let's say if there were X number of entity in a prop.
/// This method will be called to every entity by its eid, value and the &prop, X times in total.
/// Different method calls may happen at same time.
///
/// ## Stateful
/// This method is considered as stateful, here are some interesting ideas that could be used inside a tick:
///
/// * initiate once stateful container
/// * hold a thread pool to split every tick call into different thread, the call on every method is treated as trigger
/// * use zero-mq to communicate between different tick method.
///
/// ## Thread Safe
/// Notice that, the TickFn will be used as Sync + Send, so it is your job to ensure the stateful data is
/// Send + Send inside the TickFn.
///
/// # Args
/// - `eid: EID`: The operation target id, useful if the tick method is mutable (like FnMut closure).
/// - `value: Value`: The value that exists in database before tick.
/// - `prop: &Prop`: The whole property storage that the value is stored in.
///
/// # Return
/// - `Option::<Delta>`: The new delta after tick. Option::None if stays the same (takes no effect).
///
pub trait TickFn<V>: Fn(&EID, V, &Prop<V>) -> Option<V> + Send + Sync + 'static
where
V: Value,
{
}
impl<T, V> TickFn<V> for T
where
T: Fn(&EID, V, &Prop<V>) -> Option<V> + Send + Sync + 'static,
V: Value,
{
}