Skip to content

Commit

Permalink
Make a few things public to enable users to create custom systems
Browse files Browse the repository at this point in the history
  • Loading branch information
leudz committed Jun 9, 2024
1 parent a007a27 commit 60a8baa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/borrow/borrow_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::error;
use crate::scheduler::TypeInfo;
use crate::sparse_set::SparseSet;
use crate::storage::StorageId;
use crate::system::Nothing;
use crate::tracking::{Track, Tracking};
use crate::unique::UniqueStorage;
use crate::views::{
Expand Down Expand Up @@ -66,6 +67,12 @@ pub unsafe trait BorrowInfo {
);
}

// this is needed for downstream crates to impl IntoWorkloadSystem
unsafe impl BorrowInfo for Nothing {
fn borrow_info(_: &mut Vec<TypeInfo>) {}
fn enable_tracking(_: &mut Vec<fn(&AllStorages) -> Result<(), error::GetStorage>>) {}
}

unsafe impl BorrowInfo for () {
fn borrow_info(_: &mut Vec<TypeInfo>) {}
fn enable_tracking(_: &mut Vec<fn(&AllStorages) -> Result<(), error::GetStorage>>) {}
Expand Down
15 changes: 15 additions & 0 deletions src/borrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::atomic_refcell::{ARef, ARefMut, SharedBorrow};
use crate::component::{Component, Unique};
use crate::error;
use crate::sparse_set::SparseSet;
use crate::system::Nothing;
use crate::tracking::{Track, Tracking, TrackingTimestamp};
use crate::unique::UniqueStorage;
use crate::views::{EntitiesView, EntitiesViewMut, UniqueView, UniqueViewMut, View, ViewMut};
Expand Down Expand Up @@ -86,6 +87,20 @@ pub trait Borrow {
) -> Result<Self::View<'a>, error::GetStorage>;
}

// this is needed for downstream crate to impl System
impl Borrow for Nothing {
type View<'a> = ();

fn borrow<'a>(
_all_storages: &'a AllStorages,
_all_borrow: Option<SharedBorrow<'a>>,
_last_run: Option<TrackingTimestamp>,
_current: TrackingTimestamp,
) -> Result<Self::View<'a>, error::GetStorage> {
Ok(())
}
}

impl Borrow for () {
type View<'a> = ();

Expand Down
40 changes: 25 additions & 15 deletions src/scheduler/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,32 @@ use alloc::vec::Vec;
/// [`Workload`]: crate::Workload
#[allow(clippy::type_complexity)]
pub struct WorkloadSystem {
#[doc(hidden)]
pub(crate) type_id: TypeId,
pub(crate) display_name: Box<dyn Label>,
pub(crate) system_fn: Box<dyn Fn(&World) -> Result<(), error::Run> + Send + Sync + 'static>,
#[allow(missing_docs)]
pub type_id: TypeId,
#[allow(missing_docs)]
pub display_name: Box<dyn Label>,
#[allow(missing_docs)]
pub system_fn: Box<dyn Fn(&World) -> Result<(), error::Run> + Send + Sync + 'static>,
/// access information
pub(crate) borrow_constraints: Vec<TypeInfo>,
pub(crate) tracking_to_enable: Vec<fn(&AllStorages) -> Result<(), error::GetStorage>>,
pub(crate) generator: Box<dyn Fn(&mut Vec<TypeInfo>) -> TypeId + Send + Sync + 'static>,
pub(crate) run_if:
Option<Box<dyn Fn(&World) -> Result<bool, error::Run> + Send + Sync + 'static>>,
pub(crate) tags: Vec<Box<dyn Label>>,
pub(crate) before_all: DedupedLabels,
pub(crate) after_all: DedupedLabels,
pub(crate) require_in_workload: DedupedLabels,
pub(crate) require_before: DedupedLabels,
pub(crate) require_after: DedupedLabels,
pub borrow_constraints: Vec<TypeInfo>,
/// Generates the tracking to enable for this system's views
pub tracking_to_enable: Vec<fn(&AllStorages) -> Result<(), error::GetStorage>>,
/// Generates constraints and system type id
pub generator: Box<dyn Fn(&mut Vec<TypeInfo>) -> TypeId + Send + Sync + 'static>,
#[allow(missing_docs)]
pub run_if: Option<Box<dyn Fn(&World) -> Result<bool, error::Run> + Send + Sync + 'static>>,
#[allow(missing_docs)]
pub tags: Vec<Box<dyn Label>>,
#[allow(missing_docs)]
pub before_all: DedupedLabels,
#[allow(missing_docs)]
pub after_all: DedupedLabels,
#[allow(missing_docs)]
pub require_in_workload: DedupedLabels,
#[allow(missing_docs)]
pub require_before: DedupedLabels,
#[allow(missing_docs)]
pub require_after: DedupedLabels,
}

impl Extend<WorkloadSystem> for Workload {
Expand Down
3 changes: 2 additions & 1 deletion src/type_id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use core::hash::{Hash, Hasher};
pub struct TypeId(pub(crate) u128);

impl TypeId {
pub(crate) fn of<T: ?Sized + 'static>() -> Self {
/// Returns the `TypeId` of the type this generic function has been instantiated with.
pub fn of<T: ?Sized + 'static>() -> Self {
core::any::TypeId::of::<T>().into()
}
#[cfg(test)]
Expand Down

0 comments on commit 60a8baa

Please sign in to comment.