From 23cbbda2b08fe58911d7fdc13aad3e704d43fc69 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Mon, 8 Feb 2016 20:03:43 +0100 Subject: [PATCH] Only calling crypto_init once --- src/crypto.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 52c9741..93d5c3f 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -4,6 +4,9 @@ use std::ptr; use std::ffi::CStr; +use std::sync::{Once, ONCE_INIT}; + +static CRYPTO_INIT: Once = ONCE_INIT; use libc::{size_t, c_char, c_ulonglong, c_int}; use aligned_alloc::{aligned_alloc, aligned_free}; @@ -147,9 +150,11 @@ fn inc_nonce_12(nonce: &mut [u8; 12]) { impl Crypto { pub fn init() { - if unsafe { sodium_init() } != 0 { - fail!("Failed to initialize crypto library"); - } + CRYPTO_INIT.call_once(|| { + if unsafe { sodium_init() } != 0 { + fail!("Failed to initialize crypto library"); + } + }); } pub fn sodium_version() -> String {