kik_sync_service/kik_message.rs
1//! # Messages
2//!
3//! Traits for defining how the channel should transfer, work and retrieve the messages.
4//! Users have to implement these traits in their data structure before using the *DeliveryService*.
5//!
6//! The type with the *Message* trait must be able to hold types that implement *MessageData* and *MessageInput* as well.
7//!
8//! *Message* is what the channels share. *MessageInput* is what *FeederRecycler* sets in each *Message*.
9//! *MessageData* is what the channel returns when the user iterates through it.
10//!
11//! The *MessageData* shared must be *Sync* and *Send*. Must have *'static* lifetimes, must have *Clone* trait,
12//! but doesn't need to be *Copy*. I haven't tested if being *Copy* will break *Drop* behaviors.
13//! But if it did, compiler would probably notice.
14//!
15//!
16
17use std::marker::{Send, Sync};
18
19// Making sure that this trait only applies to objects that have Clone
20/// MessageData holds the resource type that will be returned by the worker-threads. Must implement Sync, Send, Clone and have lifetime 'static.
21pub trait MessageData: Sync + Send + Clone + 'static{
22 fn new() -> Self;
23}
24
25// This is the trait input that can only be applied to ojbects with MessageData trait
26/// MessageInput will have the input arguments for generating each MessageData. Must implement Sync, Send, Clone and have lifetime 'static.
27pub trait MessageInput<T> : Sync + Send + Clone + 'static where T: MessageData
28{
29 fn new() -> Self;
30}
31
32// This is the Message Trait that holds the data and the value type that changes it
33/// Message has the tools to generate each MessageData T, based on each MessageInput R. Must implement Sync, Send, Clone and have lifetime 'static.
34pub trait Message<T, R> : Sync + Send + Clone + 'static where
35 R: MessageInput<T>,
36 T: MessageData,
37{
38 /// Behavior for storing a given input MessageInput, before a worker can use it for generating MessageData. Used by kik_feeder.
39 fn set_input(&mut self, message_input: R);
40
41 /// Workers will call this to use the stored MessageInput (R<T>) to generate and replace the existing MessageData stored. Used by kik_worker.
42 fn work(&mut self);
43
44 /// This will call MessageInput::new() method. No need to implement this. Used by kik_feeder.
45 fn new_message_input() -> R{
46 R::new()
47 }
48
49 /// This will call MessageData::new() method. No need to implement this. Used by kik_feeder.
50 fn new_message_data() -> T{
51 T::new()
52 }
53
54 /// This method is used when retrieving MessageData for the iterator. Clone the MessageData stored and return it. Used by kik_feeder.
55 fn clone_message_data(&self) -> T;
56
57 /// Construct a new message with default values. Used by kik_feeder.
58 fn new() -> Self;
59
60}