hao 0.0.19

Read, edit, create and write .net assemblies
Documentation
//! The pointer and lock types behind every [`Entry`](crate::dotnet::entries::Entry).
//!
//! By default entries are `Rc<RefCell<_>>`: cheap, but not `Send`/`Sync`. With the
//! `sync` feature they become `Arc<RwLock<_>>`, so a module can be shared across
//! threads. Code that uses these aliases and functions instead of naming
//! `Rc`/`Arc` directly works with either.
//!
//! Most code never needs this module: [`Entry::get`](crate::dotnet::entries::Entry::get)
//! and [`Entry::get_mut`](crate::dotnet::entries::Entry::get_mut) do the locking.

#[cfg(not(feature = "sync"))]
mod imp {
    use std::cell::{Cell, Ref, RefCell, RefMut};

    /// Shared ownership of an entry: `Rc<T>`, or `Arc<T>` with `sync`.
    pub type Shared<T> = std::rc::Rc<T>;
    /// A non-owning [`Shared`]: `rc::Weak<T>`, or `sync::Weak<T>` with `sync`.
    pub type WeakShared<T> = std::rc::Weak<T>;
    /// Interior mutability for an entry: `RefCell<T>`, or `RwLock<T>` with `sync`.
    pub type Lock<T> = RefCell<T>;
    /// A shared borrow of a [`Lock`]: `Ref`, or `RwLockReadGuard` with `sync`.
    pub type ReadGuard<'a, T> = Ref<'a, T>;
    /// An exclusive borrow of a [`Lock`]: `RefMut`, or `RwLockWriteGuard` with `sync`.
    pub type WriteGuard<'a, T> = RefMut<'a, T>;

    /// Borrows `lock` for reading.
    ///
    /// Without `sync` this panics if it is already borrowed for writing; with `sync`
    /// it blocks, and ignores poisoning.
    #[inline(always)]
    pub fn read<T>(lock: &Lock<T>) -> ReadGuard<'_, T> {
        lock.borrow()
    }

    /// Borrows `lock` for writing.
    ///
    /// Without `sync` this panics if it is already borrowed; with `sync` it blocks,
    /// and ignores poisoning.
    #[inline(always)]
    pub fn write<T>(lock: &Lock<T>) -> WriteGuard<'_, T> {
        lock.borrow_mut()
    }

    /// Borrows `lock` for reading, or `None` if it is borrowed for writing (or, with
    /// `sync`, poisoned).
    #[inline(always)]
    pub fn try_read<T>(lock: &Lock<T>) -> Option<ReadGuard<'_, T>> {
        lock.try_borrow().ok()
    }

    /// Borrows `lock` for writing, or `None` if it is borrowed at all (or, with
    /// `sync`, poisoned).
    #[inline(always)]
    pub fn try_write<T>(lock: &Lock<T>) -> Option<WriteGuard<'_, T>> {
        lock.try_borrow_mut().ok()
    }

    /// A row id (RID) that can be updated through a shared reference, used to number
    /// entries when a module is written. A `Cell<u32>`, or an `AtomicU32` with `sync`.
    #[derive(Debug, Default)]
    pub struct RidCell(Cell<u32>);

    impl RidCell {
        /// A cell holding `v`.
        pub fn new(v: u32) -> Self {
            Self(Cell::new(v))
        }
        /// The current value.
        pub fn get(&self) -> u32 {
            self.0.get()
        }
        /// Replaces the value.
        pub fn set(&self, v: u32) {
            self.0.set(v)
        }
    }
}

#[cfg(feature = "sync")]
mod imp {
    use std::sync::{
        atomic::{AtomicU32, Ordering},
        PoisonError, RwLock, RwLockReadGuard, RwLockWriteGuard,
    };

    /// Shared ownership of an entry: `Rc<T>`, or `Arc<T>` with `sync`.
    pub type Shared<T> = std::sync::Arc<T>;
    /// A non-owning [`Shared`]: `rc::Weak<T>`, or `sync::Weak<T>` with `sync`.
    pub type WeakShared<T> = std::sync::Weak<T>;
    /// Interior mutability for an entry: `RefCell<T>`, or `RwLock<T>` with `sync`.
    pub type Lock<T> = RwLock<T>;
    /// A shared borrow of a [`Lock`]: `Ref`, or `RwLockReadGuard` with `sync`.
    pub type ReadGuard<'a, T> = RwLockReadGuard<'a, T>;
    /// An exclusive borrow of a [`Lock`]: `RefMut`, or `RwLockWriteGuard` with `sync`.
    pub type WriteGuard<'a, T> = RwLockWriteGuard<'a, T>;

    /// Borrows `lock` for reading.
    ///
    /// Without `sync` this panics if it is already borrowed for writing; with `sync`
    /// it blocks, and ignores poisoning.
    #[inline(always)]
    pub fn read<T>(lock: &Lock<T>) -> ReadGuard<'_, T> {
        lock.read().unwrap_or_else(PoisonError::into_inner)
    }

    /// Borrows `lock` for writing.
    ///
    /// Without `sync` this panics if it is already borrowed; with `sync` it blocks,
    /// and ignores poisoning.
    #[inline(always)]
    pub fn write<T>(lock: &Lock<T>) -> WriteGuard<'_, T> {
        lock.write().unwrap_or_else(PoisonError::into_inner)
    }

    /// Borrows `lock` for reading, or `None` if it is borrowed for writing (or, with
    /// `sync`, poisoned).
    #[inline(always)]
    pub fn try_read<T>(lock: &Lock<T>) -> Option<ReadGuard<'_, T>> {
        lock.try_read().ok()
    }

    /// Borrows `lock` for writing, or `None` if it is borrowed at all (or, with
    /// `sync`, poisoned).
    #[inline(always)]
    pub fn try_write<T>(lock: &Lock<T>) -> Option<WriteGuard<'_, T>> {
        lock.try_write().ok()
    }

    /// A row id (RID) that can be updated through a shared reference, used to number
    /// entries when a module is written. A `Cell<u32>`, or an `AtomicU32` with `sync`.
    #[derive(Debug, Default)]
    #[cfg_attr(feature = "reflect", derive(facet::Facet), facet(opaque))]
    pub struct RidCell(AtomicU32);

    impl RidCell {
        /// A cell holding `v`.
        pub fn new(v: u32) -> Self {
            Self(AtomicU32::new(v))
        }
        /// The current value.
        pub fn get(&self) -> u32 {
            self.0.load(Ordering::Relaxed)
        }
        /// Replaces the value.
        pub fn set(&self, v: u32) {
            self.0.store(v, Ordering::Relaxed)
        }
    }
}

pub use imp::*;