tinymist_analysis/ty/
select.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use super::{Iface, IfaceChecker};
use crate::ty::def::*;

pub trait SelectChecker: TyCtx {
    fn select(&mut self, sig: Iface, key: &Interned<str>, pol: bool);
}

impl Ty {
    /// Select the given type with the given key.
    pub fn select(&self, key: &Interned<str>, pol: bool, checker: &mut impl SelectChecker) {
        SelectKeyChecker(checker, key).ty(self, pol);
    }
}

#[derive(BindTyCtx)]
#[bind(0)]
pub struct SelectKeyChecker<'a, T: TyCtx>(&'a mut T, &'a Interned<str>);

impl<T: SelectChecker> SelectKeyChecker<'_, T> {
    fn ty(&mut self, ty: &Ty, pol: bool) {
        ty.iface_surface(pol, self)
    }
}

impl<T: SelectChecker> IfaceChecker for SelectKeyChecker<'_, T> {
    fn check(
        &mut self,
        iface: Iface,
        _ctx: &mut super::IfaceCheckContext,
        pol: bool,
    ) -> Option<()> {
        self.0.select(iface, self.1, pol);
        Some(())
    }
}