[pve-devel] [PATCH v3 proxmox 15/66] notify: add context

Lukas Wagner l.wagner at proxmox.com
Mon Jul 17 17:00:00 CEST 2023


Since `proxmox-notify` is intended to be used by multiple products,
there needs to be a way to inject product-specific behavior.

Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
 proxmox-notify/src/context.rs | 13 +++++++++++++
 proxmox-notify/src/lib.rs     |  1 +
 2 files changed, 14 insertions(+)
 create mode 100644 proxmox-notify/src/context.rs

diff --git a/proxmox-notify/src/context.rs b/proxmox-notify/src/context.rs
new file mode 100644
index 00000000..55c0eda1
--- /dev/null
+++ b/proxmox-notify/src/context.rs
@@ -0,0 +1,13 @@
+use std::sync::Mutex;
+
+pub trait Context: Send + Sync {}
+
+static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(None);
+
+pub fn set_context(context: &'static dyn Context) {
+    *CONTEXT.lock().unwrap() = Some(context);
+}
+
+pub(crate) fn context() -> &'static dyn Context {
+    (*CONTEXT.lock().unwrap()).expect("context for proxmox-notify has not been set yet")
+}
diff --git a/proxmox-notify/src/lib.rs b/proxmox-notify/src/lib.rs
index 548cc56f..3c2b6d55 100644
--- a/proxmox-notify/src/lib.rs
+++ b/proxmox-notify/src/lib.rs
@@ -13,6 +13,7 @@ use std::error::Error as StdError;
 
 pub mod api;
 mod config;
+pub mod context;
 pub mod endpoints;
 pub mod filter;
 pub mod group;
-- 
2.39.2






More information about the pve-devel mailing list