knave 0.11.9

A Lisp programming language for humans and agents that infers types and ownership and compiles to readable Rust
Documentation
use crate::types::ClosureKind;

#[derive(Clone, Copy)]
pub(super) enum FunctionBorrow {
    Shared,
    Mutable,
}

impl FunctionBorrow {
    pub(super) fn of(kind: ClosureKind) -> Self {
        match kind {
            ClosureKind::Fn => Self::Shared,
            ClosureKind::FnMut | ClosureKind::FnOnce => Self::Mutable,
        }
    }

    pub(super) fn prefix(self) -> &'static str {
        match self {
            Self::Shared => "&",
            Self::Mutable => "&mut ",
        }
    }

    pub(super) fn trait_object(self, binder: &str) -> String {
        let callable = match self {
            Self::Shared => ClosureKind::Fn,
            Self::Mutable => ClosureKind::FnMut,
        };
        format!("{}dyn {binder}{}", self.prefix(), callable.keyword())
    }
}