cloneable-file 0.1.1

Cloneable file descriptor
Documentation
//! Feature-independent lock aliases and helper functions.
#[cfg(not(feature = "parking_lot"))]
pub(crate) type Lock<T> = std::sync::Mutex<T>;

#[cfg(feature = "parking_lot")]
pub(crate) type Lock<T> = parking_lot::Mutex<T>;

pub(crate) fn acquire<T, R>(lock: &Lock<T>, func: impl FnOnce(&T) -> R) -> R {
    #[cfg(not(feature = "parking_lot"))]
    {
        return func(&lock.lock().unwrap());
    }
    #[cfg(feature = "parking_lot")]
    {
        return func(&lock.lock());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn smoke_test() {
        let lock = Lock::new(1);
        let result = acquire(&lock, |v| *v + 1);
        assert_eq!(result, 2);
    }
}