BREP_boolean 0.34.1

Rust B-rep CAD kernel (next.BREP.io): booleans and healing: imprint, fragment, boolean assembly, sewing and face merging.
Documentation
//! Dedicated booleans: routes a crate that builds a kind of solid registers
//! for booleans of two such solids (the swept tori of `BREP_construction`,
//! `BREP_SWEEP_TUBE_BOOLEAN`). A route recognises its operands itself and
//! answers `None` for any other pair. That is a miss, and the general boolean
//! runs as before. With no route registered nothing changes.
use super::{BooleanOperation, BrepSolid, KernelRefusal};

/// A dedicated boolean: `None` where it does not recognise both operands.
pub type DedicatedBoolean = fn(&BrepSolid, &BrepSolid, BooleanOperation) -> Option<Result<BrepSolid, KernelRefusal>>;

static ROUTES: std::sync::RwLock<Vec<DedicatedBoolean>> = std::sync::RwLock::new(Vec::new());

/// Register `route` (once; registering it again is a no-op).
pub fn register_dedicated_boolean(route: DedicatedBoolean) {
    if let Ok(mut routes) = ROUTES.write() {
        if !routes.iter().any(|known| *known as usize == route as usize) {
            routes.push(route);
        }
    }
}

/// The switch every dedicated route runs under (`BREP_SWEEP_TUBE_BOOLEAN`,
/// default off). Off, a boolean reads only this cached flag here: the
/// registry is not read and the exact-sections scope is not consulted.
pub fn dedicated_booleans_on() -> bool {
    crate::switch!("BREP_SWEEP_TUBE_BOOLEAN", false)
}

/// The first registered route's answer for this pair, if any recognises it.
pub(super) fn dedicated(first: &BrepSolid, second: &BrepSolid, operation: BooleanOperation) -> Option<Result<BrepSolid, KernelRefusal>> {
    if !dedicated_booleans_on() {
        return None;
    }
    let routes: Vec<DedicatedBoolean> = ROUTES.read().ok()?.clone();
    routes.into_iter().find_map(|route| route(first, second, operation))
}