Skip to content

Commit c115521

Browse files
committed
Add pub fn direct_super_traits(db, trait_id) to hir_ty crate
1 parent 5a9767b commit c115521

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

crates/hir-ty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub use mapping::{
9898
};
9999
pub use method_resolution::check_orphan_rules;
100100
pub use traits::TraitEnvironment;
101-
pub use utils::{all_super_traits, is_fn_unsafe_to_call};
101+
pub use utils::{all_super_traits, direct_super_traits, is_fn_unsafe_to_call};
102102

103103
pub use chalk_ir::{
104104
cast::Cast,

crates/hir-ty/src/utils.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ pub(crate) fn fn_traits(
4343
.flat_map(|it| it.as_trait())
4444
}
4545

46+
/// Returns an iterator over the direct super traits (including the trait itself).
47+
pub fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[TraitId; 4]> {
48+
let mut result = smallvec![trait_];
49+
direct_super_traits_cb(db, trait_, |tt| {
50+
if !result.contains(&tt) {
51+
result.push(tt);
52+
}
53+
});
54+
result
55+
}
56+
4657
/// Returns an iterator over the whole super trait hierarchy (including the
4758
/// trait itself).
4859
pub fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[TraitId; 4]> {
@@ -54,7 +65,7 @@ pub fn all_super_traits(db: &dyn DefDatabase, trait_: TraitId) -> SmallVec<[Trai
5465
while let Some(&t) = result.get(i) {
5566
// yeah this is quadratic, but trait hierarchies should be flat
5667
// enough that this doesn't matter
57-
direct_super_traits(db, t, |tt| {
68+
direct_super_traits_cb(db, t, |tt| {
5869
if !result.contains(&tt) {
5970
result.push(tt);
6071
}
@@ -153,7 +164,7 @@ impl Iterator for ClauseElaborator<'_> {
153164
}
154165
}
155166

156-
fn direct_super_traits(db: &dyn DefDatabase, trait_: TraitId, cb: impl FnMut(TraitId)) {
167+
fn direct_super_traits_cb(db: &dyn DefDatabase, trait_: TraitId, cb: impl FnMut(TraitId)) {
157168
let resolver = trait_.resolver(db);
158169
let generic_params = db.generic_params(trait_.into());
159170
let trait_self = generic_params.trait_self_param();

0 commit comments

Comments
 (0)