Skip to content

Commit

Permalink
replace trait based systems by function based ones
Browse files Browse the repository at this point in the history
replace borrowing types, from reference to views
impl Iterator for all Shiperator
make the proc macro opt-in
remove prelude and internal modules
modify RunWorkload error
add Run error
add system and try_system macros
fix #75
fix #30
fix #17
  • Loading branch information
leudz committed Apr 22, 2020
1 parent 0293052 commit f31d965
Show file tree
Hide file tree
Showing 120 changed files with 2,234 additions and 2,078 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ parking_lot = { version = "^0.10" }
hashbrown = "^0.7"

[features]
default = ["parallel", "proc"]
default = ["parallel"]
parallel = ["rayon", "num_cpus", "std"]
proc = ["shipyard_proc"]
non_send = ["std"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ If you are new here, the [user guide](https://leudz.github.io/shipyard/book) is

## Simple Example
```rust
use shipyard::prelude::*;
use shipyard::*;

struct Health(f32);
struct Position { x: f32, y: f32 }
Expand Down Expand Up @@ -91,7 +91,7 @@ There is still a lot of room for optimization, the current focus is more on addi
## Features

- **parallel** *(default)* — adds parallel iterators and dispatch
- **proc** *(default)* — adds `system` proc macro
- **proc** — adds `system` proc macro
- **serde** — adds (de)serialization support with [serde](https://github.com/serde-rs/serde)
- **non_send** — add methods and types required to work with `!Send` components
- **non_sync** — add methods and types required to work with `!Sync` components
Expand Down
2 changes: 1 addition & 1 deletion demo/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use awsm_web::webgl::{
};
use awsm_web::window::get_window_size;
use gloo_events::EventListener;
use shipyard::prelude::*;
use shipyard::*;
use std::cell::RefCell;
use std::rc::Rc;
use wasm_bindgen::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion demo/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::components::*;

use gloo_events::EventListener;
use shipyard::prelude::*;
use shipyard::*;
use std::rc::Rc;
use web_sys::HtmlCanvasElement;

Expand Down
2 changes: 1 addition & 1 deletion demo/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::components::*;
use crate::config::*;
use crate::geometry::*;
use rand::prelude::*;
use shipyard::prelude::*;
use shipyard::*;

pub const TICK: &str = "TICK";

Expand Down
2 changes: 1 addition & 1 deletion demo/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::components::*;
use crate::geometry::*;
use crate::hud::Hud;
use crate::renderer::SceneRenderer;
use shipyard::prelude::*;
use shipyard::*;

pub fn init_world(img_area: Area, stage_area: Area, hud: Hud, renderer: SceneRenderer) -> World {
let world = World::default();
Expand Down
12 changes: 6 additions & 6 deletions shipyard_proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ fn expand_system(name: syn::Ident, mut run: syn::ItemFn) -> Result<TokenStream>
// transform &Entities into Entites and &mut Entities into EntitiesMut
if path.path.segments.last().unwrap().ident == "Entities" {
if reference.mutability.is_some() {
**ty = parse_quote!(::shipyard::prelude::EntitiesMut);
**ty = parse_quote!(::shipyard::EntitiesMut);
exclusive_borrows.push((i, parse_quote!(Entities)));
} else {
**ty = parse_quote!(::shipyard::prelude::Entities);
**ty = parse_quote!(::shipyard::Entities);
shared_borrows.push((i, parse_quote!(Entities)));
}

Expand All @@ -101,7 +101,7 @@ fn expand_system(name: syn::Ident, mut run: syn::ItemFn) -> Result<TokenStream>
"You probably forgot a mut, &AllStorages isn't a valid storage access"
));
} else {
**ty = parse_quote!(::shipyard::prelude::AllStorages);
**ty = parse_quote!(::shipyard::AllStorages);

match &mut conflict {
Conflict::AllStorages(all_storages) => {
Expand All @@ -118,7 +118,7 @@ fn expand_system(name: syn::Ident, mut run: syn::ItemFn) -> Result<TokenStream>
}
} else if path.path.segments.last().unwrap().ident == "ThreadPool" {
if reference.mutability.is_none() {
**ty = parse_quote!(::shipyard::prelude::ThreadPool);
**ty = parse_quote!(::shipyard::ThreadPool);
} else {
return Err(Error::new_spanned(
path,
Expand Down Expand Up @@ -557,9 +557,9 @@ fn expand_system(name: syn::Ident, mut run: syn::ItemFn) -> Result<TokenStream>

Ok(quote! {
#vis struct #name;
impl<'sys> ::shipyard::prelude::System<'sys> for #name {
impl<'sys> ::shipyard::System<'sys> for #name {
type Data = (#(#data,)*);
fn run((#(#binding,)*): (#(<#data as ::shipyard::prelude::SystemData<'sys>>::View,)*)) #body
fn run((#(#binding,)*): (#(<#data as ::shipyard::SystemData<'sys>>::View,)*)) #body
}
})
}
Expand Down
25 changes: 8 additions & 17 deletions src/atomic_refcell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,14 @@ impl<T: ?Sized> AtomicRefCell<T> {
where
T: Sized,
{
#[cfg(feature = "std")]
{
AtomicRefCell {
inner: UnsafeCell::new(value),
borrow_state: Default::default(),
is_sync,
send,
_non_send: core::marker::PhantomData,
}
}
#[cfg(not(feature = "std"))]
{
AtomicRefCell {
inner: UnsafeCell::new(value),
borrow_state: Default::default(),
_non_send: core::marker::PhantomData,
}
AtomicRefCell {
borrow_state: Default::default(),
#[cfg(feature = "std")]
send,
#[cfg(feature = "std")]
is_sync,
_non_send: core::marker::PhantomData,
inner: UnsafeCell::new(value),
}
}
/// Immutably borrows the wrapped value, returning an error if the value is currently mutably
Expand Down
172 changes: 172 additions & 0 deletions src/borrow/all_storages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//use super::FakeBorrow;
use crate::error;
use crate::storage::AllStorages;
use crate::view::{EntitiesView, EntitiesViewMut, UniqueView, UniqueViewMut, View, ViewMut};
#[cfg(feature = "non_send")]
use crate::NonSend;
#[cfg(all(feature = "non_send", feature = "non_sync"))]
use crate::NonSendSync;
#[cfg(feature = "non_sync")]
use crate::NonSync;
use core::convert::TryInto;

pub trait AllStoragesBorrow<'a> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage>
where
Self: Sized;
}

impl<'a> AllStoragesBorrow<'a> for () {
fn try_borrow(_: &'a AllStorages) -> Result<Self, error::GetStorage>
where
Self: Sized,
{
Ok(())
}
}

impl<'a> AllStoragesBorrow<'a> for EntitiesView<'a> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
all_storages.try_into()
}
}

impl<'a> AllStoragesBorrow<'a> for EntitiesViewMut<'a> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
all_storages.try_into()
}
}

impl<'a, T: 'static + Send + Sync> AllStoragesBorrow<'a> for View<'a, T> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
all_storages.try_into()
}
}

impl<'a, T: 'static + Send + Sync> AllStoragesBorrow<'a> for ViewMut<'a, T> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
all_storages.try_into()
}
}

impl<'a, T: 'static + Send + Sync> AllStoragesBorrow<'a> for UniqueView<'a, T> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
all_storages.try_into()
}
}

impl<'a, T: 'static + Send + Sync> AllStoragesBorrow<'a> for UniqueViewMut<'a, T> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
all_storages.try_into()
}
}

#[cfg(feature = "non_send")]
impl<'a, T: 'static + Sync> AllStoragesBorrow<'a> for NonSend<View<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
View::try_storage_from_non_send(all_storages).map(|view| NonSend(view))
}
}

#[cfg(feature = "non_send")]
impl<'a, T: 'static + Sync> AllStoragesBorrow<'a> for NonSend<ViewMut<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
ViewMut::try_storage_from_non_send(all_storages).map(|view| NonSend(view))
}
}

#[cfg(feature = "non_sync")]
impl<'a, T: 'static + Send> AllStoragesBorrow<'a> for NonSync<View<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
View::try_storage_from_non_sync(all_storages).map(|view| NonSync(view))
}
}

#[cfg(feature = "non_sync")]
impl<'a, T: 'static + Send> AllStoragesBorrow<'a> for NonSync<ViewMut<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
ViewMut::try_storage_from_non_sync(all_storages).map(|view| NonSync(view))
}
}

#[cfg(all(feature = "non_send", feature = "non_sync"))]
impl<'a, T: 'static + Sync> AllStoragesBorrow<'a> for NonSendSync<View<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
View::try_storage_from_non_send_sync(all_storages).map(|view| NonSendSync(view))
}
}

#[cfg(all(feature = "non_send", feature = "non_sync"))]
impl<'a, T: 'static + Sync> AllStoragesBorrow<'a> for NonSendSync<ViewMut<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
ViewMut::try_storage_from_non_send_sync(all_storages).map(|view| NonSendSync(view))
}
}

#[cfg(feature = "non_send")]
impl<'a, T: 'static + Sync> AllStoragesBorrow<'a> for NonSend<UniqueView<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
UniqueView::try_storage_from_non_send(all_storages).map(|view| NonSend(view))
}
}

#[cfg(feature = "non_send")]
impl<'a, T: 'static + Sync> AllStoragesBorrow<'a> for NonSend<UniqueViewMut<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
UniqueViewMut::try_storage_from_non_send(all_storages).map(|view| NonSend(view))
}
}

#[cfg(feature = "non_sync")]
impl<'a, T: 'static + Send> AllStoragesBorrow<'a> for NonSync<UniqueView<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
UniqueView::try_storage_from_non_sync(all_storages).map(|view| NonSync(view))
}
}

#[cfg(feature = "non_sync")]
impl<'a, T: 'static + Send> AllStoragesBorrow<'a> for NonSync<UniqueViewMut<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
UniqueViewMut::try_storage_from_non_sync(all_storages).map(|view| NonSync(view))
}
}

#[cfg(all(feature = "non_send", feature = "non_sync"))]
impl<'a, T: 'static + Sync> AllStoragesBorrow<'a> for NonSendSync<UniqueView<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
UniqueView::try_storage_from_non_send_sync(all_storages).map(|view| NonSendSync(view))
}
}

#[cfg(all(feature = "non_send", feature = "non_sync"))]
impl<'a, T: 'static + Sync> AllStoragesBorrow<'a> for NonSendSync<UniqueViewMut<'a, T>> {
fn try_borrow(all_storages: &'a AllStorages) -> Result<Self, error::GetStorage> {
UniqueViewMut::try_storage_from_non_send_sync(all_storages).map(|view| NonSendSync(view))
}
}

macro_rules! impl_system_data {
($(($type: ident, $index: tt))+) => {
impl<'a, $($type: AllStoragesBorrow<'a>),+> AllStoragesBorrow<'a> for ($($type,)+) {
fn try_borrow(
all_storages: &'a AllStorages,
) -> Result<Self, error::GetStorage> {
Ok(($(
<$type as AllStoragesBorrow>::try_borrow(all_storages)?,
)+))
}
}
}
}

macro_rules! system_data {
($(($type: ident, $index: tt))*;($type1: ident, $index1: tt) $(($queue_type: ident, $queue_index: tt))*) => {
impl_system_data![$(($type, $index))*];
system_data![$(($type, $index))* ($type1, $index1); $(($queue_type, $queue_index))*];
};
($(($type: ident, $index: tt))*;) => {
impl_system_data![$(($type, $index))*];
}
}

system_data![(A, 0); (B, 1) (C, 2) (D, 3) (E, 4) (F, 5) (G, 6) (H, 7) (I, 8) (J, 9)];
9 changes: 9 additions & 0 deletions src/borrow/fake_borrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use core::marker::PhantomData;

pub struct FakeBorrow<T: ?Sized>(PhantomData<T>);

impl<T: ?Sized> FakeBorrow<T> {
pub(crate) fn new() -> Self {
FakeBorrow(PhantomData)
}
}
Loading

0 comments on commit f31d965

Please sign in to comment.