From 1a2b6c73f1a94d60cc40ea8f550ba67d841cdf9b Mon Sep 17 00:00:00 2001 From: Imbris Date: Sat, 21 Nov 2020 00:07:21 -0500 Subject: [PATCH] Add `Builder::maybe_with` for convenient optional addition of a component --- src/world/mod.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/world/mod.rs b/src/world/mod.rs index b91e39739..aa74b50a0 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -65,6 +65,40 @@ pub trait Builder { #[cfg(not(feature = "parallel"))] fn with(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(self, c: Option) -> 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(self, c: Option) -> Self + where + Self: Sized, + { + match c { + Some(c) => self.with(c), + None => self, + } + } + /// Finishes the building and returns the entity. fn build(self) -> Entity; }