From 1c1e5bd46ba18b242c08228787ec83e2defeba2d Mon Sep 17 00:00:00 2001 From: Freja Roberts Date: Mon, 12 Feb 2024 00:30:01 +0100 Subject: [PATCH 1/2] chore: update ci toolchain --- .github/workflows/ci.yml | 71 ++++++------------------------------ .github/workflows/deploy.yml | 4 +- 2 files changed, 14 insertions(+), 61 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index af2cbfcf..4cc45f15 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -14,112 +14,65 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Install nightly - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - profile: minimal - + - uses: dtolnay/rust-toolchain@stable - uses: taiki-e/install-action@nextest - name: Run cargo test - uses: actions-rs/cargo@v1 - with: - command: nextest - args: run --all-features + run: cargo nextest run --all-features test_miri: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Install nightly - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@master with: toolchain: nightly - override: true components: miri - uses: taiki-e/install-action@nextest - name: Run cargo miri - uses: actions-rs/cargo@v1 - with: - command: miri - args: nextest run -j16 --no-default-features --features std,serde,flume,derive + run: cargo miri nextest run -j16 --no-default-features --features std,serde,flume,derive test_nostd: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Install nightly - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - profile: minimal - + - uses: dtolnay/rust-toolchain@stable - uses: taiki-e/install-action@nextest - name: Run cargo test - uses: actions-rs/cargo@v1 - with: - command: nextest - args: run --no-default-features + run: cargo nextest run --no-default-features lint_doc: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Install nightly - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - profile: minimal - + - uses: dtolnay/rust-toolchain@stable - name: Run cargo test - uses: actions-rs/cargo@v1 - with: - command: doc - args: --all-features + run: cargo doc --all-features test_doc: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Install nightly - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - profile: minimal - + - uses: dtolnay/rust-toolchain@stable - name: Run cargo test (doc) - uses: actions-rs/cargo@v1 - with: - command: test - args: --doc --all-features + run: cargo test --doc --all-features coverage: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - name: Install nightly - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@master with: toolchain: nightly - override: true - profile: minimal components: miri - uses: taiki-e/install-action@cargo-tarpaulin - name: Run cargo tarpaulin - uses: actions-rs/cargo@v1 - with: - command: tarpaulin - args: --all-features --out Xml --engine llvm + run: cargo tarpaulin --all-features --out Xml --engine llvm - name: Upload to codecov.io uses: codecov/codecov-action@v2 diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 6dfffe5e..0c3a3e96 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -13,9 +13,9 @@ jobs: if: github.event_name == 'push' steps: - uses: actions/checkout@v2 - - uses: dtolnay/rust-toolchain@1.70.0 + - uses: dtolnay/rust-toolchain@master with: - target: wasm32-unknown-unknown + targets: wasm32-unknown-unknown - run: cargo build --release --package asteroids --target wasm32-unknown-unknown --manifest-path=asteroids/Cargo.toml - run: mv ./target/wasm32-unknown-unknown/release/asteroids.wasm ./asteroids/public - name: Upload Artefact From adb0ff8868e5c1146793cc48ff101343fc74d6f6 Mon Sep 17 00:00:00 2001 From: Freja Roberts Date: Mon, 12 Feb 2024 15:55:50 +0100 Subject: [PATCH 2/2] fix: lazy component buffer in static memory --- src/component.rs | 7 +------ src/macros.rs | 51 ++++++++------------------------------------- src/metadata/mod.rs | 2 +- src/vtable.rs | 6 +++--- src/world.rs | 4 ++-- tests/components.rs | 17 ++++++++------- 6 files changed, 25 insertions(+), 62 deletions(-) diff --git a/src/component.rs b/src/component.rs index 30598c84..cfb2c6cc 100644 --- a/src/component.rs +++ b/src/component.rs @@ -286,11 +286,6 @@ impl Component { pub fn name(&self) -> &'static str { self.vtable.name } - - /// Returns all metadata components - pub fn get_meta(&self) -> ComponentBuffer { - self.vtable.meta.get(self.desc()) - } } impl Metadata for Component { @@ -451,7 +446,7 @@ impl ComponentDesc { self.key.target.is_some() } - pub(crate) fn get_meta(&self) -> ComponentBuffer { + pub(crate) fn create_meta(&self) -> ComponentBuffer { self.vtable.meta.get(*self) } diff --git a/src/macros.rs b/src/macros.rs index d3efa061..be456e56 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -109,27 +109,11 @@ macro_rules! component { $(#[$outer])* $vis fn $name($obj: $crate::Entity) -> $crate::Component<$ty> { - static COMPONENT_ID: ::core::sync::atomic::AtomicU32 = ::core::sync::atomic::AtomicU32::new($crate::entity::EntityIndex::MAX); - fn meta(_desc: $crate::component::ComponentDesc) -> $crate::buffer::ComponentBuffer { - let mut _buffer = $crate::buffer::ComponentBuffer::new(); - - <$crate::metadata::Name as $crate::metadata::Metadata<$ty>>::attach(_desc, &mut _buffer); - <$crate::Component<$ty> as $crate::metadata::Metadata<$ty>>::attach(_desc, &mut _buffer); - - $( - $( - <$metadata as $crate::metadata::Metadata::<$ty>>::attach(_desc, &mut _buffer); - )* - )* - - _buffer - } - - static META: $crate::vtable::LazyComponentBuffer = $crate::vtable::LazyComponentBuffer::new(meta); - static VTABLE: &$crate::vtable::ComponentVTable<$ty> = - &$crate::vtable::ComponentVTable::new(stringify!($name), &META); use $crate::entity::EntityKind; use $crate::relation::RelationExt; + + static COMPONENT_ID: ::core::sync::atomic::AtomicU32 = ::core::sync::atomic::AtomicU32::new($crate::entity::EntityIndex::MAX); + static VTABLE: &$crate::vtable::ComponentVTable<$ty> = $crate::component_vtable!($name: $ty $(=> [$($metadata),*])?); $crate::Component::static_init(&COMPONENT_ID, EntityKind::COMPONENT, VTABLE).of($obj) } @@ -142,26 +126,10 @@ macro_rules! component { $(#[$outer])* $vis fn $name() -> $crate::Component<$ty> { - static COMPONENT_ID: ::core::sync::atomic::AtomicU32 = ::core::sync::atomic::AtomicU32::new($crate::entity::EntityIndex::MAX); - fn meta(_desc: $crate::component::ComponentDesc) -> $crate::buffer::ComponentBuffer { - let mut _buffer = $crate::buffer::ComponentBuffer::new(); - - <$crate::metadata::Name as $crate::metadata::Metadata<$ty>>::attach(_desc, &mut _buffer); - <$crate::Component<$ty> as $crate::metadata::Metadata<$ty>>::attach(_desc, &mut _buffer); - - $( - $( - <$metadata as $crate::metadata::Metadata::<$ty>>::attach(_desc, &mut _buffer); - )* - )* - - _buffer - } - - static META: $crate::vtable::LazyComponentBuffer = $crate::vtable::LazyComponentBuffer::new(meta); - static VTABLE: &$crate::vtable::ComponentVTable<$ty> = - &$crate::vtable::ComponentVTable::new(stringify!($name), &META); use $crate::entity::EntityKind; + + static COMPONENT_ID: ::core::sync::atomic::AtomicU32 = ::core::sync::atomic::AtomicU32::new($crate::entity::EntityIndex::MAX); + static VTABLE: &$crate::vtable::ComponentVTable<$ty> = $crate::component_vtable!($name: $ty $(=> [$($metadata),*])?); $crate::Component::static_init(&COMPONENT_ID, EntityKind::COMPONENT, VTABLE) } @@ -204,11 +172,10 @@ macro_rules! component_vtable { } - static META: $crate::vtable::LazyComponentBuffer = $crate::vtable::LazyComponentBuffer::new(meta); - static VTABLE: &$crate::vtable::ComponentVTable<$ty> = - &$crate::vtable::ComponentVTable::new(stringify!($name), &META); + static VTABLE: $crate::vtable::ComponentVTable<$ty> = + $crate::vtable::ComponentVTable::new(stringify!($name), $crate::vtable::LazyComponentBuffer::new(meta)); - VTABLE + &VTABLE } }; diff --git a/src/metadata/mod.rs b/src/metadata/mod.rs index 2073f885..f6579a5e 100644 --- a/src/metadata/mod.rs +++ b/src/metadata/mod.rs @@ -46,7 +46,7 @@ mod test { foo: String => [crate::Debuggable], } - let meta = foo().get_meta(); + let meta = foo().desc().create_meta(); assert!(meta.get(debuggable()).is_some()); assert_eq!(meta.get(name()), Some(&"foo".into())); diff --git a/src/vtable.rs b/src/vtable.rs index 9d9f2afd..a37124a3 100644 --- a/src/vtable.rs +++ b/src/vtable.rs @@ -43,7 +43,7 @@ pub struct UntypedVTable { pub(crate) dangling: fn() -> NonNull, /// A metadata is a component which is attached to the component, such as /// metadata or name - pub(crate) meta: &'static LazyComponentBuffer, + pub(crate) meta: LazyComponentBuffer, } impl UntypedVTable { @@ -55,7 +55,7 @@ impl UntypedVTable { /// Creates a new vtable of type `T` pub(crate) const fn new( name: &'static str, - meta: &'static LazyComponentBuffer, + meta: LazyComponentBuffer, ) -> Self { unsafe fn drop_ptr(x: *mut u8) { x.cast::().drop_in_place() @@ -99,7 +99,7 @@ impl core::ops::Deref for ComponentVTable { impl ComponentVTable { /// Creates a new *typed* vtable of `T` - pub const fn new(name: &'static str, meta: &'static LazyComponentBuffer) -> Self { + pub const fn new(name: &'static str, meta: LazyComponentBuffer) -> Self { Self { inner: UntypedVTable::new::(name, meta), marker: PhantomData, diff --git a/src/world.rs b/src/world.rs index 8cc546e3..c2b13f14 100644 --- a/src/world.rs +++ b/src/world.rs @@ -342,7 +342,7 @@ impl World { } let id = desc.key().id; - let mut meta = desc.get_meta(); + let mut meta = desc.create_meta(); meta.set(component_info(), desc); meta.set(name(), desc.name().into()); @@ -917,7 +917,7 @@ impl World { let desc = component.desc(); - let mut meta = desc.get_meta(); + let mut meta = desc.create_meta(); meta.set(component_info(), desc); meta.set(components::name(), desc.name().into()); diff --git a/tests/components.rs b/tests/components.rs index 0d154d47..8d0cde7e 100644 --- a/tests/components.rs +++ b/tests/components.rs @@ -11,15 +11,16 @@ use glam::{vec2, Vec2}; fn custom_component() { let mut world = World::new(); - static META: LazyComponentBuffer = LazyComponentBuffer::new(|desc| { - let mut buf = ComponentBuffer::new(); - >::attach(desc, &mut buf); - buf - }); - - static VTABLE: &ComponentVTable = &ComponentVTable::new("position", &META); + static VTABLE: ComponentVTable = ComponentVTable::new( + "position", + LazyComponentBuffer::new(|desc| { + let mut buf = ComponentBuffer::new(); + >::attach(desc, &mut buf); + buf + }), + ); - let position = world.spawn_component(VTABLE); + let position = world.spawn_component(&VTABLE); let id = Entity::builder() .set(position, vec2(1.0, 6.4))