rustc_utils/mir/
adt_def.rs

1//! Utilities for [`AdtDef`].
2
3use rustc_hir::def_id::DefId;
4use rustc_middle::ty::{AdtDef, FieldDef, TyCtxt};
5
6/// Extension trait for [`AdtDef`].
7pub trait AdtDefExt<'tcx> {
8  /// Returns an iterator over all the fields of the ADT that are visible
9  /// from `module`.
10  fn all_visible_fields(
11    self,
12    module: DefId,
13    tcx: TyCtxt<'tcx>,
14  ) -> impl Iterator<Item = &'tcx FieldDef>;
15}
16
17impl<'tcx> AdtDefExt<'tcx> for AdtDef<'tcx> {
18  fn all_visible_fields(
19    self,
20    module: DefId,
21    tcx: TyCtxt<'tcx>,
22  ) -> impl Iterator<Item = &'tcx FieldDef> {
23    self
24      .all_fields()
25      .filter(move |field| field.vis.is_accessible_from(module, tcx))
26  }
27}