1#![deny(non_upper_case_globals)]
19#![deny(non_camel_case_types)]
20#![deny(non_snake_case)]
21#![deny(unused_mut)]
22#![warn(missing_docs)]
23
24#[macro_use]
25extern crate log;
26#[macro_use]
27extern crate lazy_static;
28#[macro_use]
29extern crate serde_derive;
30pub use parking_lot::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard};
32
33pub use secp256k1zkp as secp;
35
36pub mod logger;
38pub use crate::logger::{init_logger, init_test_logger};
39
40pub mod secp_static;
42pub use crate::secp_static::static_secp_instance;
43
44pub mod types;
45pub use crate::types::ZeroingString;
46
47pub mod macros;
48
49#[allow(unused_imports)]
51use std::ops::Deref;
52use std::sync::atomic::{AtomicBool, Ordering};
53use std::sync::Arc;
54mod hex;
55pub use crate::hex::*;
56
57pub mod file;
59pub mod zip;
61
62mod rate_counter;
63pub use crate::rate_counter::RateCounter;
64
65#[derive(Clone)]
70pub struct OneTime<T> {
71 inner: Arc<RwLock<Option<T>>>,
73}
74
75impl<T> OneTime<T>
76where
77 T: Clone,
78{
79 pub fn new() -> OneTime<T> {
81 OneTime {
82 inner: Arc::new(RwLock::new(None)),
83 }
84 }
85
86 pub fn init(&self, value: T) {
89 self.set(value, false);
90 }
91
92 pub fn set(&self, value: T, is_override: bool) {
94 let mut inner = self.inner.write();
95 if !is_override {
96 assert!(inner.is_none());
97 }
98 *inner = Some(value);
99 }
100
101 pub fn borrow(&self) -> T {
104 let inner = self.inner.read();
105 inner
106 .clone()
107 .expect("Cannot borrow one_time before initialization.")
108 }
109
110 pub fn is_init(&self) -> bool {
112 self.inner.read().is_some()
113 }
114}
115
116pub fn to_base64(s: &str) -> String {
118 base64::encode(s)
119}
120
121pub struct StopState {
127 stopped: AtomicBool,
128 paused: AtomicBool,
129}
130
131impl StopState {
132 pub fn new() -> StopState {
134 StopState {
135 stopped: AtomicBool::new(false),
136 paused: AtomicBool::new(false),
137 }
138 }
139
140 pub fn is_stopped(&self) -> bool {
142 self.stopped.load(Ordering::Relaxed)
143 }
144
145 pub fn is_paused(&self) -> bool {
147 self.paused.load(Ordering::Relaxed)
148 }
149
150 pub fn stop(&self) {
152 self.stopped.store(true, Ordering::Relaxed)
153 }
154
155 pub fn pause(&self) {
157 self.paused.store(true, Ordering::Relaxed)
158 }
159
160 pub fn resume(&self) {
162 self.paused.store(false, Ordering::Relaxed)
163 }
164}