1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#![allow(clippy::trivially_copy_pass_by_ref)]
use libhydrogen_sys as ffi;

pub mod errors;
pub mod hash;
pub mod kdf;
pub mod random;
pub mod secretbox;
pub mod sign;
pub mod utils;
pub mod version;

use crate::errors::*;
use std::sync::{
    atomic::{AtomicBool, Ordering},
    Once,
};

static INIT: Once = Once::new();
static INITIALIZED: AtomicBool = AtomicBool::new(false);

pub fn init() -> Result<(), HydroError> {
    INIT.call_once(|| {
        if unsafe { ffi::hydro_init() } >= 0 {
            INITIALIZED.store(true, Ordering::Release);
        }
    });
    if INITIALIZED.load(Ordering::Acquire) {
        Ok(())
    } else {
        Err(HydroError::InitError)
    }
}

pub fn ensure_initialized() {
    assert!(
        INITIALIZED.load(Ordering::Acquire),
        "Hydrogen library not initialized"
    )
}