2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2016-12-21 04:29:35 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-11-01 02:32:33 +03:00
|
|
|
//! Logging, as well as various low-level utilities that factor Rust
|
2017-10-12 19:56:44 +03:00
|
|
|
//! patterns that are frequent within the grin codebase.
|
2016-12-21 04:29:35 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
#![deny(non_upper_case_globals)]
|
|
|
|
#![deny(non_camel_case_types)]
|
|
|
|
#![deny(non_snake_case)]
|
|
|
|
#![deny(unused_mut)]
|
|
|
|
#![warn(missing_docs)]
|
|
|
|
|
|
|
|
#[macro_use]
|
2018-10-21 23:30:56 +03:00
|
|
|
extern crate log;
|
2017-10-12 19:56:44 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
2018-10-20 03:13:07 +03:00
|
|
|
// Re-export so only has to be included once
|
|
|
|
pub use parking_lot::Mutex;
|
|
|
|
pub use parking_lot::RwLock;
|
2017-10-12 19:56:44 +03:00
|
|
|
|
2017-11-01 02:20:55 +03:00
|
|
|
// Re-export so only has to be included once
|
2018-12-08 02:59:40 +03:00
|
|
|
pub use secp256k1zkp as secp;
|
2017-11-01 02:20:55 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
// Logging related
|
|
|
|
pub mod logger;
|
2018-12-08 02:59:40 +03:00
|
|
|
pub use crate::logger::{init_logger, init_test_logger};
|
2017-10-12 19:56:44 +03:00
|
|
|
|
2017-11-07 19:48:37 +03:00
|
|
|
// Static secp instance
|
|
|
|
pub mod secp_static;
|
2018-12-08 02:59:40 +03:00
|
|
|
pub use crate::secp_static::static_secp_instance;
|
2017-11-07 19:48:37 +03:00
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
pub mod types;
|
2018-12-08 02:59:40 +03:00
|
|
|
pub use crate::types::{LogLevel, LoggingConfig};
|
2017-10-12 19:56:44 +03:00
|
|
|
|
2018-09-04 12:58:26 +03:00
|
|
|
pub mod macros;
|
|
|
|
|
2018-11-16 14:00:39 +03:00
|
|
|
// read_exact and write_all impls
|
|
|
|
pub mod read_write;
|
|
|
|
|
2017-10-12 19:56:44 +03:00
|
|
|
// other utils
|
2016-12-21 04:29:35 +03:00
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::ops::Deref;
|
2018-10-20 03:13:07 +03:00
|
|
|
use std::sync::Arc;
|
2016-12-21 04:29:35 +03:00
|
|
|
|
2017-05-25 02:08:39 +03:00
|
|
|
mod hex;
|
2018-12-08 02:59:40 +03:00
|
|
|
pub use crate::hex::*;
|
2017-05-25 02:08:39 +03:00
|
|
|
|
2018-08-03 05:16:16 +03:00
|
|
|
/// File util
|
|
|
|
pub mod file;
|
2018-09-04 12:58:26 +03:00
|
|
|
/// Compress and decompress zip bz2 archives
|
|
|
|
pub mod zip;
|
2018-02-10 01:32:16 +03:00
|
|
|
|
2018-11-07 04:51:22 +03:00
|
|
|
mod rate_counter;
|
2018-12-08 02:59:40 +03:00
|
|
|
pub use crate::rate_counter::RateCounter;
|
2018-11-07 04:51:22 +03:00
|
|
|
|
2018-10-09 18:53:57 +03:00
|
|
|
/// Encapsulation of a RwLock<Option<T>> for one-time initialization.
|
|
|
|
/// This implementation will purposefully fail hard if not used
|
|
|
|
/// properly, for example if not initialized before being first used
|
2017-10-12 19:56:44 +03:00
|
|
|
/// (borrowed).
|
2017-07-04 02:46:25 +03:00
|
|
|
#[derive(Clone)]
|
2016-12-21 04:29:35 +03:00
|
|
|
pub struct OneTime<T> {
|
2018-10-09 18:53:57 +03:00
|
|
|
/// The inner value.
|
|
|
|
inner: Arc<RwLock<Option<T>>>,
|
2016-12-21 04:29:35 +03:00
|
|
|
}
|
|
|
|
|
2018-10-09 18:53:57 +03:00
|
|
|
impl<T> OneTime<T>
|
|
|
|
where
|
|
|
|
T: Clone,
|
|
|
|
{
|
2017-09-29 21:44:25 +03:00
|
|
|
/// Builds a new uninitialized OneTime.
|
|
|
|
pub fn new() -> OneTime<T> {
|
2017-11-01 02:32:33 +03:00
|
|
|
OneTime {
|
2018-10-09 18:53:57 +03:00
|
|
|
inner: Arc::new(RwLock::new(None)),
|
2017-11-01 02:32:33 +03:00
|
|
|
}
|
2017-09-29 21:44:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Initializes the OneTime, should only be called once after construction.
|
2018-10-09 18:53:57 +03:00
|
|
|
/// Will panic (via assert) if called more than once.
|
2017-09-29 21:44:25 +03:00
|
|
|
pub fn init(&self, value: T) {
|
2018-10-20 03:13:07 +03:00
|
|
|
let mut inner = self.inner.write();
|
2018-10-09 18:53:57 +03:00
|
|
|
assert!(inner.is_none());
|
|
|
|
*inner = Some(value);
|
2017-10-17 16:51:02 +03:00
|
|
|
}
|
|
|
|
|
2017-09-29 21:44:25 +03:00
|
|
|
/// Borrows the OneTime, should only be called after initialization.
|
2018-10-09 18:53:57 +03:00
|
|
|
/// Will panic (via expect) if called before initialization.
|
|
|
|
pub fn borrow(&self) -> T {
|
2018-10-20 03:13:07 +03:00
|
|
|
let inner = self.inner.read();
|
2018-10-09 18:53:57 +03:00
|
|
|
inner
|
|
|
|
.clone()
|
|
|
|
.expect("Cannot borrow one_time before initialization.")
|
2017-09-29 21:44:25 +03:00
|
|
|
}
|
2016-12-21 04:29:35 +03:00
|
|
|
}
|
2018-01-10 22:36:27 +03:00
|
|
|
|
2018-09-26 23:38:44 +03:00
|
|
|
/// Encode an utf8 string to a base64 string
|
|
|
|
pub fn to_base64(s: &str) -> String {
|
|
|
|
base64::encode(s)
|
|
|
|
}
|