Skip to main content

steamkit/steam/steam_client/callback_mgr/
callback_msg.rs

1//! The [`CallbackMsg`] trait, implemented by every callback a client posts.
2
3use std::any::Any;
4
5use crate::types::job_id::JobID;
6
7/// A callback. Implementors store the job id the callback belongs to; embed a
8/// [`CallbackMsgBase`] for that.
9pub trait CallbackMsg: Any + Send + Sync {
10    /// The job this callback belongs to, or [`JobID::INVALID`] if it isn't a
11    /// job callback.
12    fn job_id(&self) -> JobID;
13
14    /// Sets the job this callback belongs to.
15    fn set_job_id(&mut self, id: JobID);
16
17    /// This callback as [`Any`], for downcasting to the concrete type.
18    fn as_any(&self) -> &dyn Any;
19
20    /// This callback as a mutable [`Any`], for downcasting to the concrete type.
21    fn as_any_mut(&mut self) -> &mut dyn Any;
22}
23
24/// Storage for the job id of a [`CallbackMsg`].
25#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
26pub struct CallbackMsgBase {
27    job_id: JobID,
28}
29
30impl CallbackMsgBase {
31    /// Creates a base whose job id is [`JobID::INVALID`].
32    pub fn new() -> Self {
33        Self {
34            job_id: JobID::INVALID,
35        }
36    }
37
38    /// Creates a base with the given job id.
39    pub fn with_job_id(job_id: JobID) -> Self {
40        Self { job_id }
41    }
42
43    /// The stored job id.
44    pub fn job_id(&self) -> JobID {
45        self.job_id
46    }
47
48    /// Stores a job id.
49    pub fn set_job_id(&mut self, id: JobID) {
50        self.job_id = id;
51    }
52}
53
54impl Default for CallbackMsgBase {
55    fn default() -> Self {
56        Self::new()
57    }
58}