clips-bindings 0.1.2+clips-6.4.2

Safe, idiomatic, custom-written Rust bindings for the CLIPS 6.4.2 embedding API
Documentation
//! Compile-time checks for the crate's single-thread-affinity contract.

// Trait inference is ambiguous if a type implements the asserted trait. This
// is the same stable-Rust technique used by negative static-assertion crates.
macro_rules! assert_not_impl {
    ($type:ty: $trait:path) => {
        const _: fn() = || {
            trait AmbiguousIfImpl<Marker> {
                fn check() {}
            }

            impl<T: ?Sized> AmbiguousIfImpl<()> for T {}

            struct WouldBeAmbiguous;
            impl<T: ?Sized + $trait> AmbiguousIfImpl<WouldBeAmbiguous> for T {}

            let _ = <$type as AmbiguousIfImpl<_>>::check;
        };
    };
}

macro_rules! assert_not_send_or_sync {
    ($($type:ty),+ $(,)?) => {
        $(
            assert_not_impl!($type: Send);
            assert_not_impl!($type: Sync);
        )+
    };
}

assert_not_send_or_sync!(
    crate::Environment,
    crate::Activation<'static>,
    crate::Activations<'static>,
    crate::MessageHandler<'static>,
    crate::MessageHandlers<'static>,
    crate::Defmethod<'static>,
    crate::Defmethods<'static>,
    crate::Fact<'static>,
    crate::Facts<'static>,
    crate::FactBuilder<'static>,
    crate::FactModifier<'static>,
    crate::FunctionCall<'static>,
    crate::Instance<'static>,
    crate::Instances<'static>,
    crate::InstanceBuilder<'static>,
    crate::InstanceModifier<'static>,
    crate::FocusEntry<'static>,
    crate::FocusStack<'static>,
    crate::Multifield<'static>,
    crate::MultifieldBuilder<'static>,
    crate::TemplateSlot<'static, 'static>,
    crate::Value<'static>,
    crate::ClassSlot<'static>,
    crate::FactsInTemplate<'static>,
    crate::InstancesInClass<'static>,
    crate::MessageCall<'static>,
    crate::Dribble<'static>,
    crate::construct::Deftemplate<'static>,
    crate::construct::Deftemplates<'static>,
    crate::construct::Defrule<'static>,
    crate::construct::Defrules<'static>,
    crate::construct::Deffacts<'static>,
    crate::construct::DeffactsIter<'static>,
    crate::construct::Defglobal<'static>,
    crate::construct::Defglobals<'static>,
    crate::construct::Deffunction<'static>,
    crate::construct::Deffunctions<'static>,
    crate::construct::Defgeneric<'static>,
    crate::construct::Defgenerics<'static>,
    crate::construct::Defclass<'static>,
    crate::construct::Defclasses<'static>,
    crate::construct::Definstances<'static>,
    crate::construct::DefinstancesIter<'static>,
    crate::construct::Defmodule<'static>,
    crate::construct::Defmodules<'static>,
    crate::router::RouterEntry,
    crate::udf::UdfRegistration,
    crate::hook::HookEntry,
    // Call-scoped address tokens, generated by `address_token!`.
    crate::UdfFact<'static>,
    crate::UdfInstance<'static>,
    crate::UdfExternalAddress<'static>,
);

/// The list above is written by hand, so a new handle type can be added to the
/// crate without one. This finds that omission instead of waiting for a user to
/// discover it: every borrowing `pub struct` in `src/` must be named above.
#[cfg(test)]
#[test]
fn every_borrowing_public_type_has_a_thread_affinity_assertion() {
    let source_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
    let asserted = std::fs::read_to_string(source_dir.join("thread_safety.rs"))
        .expect("this file is readable");

    let mut missing = Vec::new();
    for entry in std::fs::read_dir(&source_dir).expect("src/ is readable") {
        let path = entry.expect("directory entry is readable").path();
        if path.extension().is_none_or(|extension| extension != "rs") {
            continue;
        }
        let source = std::fs::read_to_string(&path).expect("source file is readable");
        for line in source.lines() {
            // A lifetime parameter is what makes a type an environment-borrowing
            // handle; owned types like `WatchItem` need no assertion.
            let Some(rest) = line.trim_start().strip_prefix("pub struct ") else {
                continue;
            };
            let Some(name) = rest
                .split(['<', ' ', '{', '('])
                .next()
                .filter(|_| rest.contains('<'))
            else {
                continue;
            };
            // Macro bodies declare `pub struct $name<...>`; the types they
            // expand to are asserted under their real names instead.
            if name.starts_with('$') {
                continue;
            }
            if !asserted.contains(&format!("{name}<")) {
                missing.push(format!("{name} ({})", path.display()));
            }
        }
    }

    assert!(
        missing.is_empty(),
        "these borrowing public types have no !Send/!Sync assertion in \
         thread_safety.rs: {missing:?}"
    );
}