|
| 1 | +# Kinds |
| 2 | +A `ty::subst::Kind<'tcx>` represents some entity in the type system: currently |
| 3 | +either a type (`Ty<'tcx>`) or a lifetime (`ty::Region<'tcx>`), though in the |
| 4 | +future this will also include constants (`ty::Const<'tcx>`) to facilitate the |
| 5 | +use of const generics. `Kind` is used for type and lifetime substitution (from |
| 6 | +abstract type and lifetime parameters to concrete types and lifetimes). |
| 7 | + |
| 8 | +## `UnpackedKind` |
| 9 | +As `Kind` itself is not type-safe (see [`Kind`](#kind)), the `UnpackedKind` enum |
| 10 | +provides a more convenient and safe interface for dealing with kinds. To |
| 11 | +convert from an `UnpackedKind` to a `Kind`, you can call `Kind::from` (or |
| 12 | +`.into`). It should not be necessary to convert a `Kind` to an `UnpackedKind`: |
| 13 | +instead, you should prefer to deal with `UnpackedKind`, converting it only when |
| 14 | +passing it to `Subst` methods. |
| 15 | + |
| 16 | +## `Kind` |
| 17 | +The actual `Kind` struct is optimised for space, storing the type or lifetime |
| 18 | +as an interned pointer containing a mask identifying its kind (in the lowest |
| 19 | +2 bits). |
| 20 | + |
| 21 | +## `Subst` |
| 22 | +`ty::subst::Subst<'tcx>` is simply defined as a slice of `Kind<'tcx>`s |
| 23 | +and acts as an ordered list of substitutions from kind parameters (i.e. |
| 24 | +type and lifetime parameters) to kinds. |
| 25 | + |
| 26 | +For example, given a `HashMap<K, V>` with two type parameters, `K` and `V`, an |
| 27 | +instantiation of the parameters, for example `HashMap<i32, u32>`, would be |
| 28 | +represented by the substitution `&'tcx [tcx.types.i32, tcx.types.u32]`. |
| 29 | + |
| 30 | +`Subst` provides various convenience methods to instantiant substitutions |
| 31 | +given item definitions. |
0 commit comments