use std::any::TypeId;
use std::fmt::Debug;
use serde::Serialize;
use crate::entity::property_store::get_property_dependents_static;
use crate::entity::{Entity, EntityId};
use crate::hashing::hash_serialized_128;
use crate::{Context, HashSet};
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum PropertyInitializationKind {
Explicit,
Derived,
Constant,
}
pub trait AnyProperty: Copy + Debug + PartialEq + Serialize + 'static {}
impl<T> AnyProperty for T where T: Copy + Debug + PartialEq + Serialize + 'static {}
pub trait Property<E: Entity>: AnyProperty {
type CanonicalValue: AnyProperty;
fn name() -> &'static str {
let full = std::any::type_name::<Self>();
full.rsplit("::").next().unwrap()
}
#[must_use]
fn initialization_kind() -> PropertyInitializationKind;
#[must_use]
#[inline]
fn is_derived() -> bool {
Self::initialization_kind() == PropertyInitializationKind::Derived
}
#[must_use]
#[inline]
fn is_required() -> bool {
Self::initialization_kind() == PropertyInitializationKind::Explicit
}
#[must_use]
fn compute_derived(context: &Context, entity_id: EntityId<E>) -> Self;
#[must_use]
fn default_const() -> Self;
#[must_use]
fn make_canonical(self) -> Self::CanonicalValue;
#[must_use]
fn make_uncanonical(value: Self::CanonicalValue) -> Self;
#[must_use]
fn get_display(&self) -> String;
#[must_use]
fn hash_property_value(value: &Self::CanonicalValue) -> u128 {
hash_serialized_128(value)
}
#[must_use]
fn type_id() -> TypeId {
TypeId::of::<Self>()
}
fn id() -> usize;
fn index_id() -> usize {
Self::id()
}
fn non_derived_dependencies() -> Vec<usize> {
let mut result = HashSet::default();
Self::collect_non_derived_dependencies(&mut result);
result.into_iter().collect()
}
fn collect_non_derived_dependencies(result: &mut HashSet<usize>);
fn dependents() -> &'static [usize] {
get_property_dependents_static::<E>(Self::id())
}
}