From 8839c4eea842fd216eef27c79a3e769f4bfa29cf Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Mon, 29 May 2017 04:31:15 +0100 Subject: [PATCH] add a gen_id method for plugins to generate ids --- src/plugin.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/plugin.rs b/src/plugin.rs index b41ab2c..6053b6c 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -8,6 +8,8 @@ use std::collections::HashMap; use std::sync::{RwLock, Arc}; +use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; + use std::marker::PhantomData; use std::ops::Deref; @@ -73,6 +75,7 @@ pub struct PluginProxyBinding { dispatcher: Arc, plugin_container: Arc, jid: Jid, + next_id: Arc, } impl PluginProxyBinding { @@ -81,6 +84,7 @@ impl PluginProxyBinding { dispatcher: dispatcher, plugin_container: plugin_container, jid: jid, + next_id: Arc::new(ATOMIC_USIZE_INIT), } } } @@ -152,6 +156,13 @@ impl PluginProxy { binding.jid.clone() }) } + + /// Get a new id. + pub fn gen_id(&self) -> String { + self.with_binding(|binding| { + format!("{}", binding.next_id.fetch_add(1, Ordering::SeqCst)) + }) + } } /// A trait whch all plugins should implement.