pub struct DebruijnIndex {
pub(crate) private_use_as_methods_instead: u32,
}
Expand description
A De Bruijn index is a standard means of representing regions (and perhaps later types) in a higher-ranked setting. In particular, imagine a type like this:
for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
// ^ ^ | | |
// | | | | |
// | +------------+ 0 | |
// | | |
// +----------------------------------+ 1 |
// | |
// +----------------------------------------------+ 0
In this type, there are two binders (the outer fn and the inner fn). We need to be able to determine, for any given region, which fn type it is bound by, the inner or the outer one. There are various ways you can do this, but a De Bruijn index is one of the more convenient and has some nice properties. The basic idea is to count the number of binders, inside out. Some examples should help clarify what I mean.
Let’s start with the reference type &'b isize
that is the first
argument to the inner function. This region 'b
is assigned a De
Bruijn index of 0, meaning “the innermost binder” (in this case, a
fn). The region 'a
that appears in the second argument type (&'a isize
) would then be assigned a De Bruijn index of 1, meaning “the
second-innermost binder”. (These indices are written on the arrows
in the diagram).
What is interesting is that De Bruijn index attached to a particular
variable will vary depending on where it appears. For example,
the final type &'a char
also refers to the region 'a
declared on
the outermost fn. But this time, this reference is not nested within
any other binders (i.e., it is not an argument to the inner fn, but
rather the outer one). Therefore, in this case, it is assigned a
De Bruijn index of 0, because the innermost binder in that location
is the outer fn.
Fields§
§private_use_as_methods_instead: u32
Implementations§
Source§impl DebruijnIndex
impl DebruijnIndex
Sourcepub const MAX_AS_U32: u32 = 4_294_967_040u32
pub const MAX_AS_U32: u32 = 4_294_967_040u32
Maximum value the index can take, as a u32
.
Sourcepub const fn from_usize(value: usize) -> Self
pub const fn from_usize(value: usize) -> Self
Sourcepub const unsafe fn from_u32_unchecked(value: u32) -> Self
pub const unsafe fn from_u32_unchecked(value: u32) -> Self
Creates a new index from a given u32
.
§Safety
The provided value must be less than or equal to the maximum value for the newtype. Providing a value outside this range is undefined due to layout restrictions.
Prefer using from_u32
.
Source§impl DebruijnIndex
impl DebruijnIndex
Sourcepub fn shifted_in(self, amount: u32) -> DebruijnIndex
pub fn shifted_in(self, amount: u32) -> DebruijnIndex
Returns the resulting index when this value is moved into
amount
number of new binders. So, e.g., if you had
for<’a> fn(&’a x)
and you wanted to change it to
for<’a> fn(for<’b> fn(&’a x))
you would need to shift the index for 'a
into a new binder.
Sourcepub fn shift_in(&mut self, amount: u32)
pub fn shift_in(&mut self, amount: u32)
Update this index in place by shifting it “in” through
amount
number of binders.
Sourcepub fn shifted_out(self, amount: u32) -> DebruijnIndex
pub fn shifted_out(self, amount: u32) -> DebruijnIndex
Returns the resulting index when this value is moved out from
amount
number of new binders.
Sourcepub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self
pub fn shifted_out_to_binder(self, to_binder: DebruijnIndex) -> Self
Adjusts any De Bruijn indices so as to make to_binder
the
innermost binder. That is, if we have something bound at to_binder
,
it will now be bound at INNERMOST. This is an appropriate thing to do
when moving a region out from inside binders:
for<'a> fn(for<'b> for<'c> fn(&'a u32), _)
// Binder: D3 D2 D1 ^^
Here, the region 'a
would have the De Bruijn index D3,
because it is the bound 3 binders out. However, if we wanted
to refer to that region 'a
in the second argument (the _
),
those two binders would not be in scope. In that case, we
might invoke shift_out_to_binder(D3)
. This would adjust the
De Bruijn index of 'a
to D1 (the innermost binder).
If we invoke shift_out_to_binder
and the region is in fact
bound by one of the binders we are shifting out of, that is an
error (and should fail an assertion failure).
Trait Implementations§
Source§impl Add<usize> for DebruijnIndex
impl Add<usize> for DebruijnIndex
Source§impl Clone for DebruijnIndex
impl Clone for DebruijnIndex
Source§fn clone(&self) -> DebruijnIndex
fn clone(&self) -> DebruijnIndex
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for DebruijnIndex
impl Debug for DebruijnIndex
Source§impl From<DebruijnIndex> for u32
impl From<DebruijnIndex> for u32
Source§fn from(v: DebruijnIndex) -> u32
fn from(v: DebruijnIndex) -> u32
Source§impl From<DebruijnIndex> for usize
impl From<DebruijnIndex> for usize
Source§fn from(v: DebruijnIndex) -> usize
fn from(v: DebruijnIndex) -> usize
Source§impl From<u32> for DebruijnIndex
impl From<u32> for DebruijnIndex
Source§impl From<usize> for DebruijnIndex
impl From<usize> for DebruijnIndex
Source§impl Hash for DebruijnIndex
impl Hash for DebruijnIndex
Source§impl<__CTX> HashStable<__CTX> for DebruijnIndex
impl<__CTX> HashStable<__CTX> for DebruijnIndex
fn hash_stable(&self, __hcx: &mut __CTX, __hasher: &mut StableHasher)
Source§impl Idx for DebruijnIndex
impl Idx for DebruijnIndex
Source§impl Ord for DebruijnIndex
impl Ord for DebruijnIndex
Source§fn cmp(&self, other: &DebruijnIndex) -> Ordering
fn cmp(&self, other: &DebruijnIndex) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for DebruijnIndex
impl PartialEq for DebruijnIndex
Source§impl PartialOrd for DebruijnIndex
impl PartialOrd for DebruijnIndex
Source§impl Step for DebruijnIndex
impl Step for DebruijnIndex
Source§fn steps_between(start: &Self, end: &Self) -> Option<usize>
fn steps_between(start: &Self, end: &Self) -> Option<usize>
step_trait
)Source§fn forward_checked(start: Self, u: usize) -> Option<Self>
fn forward_checked(start: Self, u: usize) -> Option<Self>
step_trait
)Source§fn backward_checked(start: Self, u: usize) -> Option<Self>
fn backward_checked(start: Self, u: usize) -> Option<Self>
step_trait
)Source§fn forward(start: Self, count: usize) -> Self
fn forward(start: Self, count: usize) -> Self
step_trait
)Source§unsafe fn forward_unchecked(start: Self, count: usize) -> Self
unsafe fn forward_unchecked(start: Self, count: usize) -> Self
step_trait
)Source§fn backward(start: Self, count: usize) -> Self
fn backward(start: Self, count: usize) -> Self
step_trait
)Source§unsafe fn backward_unchecked(start: Self, count: usize) -> Self
unsafe fn backward_unchecked(start: Self, count: usize) -> Self
step_trait
)Source§impl<I: Interner> TypeFoldable<I> for DebruijnIndex
impl<I: Interner> TypeFoldable<I> for DebruijnIndex
Source§fn try_fold_with<F: FallibleTypeFolder<I>>(
self,
_: &mut F,
) -> Result<Self, F::Error>
fn try_fold_with<F: FallibleTypeFolder<I>>( self, _: &mut F, ) -> Result<Self, F::Error>
Source§fn fold_with<F: TypeFolder<I>>(self, _: &mut F) -> Self
fn fold_with<F: TypeFolder<I>>(self, _: &mut F) -> Self
try_fold_with
for use with infallible
folders. Do not override this method, to ensure coherence with
try_fold_with
.Source§impl<I: Interner> TypeVisitable<I> for DebruijnIndex
impl<I: Interner> TypeVisitable<I> for DebruijnIndex
Source§fn visit_with<F: TypeVisitor<I>>(&self, _: &mut F) -> F::Result
fn visit_with<F: TypeVisitor<I>>(&self, _: &mut F) -> F::Result
impl Copy for DebruijnIndex
impl Eq for DebruijnIndex
impl StructuralPartialEq for DebruijnIndex
Auto Trait Implementations§
impl DynSend for DebruijnIndex
impl DynSync for DebruijnIndex
impl Freeze for DebruijnIndex
impl RefUnwindSafe for DebruijnIndex
impl Send for DebruijnIndex
impl Sync for DebruijnIndex
impl Unpin for DebruijnIndex
impl UnwindSafe for DebruijnIndex
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)Source§impl<T, R> CollectAndApply<T, R> for T
impl<T, R> CollectAndApply<T, R> for T
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<I, T> TypeVisitableExt<I> for Twhere
I: Interner,
T: TypeVisitable<I>,
impl<I, T> TypeVisitableExt<I> for Twhere
I: Interner,
T: TypeVisitable<I>,
fn has_type_flags(&self, flags: TypeFlags) -> bool
Source§fn has_vars_bound_at_or_above(&self, binder: DebruijnIndex) -> bool
fn has_vars_bound_at_or_above(&self, binder: DebruijnIndex) -> bool
true
if self
has any late-bound regions that are either
bound by binder
or bound by some binder outside of binder
.
If binder
is ty::INNERMOST
, this indicates whether
there are any late-bound regions that appear free.fn error_reported(&self) -> Result<(), <I as Interner>::ErrorGuaranteed>
Source§fn has_vars_bound_above(&self, binder: DebruijnIndex) -> bool
fn has_vars_bound_above(&self, binder: DebruijnIndex) -> bool
true
if this type has any regions that escape binder
(and
hence are not bound by it).Source§fn has_escaping_bound_vars(&self) -> bool
fn has_escaping_bound_vars(&self) -> bool
true
if this type has regions that are not a part of the type.
For example, for<'a> fn(&'a i32)
return false
, while fn(&'a i32)
would return true
. The latter can occur when traversing through the
former. Read morefn has_aliases(&self) -> bool
fn has_opaque_types(&self) -> bool
fn has_coroutines(&self) -> bool
fn references_error(&self) -> bool
fn has_non_region_param(&self) -> bool
fn has_infer_regions(&self) -> bool
fn has_infer_types(&self) -> bool
fn has_non_region_infer(&self) -> bool
fn has_infer(&self) -> bool
fn has_placeholders(&self) -> bool
fn has_non_region_placeholders(&self) -> bool
fn has_param(&self) -> bool
Source§fn has_free_regions(&self) -> bool
fn has_free_regions(&self) -> bool
fn has_erased_regions(&self) -> bool
Source§fn has_erasable_regions(&self) -> bool
fn has_erasable_regions(&self) -> bool
Source§fn is_global(&self) -> bool
fn is_global(&self) -> bool
Source§fn has_bound_regions(&self) -> bool
fn has_bound_regions(&self) -> bool
Source§fn has_non_region_bound_vars(&self) -> bool
fn has_non_region_bound_vars(&self) -> bool
Source§fn has_bound_vars(&self) -> bool
fn has_bound_vars(&self) -> bool
Source§fn still_further_specializable(&self) -> bool
fn still_further_specializable(&self) -> bool
impl
specialization.Source§impl<I, T, U> Upcast<I, U> for Twhere
U: UpcastFrom<I, T>,
impl<I, T, U> Upcast<I, U> for Twhere
U: UpcastFrom<I, T>,
Source§impl<I, T> UpcastFrom<I, T> for T
impl<I, T> UpcastFrom<I, T> for T
fn upcast_from(from: T, _tcx: I) -> T
Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
Source§fn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
impl<'a, T> Captures<'a> for Twhere
T: ?Sized,
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: 4 bytes