use std::{
error::Error,
fmt,
sync::{Mutex, MutexGuard},
};
static FFI_MUTEX: Mutex<()> = Mutex::new(());
pub struct OpenSslMutex<'a> {
#[allow(dead_code)]
guard: MutexGuard<'a, ()>,
}
impl OpenSslMutex<'_> {
pub fn acquire() -> Result<Self, OpenSslMutexUnavailable> {
match FFI_MUTEX.lock() {
Ok(guard) => Ok(Self { guard }),
Err(_) => Err(OpenSslMutexUnavailable {}),
}
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct OpenSslMutexUnavailable;
impl fmt::Display for OpenSslMutexUnavailable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Unable to acquire OpenSSL native code mutex")
}
}
impl Error for OpenSslMutexUnavailable {}
#[cfg(test)]
mod tests {
#![allow(clippy::expect_used)]
#![allow(clippy::panic)]
#![allow(clippy::unwrap_used)]
use crate::crypto::raw_signature::openssl::OpenSslMutexUnavailable;
#[test]
fn impl_display() {
let err = OpenSslMutexUnavailable {};
assert_eq!(
err.to_string(),
"Unable to acquire OpenSSL native code mutex"
);
}
#[test]
fn impl_debug() {
let err = OpenSslMutexUnavailable {};
assert_eq!(format!("{err:?}"), "OpenSslMutexUnavailable");
}
}