use super::{Expression, Visitor};
use crate::Tuple;
use std::marker::PhantomData;
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
pub struct ViewRef(pub(crate) i32);
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct View<T, E>
where
T: Tuple,
E: Expression<T>,
{
reference: ViewRef,
view_deps: Vec<ViewRef>,
_phantom: PhantomData<(T, E)>,
}
impl<T, E> View<T, E>
where
T: Tuple,
E: Expression<T>,
{
pub(crate) fn new(reference: ViewRef) -> Self {
Self {
view_deps: vec![reference.clone()],
reference,
_phantom: PhantomData,
}
}
#[inline(always)]
pub(crate) fn reference(&self) -> &ViewRef {
&self.reference
}
#[inline(always)]
pub(crate) fn view_deps(&self) -> &[ViewRef] {
&self.view_deps
}
}
impl<T, E> Expression<T> for View<T, E>
where
T: Tuple + 'static,
E: Expression<T> + 'static,
{
fn visit<V>(&self, visitor: &mut V)
where
V: Visitor,
{
visitor.visit_view(&self);
}
}
#[cfg(test)]
mod tests {
use crate::{Database, Tuples};
#[test]
fn test_clone() {
let mut database = Database::new();
let r = database.add_relation::<i32>("r").unwrap();
let v = database.store_view(r.clone()).unwrap().clone();
database.insert(&r, vec![1, 2, 3].into()).unwrap();
assert_eq!(
Tuples::<i32>::from(vec![1, 2, 3]),
database.evaluate(&v).unwrap()
);
}
}