Skip to content

Commit

Permalink
Add Builder::maybe_with for convenient optional addition of a compo…
Browse files Browse the repository at this point in the history
…nent
  • Loading branch information
Imberflur committed Nov 21, 2020
1 parent 6cba11b commit 1a2b6c7
Showing 1 changed file with 34 additions and 0 deletions.
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

0 comments on commit 1a2b6c7

Please sign in to comment.