Module bound

Module bound 

Source
Expand description

Type encoded dynamic trait bounds.

dungeon_cell uses generic types for configuration. Part of this configuration is what traits a type stored in a dungeon_cell type require.

An example is wanting a DungeonCore to be Send. To allow this, all types the DungeonCore can store must implement Send. This is expressed using the following trait bound. The bounds::Send would be part of the DungeonCore’s generics.

where
    T: Dynamic<bounds::Send>

See the traits module for the supported traits. Only a small collection of traits are supported because of how the encoding has to be setup for rustc to understand it. The chosen traits are all from core and are the most commonly used and implemented.

§Subtraiting and Variance

In Rust, a trait bound is of the form T: X + Y + Z which means the type T must implement traits X, Y, and Z. It doesn’t say those are the only traits that type T implements. Additionally, there isn’t a way in stable Rust to bound on a type T not implementing a type.

This module encodes trait bounds as types. Because of this encoding we can ask questions about the trait bound in the type system. The four questions we can ask about a bound A are:

  • is the bound B the same as A,
  • is the bound B a subset of A,
  • is the bound B a superset of A,
  • is the bound B disjoint from A.

The first question is provided by B: Equal<A> and simply checks that A and B are the same type.

For example of the second question, given the bound A of the form X + Y + Z, and bound B of the form X + Z, the bound B is a subset of A. A subset bound is a bound that requires less when used to bound a type. The B: Subset<A> trait bound allows checking this.

For example of the third question, given the bound A of the form X + Y, and bound B of the form X + Y + Z, the bound B is a superset of A. A subset bound is a bound that requires more when used to bound a type. In reality this is the inverse of the subset question so if we switch A and B we get: is A a subset of B.

For example of the forth question, given the bound A of the form X + Y, and bound B of the form Z, the bound B is disjoint from A. A disjoint bound is a bound that requires different functionality when used to bound a type. The B: Disjoint<A> trait allows checking this.

Modules§

bounds
Predefined dynamic trait bounds.
traits
Markers for traits that can be used in a dynamic trait bound.

Structs§

Bound
Type encoded trait bound.

Traits§

Disjoint
Check if dynamic trait bound is disjoint from another dynamic trait bound.
Dynamic
Trait implemented when the bound given by B is satisfied by Self.
Equal
Check if dynamic trait bound is the same as another dynamic trait bound.
Subset
Check if dynamic trait bound is a subset of another dynamic trait bound.