Struct rustc_mir_build::thir::pattern::deconstruct_pat::SplitVarLenSlice
source · Expand description
This computes constructor splitting for variable-length slices, as explained at the top of the file.
A slice pattern [x, .., y]
behaves like the infinite or-pattern [x, y] | [x, _, y] | [x, _, _, y] | ...
. The corresponding value constructors are fixed-length array constructors above a
given minimum length. We obviously can’t list this infinitude of constructors. Thankfully,
it turns out that for each finite set of slice patterns, all sufficiently large array lengths
are equivalent.
Let’s look at an example, where we are trying to split the last pattern:
match x {
[true, true, ..] => {}
[.., false, false] => {}
[..] => {}
}
Here are the results of specialization for the first few lengths:
// length 0
[] => {}
// length 1
[_] => {}
// length 2
[true, true] => {}
[false, false] => {}
[_, _] => {}
// length 3
[true, true, _ ] => {}
[_, false, false] => {}
[_, _, _ ] => {}
// length 4
[true, true, _, _ ] => {}
[_, _, false, false] => {}
[_, _, _, _ ] => {}
// length 5
[true, true, _, _, _ ] => {}
[_, _, _, false, false] => {}
[_, _, _, _, _ ] => {}
If we went above length 5, we would simply be inserting more columns full of wildcards in the
middle. This means that the set of witnesses for length l >= 5
if equivalent to the set for
any other l' >= 5
: simply add or remove wildcards in the middle to convert between them.
This applies to any set of slice patterns: there will be a length L
above which all lengths
behave the same. This is exactly what we need for constructor splitting. Therefore a
variable-length slice can be split into a variable-length slice of minimal length L
, and many
fixed-length slices of lengths < L
.
For each variable-length pattern p
with a prefix of length plₚ
and suffix of length slₚ
,
only the first plₚ
and the last slₚ
elements are examined. Therefore, as long as L
is
positive (to avoid concerns about empty types), all elements after the maximum prefix length
and before the maximum suffix length are not examined by any variable-length pattern, and
therefore can be added/removed without affecting them - creating equivalent patterns from any
sufficiently-large length.
Of course, if fixed-length patterns exist, we must be sure that our length is large enough to
miss them all, so we can pick L = max(max(FIXED_LEN)+1, max(PREFIX_LEN) + max(SUFFIX_LEN))
max_slice
below will be made to have arity L
.
Fields§
§array_len: Option<usize>
If the type is an array, this is its size.
arity: usize
The arity of the input slice.
max_slice: SliceKind
The smallest slice bigger than any slice seen. max_slice.arity()
is the length L
described above.
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for SplitVarLenSlice
impl Send for SplitVarLenSlice
impl Sync for SplitVarLenSlice
impl Unpin for SplitVarLenSlice
impl UnwindSafe for SplitVarLenSlice
Blanket Implementations§
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...)
attributes. Please see the Rust Reference’s “Type Layout” chapter for details on type layout guarantees.
Size: 48 bytes