quarkrs 0.1.1

A collection of small life improvements for rust.
Documentation
use std::sync::{RwLock, Arc, RwLockReadGuard, RwLockWriteGuard, TryLockError};

/// An Abstracted generic combining the functionality of Arc types and Read/Write locks.
/// all functions are atomic and can be passed between threads safely.
pub struct ThreadData<T> {
    value: Arc<RwLock<T>>
}

impl<T> Clone for ThreadData<T> {
    fn clone(&self) -> Self {
        Self { value: self.value.clone() }
    }
} 


impl<T> ThreadData<T> {
    pub fn new(val: T) -> ThreadData<T> {
        ThreadData { 
            value: Arc::new(
                RwLock::new(val)
            ),
        }
    }

    /// Attepmts to aquire immutable access to the data contained within, returning eithwer a ReadGuard if the data is available to be read, or a TryLockError if data is unreadable
    /// the data will be unreadable if there is a mutable access to the data somewhere else.
    /// ---
    /// ThreadData allows infinite immutable accesses or a single mutable access.
    pub fn try_read_access(&self) -> Result<RwLockReadGuard<'_, T>, TryLockError<RwLockReadGuard<'_, T>>>  {
        self.value.try_read() 
    }

    /// Attepmts to aquire mutable access to the data contained within, returning eithwer a WriteGuard if the data is available to be read, or a TryLockError if data is unreadable
    /// the data will be unreadable if there is a  access to the data somewhere else in any form.
    /// ---
    /// ThreadData allows infinite immutable accesses or a single mutable access.
    pub fn try_write_access(&self) -> Result<RwLockWriteGuard<'_, T>, TryLockError<RwLockWriteGuard<'_, T>>> {
        self.value.try_write() 
    }

    /// ignores any error managing while attempting to aquire an immutable referance to the data stored, if access is unavailable, function will panic.
    /// Useage is heavily discouraged as will likely cause panics.
    pub fn force_read_access(&self) -> RwLockReadGuard<'_, T> {
        match self.value.try_read() {
            Ok(out) => out,
            Err(_) => panic!(),
        }
    }

    /// ignores any error managing while attempting to aquire a mutable referance to the data stored, if access is unavailable, function will panic.
    /// Useage is heavily discouraged as will likely cause panics.
    pub fn force_write_access(&self) -> RwLockWriteGuard<'_, T> {
        match self.value.try_write() {
            Ok(out) => out,
            Err(_) => panic!(),
        }
    }
}