use super::{Type, TypeVar};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Row {
pub fields: HashMap<String, Type>,
pub rest: Option<TypeVar>,
}
impl Row {
pub fn empty() -> Self {
Self {
fields: HashMap::new(),
rest: None,
}
}
pub fn closed(fields: HashMap<String, Type>) -> Self {
Self {
fields,
rest: None,
}
}
pub fn open(fields: HashMap<String, Type>, rest: TypeVar) -> Self {
Self {
fields,
rest: Some(rest),
}
}
pub fn extend(&mut self, name: String, type_: Type) {
self.fields.insert(name, type_);
}
pub fn is_closed(&self) -> bool {
self.rest.is_none()
}
}
impl std::hash::Hash for Row {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
let mut sorted_fields: Vec<_> = self.fields.iter().collect();
sorted_fields.sort_by_key(|(k, _)| *k);
for (key, ty) in sorted_fields {
key.hash(state);
ty.hash(state);
}
self.rest.hash(state);
}
}