diff options
| author | rami3l <rami3l@outlook.com> | 2022-12-13 12:25:44 +0800 |
|---|---|---|
| committer | rami3l <rami3l@outlook.com> | 2022-12-14 19:18:05 +0800 |
| commit | 12b05d241698fb3ecb18eaebc75cdce85abb1013 (patch) | |
| tree | 79797530c832ef431a5930552046b22870978c17 /crates/syntax | |
| parent | 4596847a88abb0d5077c5111c3093e724673d7a0 (diff) | |
| download | rust-12b05d241698fb3ecb18eaebc75cdce85abb1013.tar.gz rust-12b05d241698fb3ecb18eaebc75cdce85abb1013.zip | |
fix: add generic `TypeBoundList` in generated derivable impl
Diffstat (limited to 'crates/syntax')
| -rw-r--r-- | crates/syntax/src/ast/make.rs | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 8c26009add2..11822361f40 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -719,11 +719,22 @@ pub fn param_list( ast_from_text(&list) } -pub fn type_param(name: ast::Name, ty: Option<ast::TypeBoundList>) -> ast::TypeParam { - let bound = match ty { - Some(it) => format!(": {it}"), - None => String::new(), - }; +pub fn type_bound(bound: &str) -> ast::TypeBound { + ast_from_text(&format!("fn f<T: {bound}>() {{ }}")) +} + +pub fn type_bound_list( + bounds: impl IntoIterator<Item = ast::TypeBound>, +) -> Option<ast::TypeBoundList> { + let bounds = bounds.into_iter().map(|it| it.to_string()).unique().join(" + "); + if bounds.is_empty() { + return None; + } + Some(ast_from_text(&format!("fn f<T: {bounds}>() {{ }}"))) +} + +pub fn type_param(name: ast::Name, bounds: Option<ast::TypeBoundList>) -> ast::TypeParam { + let bound = bounds.map_or_else(String::new, |it| format!(": {it}")); ast_from_text(&format!("fn f<{name}{bound}>() {{ }}")) } |
