Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Builder::maybe_with for convenient optional addition of a compo… #712

Merged
merged 1 commit into from
Nov 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,40 @@ pub trait Builder {
#[cfg(not(feature = "parallel"))]
fn with<C: Component>(self, c: C) -> Self;

/// Convenience method that calls `self.with(component)` if `Some(component)` is provided
///
/// # Panics
///
/// Panics if the component hasn't been `register()`ed in the
/// `World`.
#[cfg(feature = "parallel")]
fn maybe_with<C: Component + Send + Sync>(self, c: Option<C>) -> Self
where
Self: Sized,
{
match c {
Some(c) => self.with(c),
None => self,
}
}

/// Convenience method that calls `self.with(component)` if `Some(component)` is provided
///
/// # Panics
///
/// Panics if the component hasn't been `register()`ed in the
/// `World`.
#[cfg(not(feature = "parallel"))]
fn maybe_with<C: Component>(self, c: Option<C>) -> Self
where
Self: Sized,
{
match c {
Some(c) => self.with(c),
None => self,
}
}

/// Finishes the building and returns the entity.
fn build(self) -> Entity;
}
Expand Down