@@ -569,14 +569,38 @@ impl<'a, 'gcx, 'tcx> Binder<ExistentialPredicate<'tcx>> {
569
569
impl < ' tcx > serialize:: UseSpecializedDecodable for & ' tcx List < ExistentialPredicate < ' tcx > > { }
570
570
571
571
impl < ' tcx > List < ExistentialPredicate < ' tcx > > {
572
+ /// Returns the "principal def id" of this set of existential predicates.
573
+ ///
574
+ /// A Rust trait object type consists (in addition to a lifetime bound)
575
+ /// of a set of trait bounds, which are separated into any number
576
+ /// of auto-trait bounds, and at most 1 non-auto-trait bound. The
577
+ /// non-auto-trait bound is called the "principal" of the trait
578
+ /// object.
579
+ ///
580
+ /// Only the principal can have methods or type parameters (because
581
+ /// auto traits can have neither of them). This is important, because
582
+ /// it means the auto traits can be treated as an unordered set (methods
583
+ /// would force an order for the vtable, while relating traits with
584
+ /// type parameters without knowing the order to relate them in is
585
+ /// a rather non-trivial task).
586
+ ///
587
+ /// For example, in the trait object `dyn fmt::Debug + Sync`, the
588
+ /// principal bound is `Some(fmt::Debug)`, while the auto-trait bounds
589
+ /// are the set `{Sync}`.
590
+ ///
591
+ /// It is also possible to have a "trivial" trait object that
592
+ /// consists only of auto traits, with no principal - for example,
593
+ /// `dyn Send + Sync`. In that case, the set of auto-trait bounds
594
+ /// is `{Send, Sync}`, while there is no principal. These trait objects
595
+ /// have a "trivial" vtable consisting of just the size, alignment,
596
+ /// and destructor.
572
597
pub fn principal ( & self ) -> Option < ExistentialTraitRef < ' tcx > > {
573
598
match self [ 0 ] {
574
599
ExistentialPredicate :: Trait ( tr) => Some ( tr) ,
575
600
_ => None
576
601
}
577
602
}
578
603
579
-
580
604
pub fn principal_def_id ( & self ) -> Option < DefId > {
581
605
self . principal ( ) . map ( |d| d. def_id )
582
606
}
0 commit comments