pub trait Pool: Sized + Component<Mutability = Mutable> {
type Quantity: Add<Output = Self::Quantity> + Sub<Output = Self::Quantity> + AddAssign + SubAssign + PartialEq + PartialOrd + Clone + Copy + Send + Sync + 'static;
const MIN: Self::Quantity;
// Required methods
fn current(&self) -> Self::Quantity;
fn set_current(&mut self, new_quantity: Self::Quantity) -> Self::Quantity;
fn max(&self) -> Self::Quantity;
fn set_max(
&mut self,
new_max: Self::Quantity,
) -> Result<(), MaxPoolLessThanMin>;
// Provided methods
fn available(&self, amount: Self::Quantity) -> Result<(), CannotUseAbility> { ... }
fn is_full(&self) -> bool { ... }
fn is_empty(&self) -> bool { ... }
fn expend(&mut self, amount: Self::Quantity) -> Result<(), CannotUseAbility> { ... }
fn replenish(&mut self, amount: Self::Quantity) { ... }
}Expand description
A reservoir of a resource that can be used to pay for abilities, or keep track of character state.
Each type that implements this trait should be stored on a component, and contains information about the current and max values.
There are two core benefits to using pools, rather than creating your own solutions:
- It is impossible to accidentally set the value outside of the bounds of the pool.
- The
AbilityCostscomponent can be used to automatically check if an ability can be used.
See RegeneratingPool for pools that regenerate over time.
Required Associated Constants§
Required Associated Types§
Sourcetype Quantity: Add<Output = Self::Quantity> + Sub<Output = Self::Quantity> + AddAssign + SubAssign + PartialEq + PartialOrd + Clone + Copy + Send + Sync + 'static
type Quantity: Add<Output = Self::Quantity> + Sub<Output = Self::Quantity> + AddAssign + SubAssign + PartialEq + PartialOrd + Clone + Copy + Send + Sync + 'static
A type that tracks the quantity within a pool.
Unlike a Pool type, which stores a max, min and regeneration,
quantities are lighter weight and should be used for things like damage amounts, mana costs and regen rates.
Required Methods§
Sourcefn set_current(&mut self, new_quantity: Self::Quantity) -> Self::Quantity
fn set_current(&mut self, new_quantity: Self::Quantity) -> Self::Quantity
Sets the current quantity of resources in the pool.
This will be bounded by the minimum and maximum values of this pool. The value that was actually set is returned.
Sourcefn set_max(&mut self, new_max: Self::Quantity) -> Result<(), MaxPoolLessThanMin>
fn set_max(&mut self, new_max: Self::Quantity) -> Result<(), MaxPoolLessThanMin>
Sets the maximum quantity of resources that this pool can store.
The current value will be reduced to the new max if necessary.
Has no effect if new_max < Pool::MIN.
Returns a MaxPoolLessThanMin error if this occurs.
Provided Methods§
Sourcefn available(&self, amount: Self::Quantity) -> Result<(), CannotUseAbility>
fn available(&self, amount: Self::Quantity) -> Result<(), CannotUseAbility>
Check if the given cost can be paid by this pool.
Sourcefn is_empty(&self) -> bool
fn is_empty(&self) -> bool
Is the pool currently empty?
Note that this compares the current value to Pool::MIN, not 0.
Sourcefn expend(&mut self, amount: Self::Quantity) -> Result<(), CannotUseAbility>
fn expend(&mut self, amount: Self::Quantity) -> Result<(), CannotUseAbility>
Spend the specified amount from the pool, if there is that much available.
Otherwise, return the error CannotUseAbility::PoolInsufficient.
Sourcefn replenish(&mut self, amount: Self::Quantity)
fn replenish(&mut self, amount: Self::Quantity)
Replenish the pool by the specified amount.
This cannot cause the pool to exceed maximum value that can be stored in the pool.
This is the sign-flipped counterpart to Self::expend,
however, unlike Self::expend, this method will not return an error if the pool is empty.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".