tola-caps 0.2.0

Capability system enabling type-level state tracking, trait detection, and stable specialization.
Documentation
//! Type-Level Dispatch Prototype V2
//!
//! Goal: Verify pure type-level dispatch on Stable Rust
//! No if branches, Debug and Release both are pure type calls
//!
//! Key insight: Use associated types instead of const generics

// =============================================================================
// Step 1: Bool types and If selector
// =============================================================================

pub struct True;
pub struct False;

pub trait Bool {
    type If<Then, Else>;
}

impl Bool for True {
    type If<Then, Else> = Then;
}

impl Bool for False {
    type If<Then, Else> = Else;
}

// =============================================================================
// Step 2: CapSet using associated types (not const bool!)
// =============================================================================

pub trait CapSet {
    /// Type-level bool for Clone capability
    type HasClone: Bool;
    /// Type-level bool for Copy capability
    type HasCopy: Bool;
}

// =============================================================================
// Step 3: Select Trait - core type selector
// =============================================================================

pub struct IsClone;
pub struct IsCopy;

/// Select implementation based on capability
pub trait Select<Query, Then, Else> {
    type Out;
}

// Select for IsClone query
impl<C, Then, Else> Select<IsClone, Then, Else> for C
where
    C: CapSet,
    C::HasClone: Bool,
{
    type Out = <C::HasClone as Bool>::If<Then, Else>;
}

// Note: We cannot have both impls for different Query types
// due to Rust's trait coherence rules. We need a different approach.

// =============================================================================
// Step 3b: Alternative - Query-specific traits
// =============================================================================

pub trait SelectClone<Then, Else> {
    type Out;
}

impl<C, Then, Else> SelectClone<Then, Else> for C
where
    C: CapSet,
{
    type Out = <C::HasClone as Bool>::If<Then, Else>;
}

pub trait SelectCopy<Then, Else> {
    type Out;
}

impl<C, Then, Else> SelectCopy<Then, Else> for C
where
    C: CapSet,
{
    type Out = <C::HasCopy as Bool>::If<Then, Else>;
}

// =============================================================================
// Step 4: Method implementations (as types)
// =============================================================================

/// Implementation trait for describe method
pub trait DescribeImpl<T: ?Sized> {
    fn describe(value: &T) -> &'static str;
}

/// Clone type implementation
pub struct CloneDescribe;
impl<T: ?Sized> DescribeImpl<T> for CloneDescribe {
    fn describe(_: &T) -> &'static str {
        "Has Clone (Type-Level)"
    }
}

/// Default implementation
pub struct DefaultDescribe;
impl<T: ?Sized> DescribeImpl<T> for DefaultDescribe {
    fn describe(_: &T) -> &'static str {
        "General (Type-Level)"
    }
}

// =============================================================================
// Step 5: Describe Trait using Type-Level Dispatch
// =============================================================================

pub trait Describe {
    fn describe(&self) -> &'static str;
}

impl<T> Describe for T
where
    T: CapSet,
    T: SelectClone<CloneDescribe, DefaultDescribe>,
    <T as SelectClone<CloneDescribe, DefaultDescribe>>::Out: DescribeImpl<T>,
{
    fn describe(&self) -> &'static str {
        // Purely type-level! No if!
        <T as SelectClone<CloneDescribe, DefaultDescribe>>::Out::describe(self)
    }
}

// =============================================================================
// Step 6: Test types
// =============================================================================

#[derive(Clone)]
struct HasClone;

impl CapSet for HasClone {
    type HasClone = True;
    type HasCopy = False;
}

struct NoClone;

impl CapSet for NoClone {
    type HasClone = False;
    type HasCopy = False;
}

#[derive(Clone, Copy)]
struct HasBoth;

impl CapSet for HasBoth {
    type HasClone = True;
    type HasCopy = True;
}

// =============================================================================
// Main
// =============================================================================

fn main() {
    println!("==================================================================");
    println!("       TYPE-LEVEL DISPATCH PROTOTYPE V2");
    println!("       No `if` - Pure Type Selection (Stable Rust!)");
    println!("==================================================================\n");

    let has_clone = HasClone;
    let no_clone = NoClone;
    let has_both = HasBoth;

    println!("Type-Level Dispatch Results:");
    println!("  HasClone:  {}", has_clone.describe());
    println!("  NoClone:   {}", no_clone.describe());
    println!("  HasBoth:   {}", has_both.describe());

    // Compile-time assertions
    assert_eq!(has_clone.describe(), "Has Clone (Type-Level)");
    assert_eq!(no_clone.describe(), "General (Type-Level)");
    assert_eq!(has_both.describe(), "Has Clone (Type-Level)");

    println!();
    println!("==================================================================");
    println!("         TYPE-LEVEL DISPATCH WORKS ON STABLE!");
    println!("         No if branches, pure type selection");
    println!("==================================================================");
}