//! This module is included while awaiting an upstream merge in stretch proper. //! You should not rely on it, and consider it an implementation detail. use core::any::Any; use std::collections::HashMap; use std::ops::Drop; use std::sync::Mutex; use lazy_static::lazy_static; use crate::stretch::geometry::Size; use crate::stretch::id; use crate::stretch::number::Number; use crate::stretch::result::{Cache, Layout}; use crate::stretch::style::*; use crate::stretch::Error; type MeasureFunc = Box) -> Result, Box> + Send + Sync + 'static>; lazy_static! { /// Global stretch instance id allocator. static ref INSTANCE_ALLOCATOR: Mutex = Mutex::new(id::Allocator::new()); } #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub struct Node { instance: id::Id, local: id::Id, } pub(crate) struct Storage(HashMap); impl Storage { pub fn new() -> Self { Storage(HashMap::new()) } pub fn get(&self, node: Node) -> Result<&T, Error> { match self.0.get(&node) { Some(v) => Ok(v), None => Err(Error::InvalidNode(node)), } } pub fn get_mut(&mut self, node: Node) -> Result<&mut T, Error> { match self.0.get_mut(&node) { Some(v) => Ok(v), None => Err(Error::InvalidNode(node)), } } pub fn insert(&mut self, node: Node, value: T) -> Option { self.0.insert(node, value) } } impl std::ops::Index<&Node> for Storage { type Output = T; fn index(&self, idx: &Node) -> &T { &(self.0)[idx] } } pub struct Stretch { id: id::Id, nodes: id::Allocator, pub(crate) style: Storage