use crate::borrow::Mutability;
use crate::scheduler::{AsLabel, Label};
use crate::storage::StorageId;
pub use crate::type_id::TypeId;
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Debug, Clone)]
pub struct WorkloadInfo {
#[allow(missing_docs)]
pub name: Box<dyn Label>,
#[allow(missing_docs)]
pub batch_info: Vec<BatchInfo>,
}
#[derive(Debug, Clone)]
pub struct BatchInfo {
#[allow(missing_docs)]
pub systems: (Option<SystemInfo>, Vec<SystemInfo>),
}
#[derive(Clone)]
pub struct SystemInfo {
#[allow(missing_docs)]
pub name: Box<dyn Label>,
#[allow(missing_docs)]
pub type_id: TypeId,
#[allow(missing_docs)]
pub borrow: Vec<TypeInfo>,
pub conflict: Option<Conflict>,
}
impl core::fmt::Debug for SystemInfo {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("StorageInfo")
.field("name", &self.name)
.field("borrow", &self.borrow)
.field("conflict", &self.conflict)
.finish()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Conflict {
Borrow {
#[allow(missing_docs)]
type_info: Option<TypeInfo>,
#[allow(missing_docs)]
other_system: SystemId,
#[allow(missing_docs)]
other_type_info: TypeInfo,
},
NotSendSync(TypeInfo),
OtherNotSendSync {
#[allow(missing_docs)]
system: SystemId,
#[allow(missing_docs)]
type_info: TypeInfo,
},
}
#[derive(Clone, Eq)]
pub struct SystemId {
#[allow(missing_docs)]
pub name: Box<dyn Label>,
#[allow(missing_docs)]
pub type_id: TypeId,
}
impl PartialEq for SystemId {
fn eq(&self, other: &Self) -> bool {
self.type_id == other.type_id
}
}
impl core::fmt::Debug for SystemId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!("{:?}", self.name))
}
}
#[derive(Clone, Eq)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct TypeInfo {
#[allow(missing_docs)]
pub name: Cow<'static, str>,
#[allow(missing_docs)]
pub mutability: Mutability,
#[allow(missing_docs)]
pub storage_id: StorageId,
#[allow(missing_docs)]
pub thread_safe: bool,
}
impl PartialEq for TypeInfo {
fn eq(&self, rhs: &Self) -> bool {
self.storage_id == rhs.storage_id && self.mutability == rhs.mutability
}
}
impl PartialEq<(TypeId, Mutability)> for TypeInfo {
fn eq(&self, rhs: &(TypeId, Mutability)) -> bool {
self.storage_id == rhs.0 && self.mutability == rhs.1
}
}
impl PartialOrd for TypeInfo {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for TypeInfo {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
match self.storage_id.cmp(&other.storage_id) {
core::cmp::Ordering::Equal => {}
ord => return ord,
}
self.mutability.cmp(&other.mutability)
}
}
impl core::fmt::Debug for TypeInfo {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut debug_struct = f.debug_struct("TypeInfo");
debug_struct
.field("name", &self.name)
.field("mutability", &self.mutability)
.field("thread_safe", &self.thread_safe)
.finish()
}
}
impl core::hash::Hash for TypeInfo {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.storage_id.hash(state);
self.mutability.hash(state);
}
}
#[allow(clippy::type_complexity)]
#[derive(Debug)]
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
pub struct WorkloadsTypeUsage(pub hashbrown::HashMap<String, Vec<(String, Vec<TypeInfo>)>>);
#[derive(Clone, Debug, Default)]
pub struct DedupedLabels(Vec<Box<dyn Label>>);
impl DedupedLabels {
pub(crate) fn new() -> DedupedLabels {
DedupedLabels(Vec::new())
}
pub(crate) fn with_capacity(capacity: usize) -> DedupedLabels {
DedupedLabels(Vec::with_capacity(capacity))
}
pub(crate) fn add<T>(&mut self, label: impl AsLabel<T>) -> bool {
let label = label.as_label();
if !self.0.contains(&label) {
self.0.push(label);
true
} else {
false
}
}
pub(crate) fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub(crate) fn iter(&self) -> RequirementsIter<'_> {
self.into_iter()
}
pub(crate) fn len(&self) -> usize {
self.0.len()
}
pub(crate) fn clear(&mut self) {
self.0.clear();
}
pub(crate) fn to_vec(&self) -> Vec<Box<dyn Label>> {
self.0.clone()
}
pub(crate) fn retain<F: FnMut(&Box<dyn Label>) -> bool>(&mut self, f: F) {
self.0.retain(f);
}
}
impl<'a> IntoIterator for &'a DedupedLabels {
type Item = &'a Box<dyn Label>;
type IntoIter = RequirementsIter<'a>;
fn into_iter(self) -> Self::IntoIter {
RequirementsIter(self.0.iter())
}
}
pub struct RequirementsIter<'a>(core::slice::Iter<'a, Box<dyn Label>>);
impl<'a> Iterator for RequirementsIter<'a> {
type Item = &'a Box<dyn Label>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
impl Extend<Box<dyn Label>> for DedupedLabels {
fn extend<T: IntoIterator<Item = Box<dyn Label>>>(&mut self, iter: T) {
for label in iter {
self.add(label);
}
}
}
impl<'a> Extend<&'a Box<dyn Label>> for DedupedLabels {
fn extend<T: IntoIterator<Item = &'a Box<dyn Label>>>(&mut self, iter: T) {
for label in iter {
self.add(label.clone());
}
}
}