merc_aterm 3.0.0

A thread-safe library to manipulate and store first-order terms.
Documentation
use std::cmp::Ordering;
use std::fmt;
use std::hash::Hash;
use std::hash::Hasher;
use std::marker::PhantomData;

use delegate::delegate;

use merc_unsafety::ProtectionIndex;
use merc_unsafety::StablePointer;

use crate::Markable;
use crate::storage::Marker;
use crate::storage::SharedSymbol;
use crate::storage::THREAD_TERM_POOL;

/// The public interface for a function symbol. Can be used to write generic
/// functions that accept both [Symbol] and [SymbolRef].
///
/// See [crate::Term] for more information on how to use this trait with two lifetimes.
pub trait Symb<'a, 'b> {
    /// Obtain the symbol's name.
    fn name(&'b self) -> &'a str;

    /// Obtain the symbol's arity.
    fn arity(&self) -> usize;

    /// Create a copy of the symbol reference.
    fn copy(&'b self) -> SymbolRef<'a>;

    /// Returns a unique index for the symbol.
    fn index(&self) -> usize;

    /// Returns the underlying pool index of the symbol.
    fn shared(&self) -> &SymbolIndex;
}

/// An alias for the type that is used to reference into the [SharedSymbol] set.
pub type SymbolIndex = StablePointer<SharedSymbol>;

/// A reference to a function symbol in the symbol pool.
#[repr(transparent)]
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct SymbolRef<'a> {
    shared: SymbolIndex,
    marker: PhantomData<&'a ()>,
}

/// Check that the SymbolRef is the same size as a usize.
#[cfg(not(debug_assertions))]
const _: () = assert!(std::mem::size_of::<SymbolRef>() == std::mem::size_of::<usize>());

/// Check that the Option<SymbolRef> is the same size as a usize using niche value optimisation.
#[cfg(not(debug_assertions))]
const _: () = assert!(std::mem::size_of::<Option<SymbolRef>>() == std::mem::size_of::<usize>());

/// A reference to a function symbol with a known lifetime.
impl<'a> SymbolRef<'a> {
    /// Protects the symbol from garbage collection, yielding a `Symbol`.
    pub fn protect(&self) -> Symbol {
        THREAD_TERM_POOL.with(|tp| tp.protect_symbol(self))
    }

    /// Internal constructor to create a [SymbolRef] from a [SymbolIndex].
    ///
    /// # Safety
    ///
    /// We must ensure that the lifetime `'a` is valid for the returned `SymbolRef`.
    pub unsafe fn from_index(index: &SymbolIndex) -> SymbolRef<'a> {
        SymbolRef {
            // SAFETY: the caller guarantees the index remains valid for `'a`.
            shared: unsafe { index.copy() },
            marker: PhantomData,
        }
    }
}

impl SymbolRef<'_> {
    /// Internal constructor to convert any `Symb` to a `SymbolRef`.
    pub(crate) fn from_symbol<'a, 'b, S: Symb<'a, 'b>>(symbol: &'b S) -> Self {
        SymbolRef {
            // SAFETY: `symbol` keeps the index alive during this call.
            shared: unsafe { symbol.shared().copy() },
            marker: PhantomData,
        }
    }
}

impl<'a> Symb<'a, '_> for SymbolRef<'a> {
    fn name(&self) -> &'a str {
        unsafe { std::mem::transmute(self.shared.deref().name()) }
    }

    fn arity(&self) -> usize {
        // SAFETY: `self` is a `SymbolRef<'a>`, so the symbol is kept alive for
        // `'a` and the pointee is valid to read.
        unsafe { SharedSymbol::arity_of(self.shared.erased()) }
    }

    fn copy(&self) -> SymbolRef<'a> {
        unsafe { SymbolRef::from_index(self.shared()) }
    }

    fn index(&self) -> usize {
        self.shared.erased().as_ptr() as usize
    }

    fn shared(&self) -> &SymbolIndex {
        &self.shared
    }
}

impl Markable for SymbolRef<'_> {
    fn mark(&self, marker: &mut Marker) {
        marker.mark_symbol(self);
    }

    fn contains_term(&self, _term: &crate::aterm::ATermRef<'_>) -> bool {
        false
    }

    fn contains_symbol(&self, symbol: &SymbolRef<'_>) -> bool {
        self == symbol
    }

    fn len(&self) -> usize {
        1
    }
}

