pubsub_bus/
bus_event.rs

1// *************************************************************************
2//
3// Copyright (c) 2025 Andrei Gramakov. All rights reserved.
4//
5// This file is licensed under the terms of the MIT license.
6// For a copy, see: https://opensource.org/licenses/MIT
7//
8// site:    https://agramakov.me
9// e-mail:  mail@agramakov.me
10//
11// *************************************************************************
12
13#[cfg(test)]
14mod tests;
15
16/// A struct that represents an event that can be sent over the event bus.
17/// The content is user-defined. Besudes the content, the event has an id,
18/// a source id, and a topic id.
19pub struct BusEvent<ContentType, TopicId> {
20    id: usize,
21    topic_id: Option<TopicId>,
22    source_id: u64,
23    content: ContentType,
24}
25
26impl<ContentType, TopicId> BusEvent<ContentType, TopicId> {
27    pub fn new(id: usize, source_id: u64, topic_id: Option<TopicId>, content: ContentType) -> Self {
28        BusEvent {
29            id,
30            topic_id,
31            source_id,
32            content,
33        }
34    }
35
36    pub fn get_topic_id(&self) -> &Option<TopicId> {
37        &self.topic_id
38    }
39
40    pub fn get_id(&self) -> usize {
41        self.id
42    }
43
44    pub fn get_source_id(&self) -> u64 {
45        self.source_id
46    }
47
48    pub fn get_content(&self) -> &ContentType {
49        &self.content
50    }
51
52    pub fn get_mut_content(&mut self) -> &mut ContentType {
53        &mut self.content
54    }
55}