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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
use std::{
rc::Rc,
cell::RefCell,
};
use crate::common::{
stamp::stamp,
lambda::Lambda,
data::Data,
};
// TODO: take a reference to lambda?
/// Wraps a `Lambda` with some scope context.
/// Each closure is unique when constructed,
/// Because it depends on the surrounding environment it was constructed in.
/// It holds a set of references to variables it captures.
#[derive(Debug, Clone, PartialEq)]
pub struct Closure {
pub id: String,
pub lambda: Rc<Lambda>,
pub captures: Vec<Rc<RefCell<Data>>>,
}
impl Closure {
/// Constructs a new `Closure` by wrapping a `Lambda`.
/// This closure has no captured variables when constructed.
pub fn wrap(lambda: Rc<Lambda>) -> Closure {
Closure {
id: stamp(0),
lambda,
captures: vec![],
}
}
}
#[cfg(test)]
mod tests {
// use super::*;
// use crate::common::lambda::Lambda;
// #[test]
// fn unique() {
// let lambda = Lambda::empty();
// let a = Closure::wrap(lambda.clone());
// let b = Closure::wrap(lambda.clone());
//
// assert_ne!(a.id, b.id);
// }
}