nitrokey_test_state/
lib.rs

1// lib.rs
2
3// *************************************************************************
4// * Copyright (C) 2019 Daniel Mueller (deso@posteo.net)                   *
5// *                                                                       *
6// * This program is free software: you can redistribute it and/or modify  *
7// * it under the terms of the GNU General Public License as published by  *
8// * the Free Software Foundation, either version 3 of the License, or     *
9// * (at your option) any later version.                                   *
10// *                                                                       *
11// * This program is distributed in the hope that it will be useful,       *
12// * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14// * GNU General Public License for more details.                          *
15// *                                                                       *
16// * You should have received a copy of the GNU General Public License     *
17// * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
18// *************************************************************************
19
20#![deny(
21  missing_copy_implementations,
22  missing_debug_implementations,
23  trivial_casts,
24  trivial_numeric_casts,
25  unstable_features,
26  unused_import_braces,
27  unused_qualifications,
28  unused_results,
29)]
30#![warn(
31  future_incompatible,
32  rust_2018_compatibility,
33  rust_2018_idioms,
34)]
35
36//! A crate capturing the state needed by the `nitrokey-test` crate.
37
38use std::sync;
39
40/// A function returning a `Mutex` used for serializing tests.
41pub fn mutex() -> &'static sync::Mutex<()> {
42  static mut MUTEX: Option<sync::Mutex<()>> = None;
43  static ONCE: sync::Once = sync::Once::new();
44
45  ONCE.call_once(|| unsafe { MUTEX = Some(sync::Mutex::new(())) });
46
47  match unsafe { &MUTEX } {
48    Some(mutex) => mutex,
49    None => unreachable!(),
50  }
51}