From f9e4280836757f7a53aba6ad4c05b9ae47096daf Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Mon, 27 Jul 2015 22:15:44 +0200 Subject: [PATCH] Use std::sync::Once for init --- src/lib.rs | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 03588c6..e6ce7c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ extern crate mongo_c_driver_wrapper; extern crate bson; use std::result; +use std::sync::{Once,ONCE_INIT}; use mongo_c_driver_wrapper::bindings; @@ -21,37 +22,21 @@ pub use error::{MongoError,BsoncError,InvalidParamsError}; pub type Result = result::Result; -static mut INITIALIZED: bool = false; +static MONGOC_INIT: Once = ONCE_INIT; /// Init mongo driver, needs to be called once before doing /// anything else. pub fn init() { - unsafe { - bindings::mongoc_init(); - INITIALIZED = true; - } -} - -/// Clean up mongo driver's resources -pub fn cleanup() { - unsafe { - bindings::mongoc_cleanup(); - INITIALIZED = false; - } -} - -pub fn is_initialized() -> bool { - unsafe { INITIALIZED } + MONGOC_INIT.call_once(|| { + unsafe { bindings::mongoc_init(); } + }); } #[cfg(test)] mod tests { #[test] - fn test_init_and_cleanup() { + fn test_init() { + super::init(); super::init(); - assert!(super::is_initialized()); - - super::cleanup(); - assert!(!super::is_initialized()); } }