[pve-devel] [PATCH v3 proxmox 15/66] notify: add context
Wolfgang Bumiller
w.bumiller at proxmox.com
Tue Jul 18 14:57:29 CEST 2023
On Mon, Jul 17, 2023 at 05:00:00PM +0200, Lukas Wagner wrote:
> 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);
Hmmm.
Given how this will be accsssed, we could probably just use a Cell
instead of a Mutex to make this even cheaper...
> +
> +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