impl fmt::Display for SymbolRef<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

impl fmt::Debug for SymbolRef<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

/// A protected function symbol, with the same interface as [SymbolRef].
pub struct Symbol {
    symbol: SymbolRef<'static>,
    root: ProtectionIndex,
}

impl Symbol {
    /// Create a new symbol with the given name and arity.
    pub fn new<N>(name: N, arity: usize) -> Symbol
    where
        N: AsRef<str>,
    {
        THREAD_TERM_POOL.with(|tp| tp.create_symbol(name, arity))
    }

    /// Create a new symbol whose name is given by the format arguments, e.g.,
    /// `Symbol::new_fmt(format_args!("x{n}"), 0)`.
    ///
    /// Formats into a reusable per-thread buffer, so unlike
    /// `Symbol::new(format!(..), arity)` this does not allocate a temporary
    /// string; the name is only copied when the symbol does not exist yet.
    pub fn new_fmt(args: fmt::Arguments<'_>, arity: usize) -> Symbol {
        THREAD_TERM_POOL.with(|tp| tp.create_symbol_fmt(args, arity))
    }

    /// Like [Symbol::new_fmt], but only creates the symbol if no symbol with this name and
    /// arity exists yet, and returns `None` otherwise.
    ///
    /// A term keeps its symbols alive, so the returned symbol occurs in no term that is alive,
    /// in any thread, which makes it a fresh name. Checking and creating is one step, so two
    /// threads never receive the same symbol. `None` is also returned for a symbol that no
    /// term uses any more but that was not garbage collected yet.
    pub fn new_unused_fmt(args: fmt::Arguments<'_>, arity: usize) -> Option<Symbol> {
        THREAD_TERM_POOL.with(|tp| tp.create_unused_symbol_fmt(args, arity))
    }
}

impl Symbol {
    /// Internal constructor to create a symbol from an index and a root.
    pub(crate) unsafe fn from_index(index: &SymbolIndex, root: ProtectionIndex) -> Symbol {
        Self {
            symbol: unsafe { SymbolRef::from_index(index) },
            root,
        }
    }

    /// Returns the root index, i.e., the index in the thread's protection set.
    pub fn root(&self) -> ProtectionIndex {
        self.root
    }

    /// Create a copy of the symbol reference.
    pub fn copy(&self) -> SymbolRef<'_> {
        self.symbol.copy()
    }

    /// Returns the symbol as a borrowed [SymbolRef] whose lifetime is bounded
    /// by this protected symbol.
    pub fn get(&self) -> &SymbolRef<'_> {
        &self.symbol
    }
}

impl<'a> Symb<'a, '_> for &'a Symbol {
    delegate! {
        to self.symbol {
            fn name(&self) -> &'a str;
            fn arity(&self) -> usize;
            fn copy(&self) -> SymbolRef<'a>;
            fn index(&self) -> usize;
            fn shared(&self) -> &SymbolIndex;
        }
    }
}

impl<'a, 'b> Symb<'a, 'b> for Symbol
where
    'b: 'a,
{
    delegate! {
        to self.symbol {
            fn name(&self) -> &'a str;
            fn arity(&self) -> usize;
            fn copy(&self) -> SymbolRef<'a>;
            fn index(&self) -> usize;
            fn shared(&self) -> &SymbolIndex;
        }
    }
}

impl Drop for Symbol {
    fn drop(&mut self) {
        THREAD_TERM_POOL.with(|tp| {
            tp.drop_symbol(self);
        })
    }
}

impl From<&SymbolRef<'_>> for Symbol {
    fn from(value: &SymbolRef) -> Self {
        value.protect()
    }
}

impl Clone for Symbol {
    fn clone(&self) -> Self {
        self.copy().protect()
    }
}

impl fmt::Display for Symbol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

impl fmt::Debug for Symbol {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name())
    }
}

impl Hash for Symbol {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.copy().hash(state)
    }
}

impl PartialEq for Symbol {
    fn eq(&self, other: &Self) -> bool {
        self.copy().eq(&other.copy())
    }
}

impl PartialEq<SymbolRef<'_>> for Symbol {
    fn eq(&self, other: &SymbolRef<'_>) -> bool {
        self.copy().eq(other)
    }
}

impl PartialOrd for Symbol {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Symbol {
    fn cmp(&self, other: &Self) -> Ordering {
        self.copy().cmp(&other.copy())
    }
}

impl Eq for Symbol {}