diff options
author | Aleksey Kladov <[email protected]> | 2020-07-13 15:16:53 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-13 15:21:02 +0100 |
commit | 693ac892f2db5db1ce7cf86db7bf6207b3515c42 (patch) | |
tree | 89272d8f03145df99fe68f0e35f8892dc4ab2c72 /crates | |
parent | 6b4cf5b7d8043469c9856f6578d282f9532d7fe0 (diff) |
Don't copy-paste `impl_froms` into every crate
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 23 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir/src/semantics/source_to_def.rs | 5 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 45 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/infer.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/lib.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/lower.rs | 13 | ||||
-rw-r--r-- | crates/ra_tt/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_tt/src/lib.rs | 19 | ||||
-rw-r--r-- | crates/stdx/src/macros.rs | 21 |
11 files changed, 67 insertions, 106 deletions
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index ba7b39a19..512676c99 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml | |||
@@ -15,6 +15,7 @@ arrayvec = "0.5.1" | |||
15 | 15 | ||
16 | itertools = "0.9.0" | 16 | itertools = "0.9.0" |
17 | 17 | ||
18 | stdx = { path = "../stdx" } | ||
18 | ra_syntax = { path = "../ra_syntax" } | 19 | ra_syntax = { path = "../ra_syntax" } |
19 | ra_db = { path = "../ra_db" } | 20 | ra_db = { path = "../ra_db" } |
20 | ra_prof = { path = "../ra_prof" } | 21 | ra_prof = { path = "../ra_prof" } |
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 04fd335fe..9222009fe 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -38,6 +38,7 @@ use ra_syntax::{ | |||
38 | AstNode, | 38 | AstNode, |
39 | }; | 39 | }; |
40 | use rustc_hash::FxHashSet; | 40 | use rustc_hash::FxHashSet; |
41 | use stdx::impl_from; | ||
41 | 42 | ||
42 | use crate::{ | 43 | use crate::{ |
43 | db::{DefDatabase, HirDatabase}, | 44 | db::{DefDatabase, HirDatabase}, |
@@ -142,8 +143,8 @@ pub enum ModuleDef { | |||
142 | TypeAlias(TypeAlias), | 143 | TypeAlias(TypeAlias), |
143 | BuiltinType(BuiltinType), | 144 | BuiltinType(BuiltinType), |
144 | } | 145 | } |
145 | impl_froms!( | 146 | impl_from!( |
146 | ModuleDef: Module, | 147 | Module, |
147 | Function, | 148 | Function, |
148 | Adt(Struct, Enum, Union), | 149 | Adt(Struct, Enum, Union), |
149 | EnumVariant, | 150 | EnumVariant, |
@@ -152,6 +153,7 @@ impl_froms!( | |||
152 | Trait, | 153 | Trait, |
153 | TypeAlias, | 154 | TypeAlias, |
154 | BuiltinType | 155 | BuiltinType |
156 | for ModuleDef | ||
155 | ); | 157 | ); |
156 | 158 | ||
157 | impl ModuleDef { | 159 | impl ModuleDef { |
@@ -541,7 +543,7 @@ pub enum Adt { | |||
541 | Union(Union), | 543 | Union(Union), |
542 | Enum(Enum), | 544 | Enum(Enum), |
543 | } | 545 | } |
544 | impl_froms!(Adt: Struct, Union, Enum); | 546 | impl_from!(Struct, Union, Enum for Adt); |
545 | 547 | ||
546 | impl Adt { | 548 | impl Adt { |
547 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { | 549 | pub fn has_non_default_type_params(self, db: &dyn HirDatabase) -> bool { |
@@ -584,7 +586,7 @@ pub enum VariantDef { | |||
584 | Union(Union), | 586 | Union(Union), |
585 | EnumVariant(EnumVariant), | 587 | EnumVariant(EnumVariant), |
586 | } | 588 | } |
587 | impl_froms!(VariantDef: Struct, Union, EnumVariant); | 589 | impl_from!(Struct, Union, EnumVariant for VariantDef); |
588 | 590 | ||
589 | impl VariantDef { | 591 | impl VariantDef { |
590 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { | 592 | pub fn fields(self, db: &dyn HirDatabase) -> Vec<Field> { |
@@ -627,8 +629,7 @@ pub enum DefWithBody { | |||
627 | Static(Static), | 629 | Static(Static), |
628 | Const(Const), | 630 | Const(Const), |
629 | } | 631 | } |
630 | 632 | impl_from!(Function, Const, Static for DefWithBody); | |
631 | impl_froms!(DefWithBody: Function, Const, Static); | ||
632 | 633 | ||
633 | impl DefWithBody { | 634 | impl DefWithBody { |
634 | pub fn module(self, db: &dyn HirDatabase) -> Module { | 635 | pub fn module(self, db: &dyn HirDatabase) -> Module { |
@@ -930,14 +931,15 @@ pub enum GenericDef { | |||
930 | // consts can have type parameters from their parents (i.e. associated consts of traits) | 931 | // consts can have type parameters from their parents (i.e. associated consts of traits) |
931 | Const(Const), | 932 | Const(Const), |
932 | } | 933 | } |
933 | impl_froms!( | 934 | impl_from!( |
934 | GenericDef: Function, | 935 | Function, |
935 | Adt(Struct, Enum, Union), | 936 | Adt(Struct, Enum, Union), |
936 | Trait, | 937 | Trait, |
937 | TypeAlias, | 938 | TypeAlias, |
938 | ImplDef, | 939 | ImplDef, |
939 | EnumVariant, | 940 | EnumVariant, |
940 | Const | 941 | Const |
942 | for GenericDef | ||
941 | ); | 943 | ); |
942 | 944 | ||
943 | impl GenericDef { | 945 | impl GenericDef { |
@@ -1578,8 +1580,8 @@ pub enum AttrDef { | |||
1578 | MacroDef(MacroDef), | 1580 | MacroDef(MacroDef), |
1579 | } | 1581 | } |
1580 | 1582 | ||
1581 | impl_froms!( | 1583 | impl_from!( |
1582 | AttrDef: Module, | 1584 | Module, |
1583 | Field, | 1585 | Field, |
1584 | Adt(Struct, Enum, Union), | 1586 | Adt(Struct, Enum, Union), |
1585 | EnumVariant, | 1587 | EnumVariant, |
@@ -1589,6 +1591,7 @@ impl_froms!( | |||
1589 | Trait, | 1591 | Trait, |
1590 | TypeAlias, | 1592 | TypeAlias, |
1591 | MacroDef | 1593 | MacroDef |
1594 | for AttrDef | ||
1592 | ); | 1595 | ); |
1593 | 1596 | ||
1594 | pub trait HasAttrs { | 1597 | pub trait HasAttrs { |
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 3364a822f..7d9b174b4 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -19,25 +19,6 @@ | |||
19 | 19 | ||
20 | #![recursion_limit = "512"] | 20 | #![recursion_limit = "512"] |
21 | 21 | ||
22 | macro_rules! impl_froms { | ||
23 | ($e:ident: $($v:ident $(($($sv:ident),*))?),*$(,)?) => { | ||
24 | $( | ||
25 | impl From<$v> for $e { | ||
26 | fn from(it: $v) -> $e { | ||
27 | $e::$v(it) | ||
28 | } | ||
29 | } | ||
30 | $($( | ||
31 | impl From<$sv> for $e { | ||
32 | fn from(it: $sv) -> $e { | ||
33 | $e::$v($v::$sv(it)) | ||
34 | } | ||
35 | } | ||
36 | )*)? | ||
37 | )* | ||
38 | } | ||
39 | } | ||
40 | |||
41 | mod semantics; | 22 | mod semantics; |
42 | pub mod db; | 23 | pub mod db; |
43 | mod source_analyzer; | 24 | mod source_analyzer; |
diff --git a/crates/ra_hir/src/semantics/source_to_def.rs b/crates/ra_hir/src/semantics/source_to_def.rs index 0e1d92fb3..42e5a1bdb 100644 --- a/crates/ra_hir/src/semantics/source_to_def.rs +++ b/crates/ra_hir/src/semantics/source_to_def.rs | |||
@@ -16,6 +16,7 @@ use ra_syntax::{ | |||
16 | match_ast, AstNode, SyntaxNode, | 16 | match_ast, AstNode, SyntaxNode, |
17 | }; | 17 | }; |
18 | use rustc_hash::FxHashMap; | 18 | use rustc_hash::FxHashMap; |
19 | use stdx::impl_from; | ||
19 | 20 | ||
20 | use crate::{db::HirDatabase, InFile, MacroDefId}; | 21 | use crate::{db::HirDatabase, InFile, MacroDefId}; |
21 | 22 | ||
@@ -255,8 +256,7 @@ pub(crate) enum ChildContainer { | |||
255 | /// here the children generic parameters, and not, eg enum variants. | 256 | /// here the children generic parameters, and not, eg enum variants. |
256 | GenericDefId(GenericDefId), | 257 | GenericDefId(GenericDefId), |
257 | } | 258 | } |
258 | impl_froms! { | 259 | impl_from! { |
259 | ChildContainer: | ||
260 | DefWithBodyId, | 260 | DefWithBodyId, |
261 | ModuleId, | 261 | ModuleId, |
262 | TraitId, | 262 | TraitId, |
@@ -265,6 +265,7 @@ impl_froms! { | |||
265 | VariantId, | 265 | VariantId, |
266 | TypeAliasId, | 266 | TypeAliasId, |
267 | GenericDefId | 267 | GenericDefId |
268 | for ChildContainer | ||
268 | } | 269 | } |
269 | 270 | ||
270 | impl ChildContainer { | 271 | impl ChildContainer { |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 564434ccc..b71d626c3 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -65,6 +65,7 @@ use item_tree::{ | |||
65 | Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait, | 65 | Const, Enum, Function, Impl, ItemTreeId, ItemTreeNode, ModItem, Static, Struct, Trait, |
66 | TypeAlias, Union, | 66 | TypeAlias, Union, |
67 | }; | 67 | }; |
68 | use stdx::impl_from; | ||
68 | 69 | ||
69 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 70 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
70 | pub struct ModuleId { | 71 | pub struct ModuleId { |
@@ -223,25 +224,6 @@ pub struct TypeParamId { | |||
223 | 224 | ||
224 | pub type LocalTypeParamId = Idx<generics::TypeParamData>; | 225 | pub type LocalTypeParamId = Idx<generics::TypeParamData>; |
225 | 226 | ||
226 | macro_rules! impl_froms { | ||
227 | ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { | ||
228 | $( | ||
229 | impl From<$v> for $e { | ||
230 | fn from(it: $v) -> $e { | ||
231 | $e::$v(it) | ||
232 | } | ||
233 | } | ||
234 | $($( | ||
235 | impl From<$sv> for $e { | ||
236 | fn from(it: $sv) -> $e { | ||
237 | $e::$v($v::$sv(it)) | ||
238 | } | ||
239 | } | ||
240 | )*)? | ||
241 | )* | ||
242 | } | ||
243 | } | ||
244 | |||
245 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 227 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
246 | pub enum ContainerId { | 228 | pub enum ContainerId { |
247 | ModuleId(ModuleId), | 229 | ModuleId(ModuleId), |
@@ -254,7 +236,7 @@ pub enum AssocContainerId { | |||
254 | ImplId(ImplId), | 236 | ImplId(ImplId), |
255 | TraitId(TraitId), | 237 | TraitId(TraitId), |
256 | } | 238 | } |
257 | impl_froms!(AssocContainerId: ContainerId); | 239 | impl_from!(ContainerId for AssocContainerId); |
258 | 240 | ||
259 | /// A Data Type | 241 | /// A Data Type |
260 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 242 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
@@ -263,7 +245,7 @@ pub enum AdtId { | |||
263 | UnionId(UnionId), | 245 | UnionId(UnionId), |
264 | EnumId(EnumId), | 246 | EnumId(EnumId), |
265 | } | 247 | } |
266 | impl_froms!(AdtId: StructId, UnionId, EnumId); | 248 | impl_from!(StructId, UnionId, EnumId for AdtId); |
267 | 249 | ||
268 | /// The defs which can be visible in the module. | 250 | /// The defs which can be visible in the module. |
269 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 251 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -279,8 +261,8 @@ pub enum ModuleDefId { | |||
279 | TypeAliasId(TypeAliasId), | 261 | TypeAliasId(TypeAliasId), |
280 | BuiltinType(BuiltinType), | 262 | BuiltinType(BuiltinType), |
281 | } | 263 | } |
282 | impl_froms!( | 264 | impl_from!( |
283 | ModuleDefId: ModuleId, | 265 | ModuleId, |
284 | FunctionId, | 266 | FunctionId, |
285 | AdtId(StructId, EnumId, UnionId), | 267 | AdtId(StructId, EnumId, UnionId), |
286 | EnumVariantId, | 268 | EnumVariantId, |
@@ -289,6 +271,7 @@ impl_froms!( | |||
289 | TraitId, | 271 | TraitId, |
290 | TypeAliasId, | 272 | TypeAliasId, |
291 | BuiltinType | 273 | BuiltinType |
274 | for ModuleDefId | ||
292 | ); | 275 | ); |
293 | 276 | ||
294 | /// The defs which have a body. | 277 | /// The defs which have a body. |
@@ -299,7 +282,7 @@ pub enum DefWithBodyId { | |||
299 | ConstId(ConstId), | 282 | ConstId(ConstId), |
300 | } | 283 | } |
301 | 284 | ||
302 | impl_froms!(DefWithBodyId: FunctionId, ConstId, StaticId); | 285 | impl_from!(FunctionId, ConstId, StaticId for DefWithBodyId); |
303 | 286 | ||
304 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | 287 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
305 | pub enum AssocItemId { | 288 | pub enum AssocItemId { |
@@ -311,7 +294,7 @@ pub enum AssocItemId { | |||
311 | // sure that you can only turn actual assoc items into AssocItemIds. This would | 294 | // sure that you can only turn actual assoc items into AssocItemIds. This would |
312 | // require not implementing From, and instead having some checked way of | 295 | // require not implementing From, and instead having some checked way of |
313 | // casting them, and somehow making the constructors private, which would be annoying. | 296 | // casting them, and somehow making the constructors private, which would be annoying. |
314 | impl_froms!(AssocItemId: FunctionId, ConstId, TypeAliasId); | 297 | impl_from!(FunctionId, ConstId, TypeAliasId for AssocItemId); |
315 | 298 | ||
316 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] | 299 | #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] |
317 | pub enum GenericDefId { | 300 | pub enum GenericDefId { |
@@ -326,14 +309,15 @@ pub enum GenericDefId { | |||
326 | // consts can have type parameters from their parents (i.e. associated consts of traits) | 309 | // consts can have type parameters from their parents (i.e. associated consts of traits) |
327 | ConstId(ConstId), | 310 | ConstId(ConstId), |
328 | } | 311 | } |
329 | impl_froms!( | 312 | impl_from!( |
330 | GenericDefId: FunctionId, | 313 | FunctionId, |
331 | AdtId(StructId, EnumId, UnionId), | 314 | AdtId(StructId, EnumId, UnionId), |
332 | TraitId, | 315 | TraitId, |
333 | TypeAliasId, | 316 | TypeAliasId, |
334 | ImplId, | 317 | ImplId, |
335 | EnumVariantId, | 318 | EnumVariantId, |
336 | ConstId | 319 | ConstId |
320 | for GenericDefId | ||
337 | ); | 321 | ); |
338 | 322 | ||
339 | impl From<AssocItemId> for GenericDefId { | 323 | impl From<AssocItemId> for GenericDefId { |
@@ -361,8 +345,8 @@ pub enum AttrDefId { | |||
361 | ImplId(ImplId), | 345 | ImplId(ImplId), |
362 | } | 346 | } |
363 | 347 | ||
364 | impl_froms!( | 348 | impl_from!( |
365 | AttrDefId: ModuleId, | 349 | ModuleId, |
366 | FieldId, | 350 | FieldId, |
367 | AdtId(StructId, EnumId, UnionId), | 351 | AdtId(StructId, EnumId, UnionId), |
368 | EnumVariantId, | 352 | EnumVariantId, |
@@ -373,6 +357,7 @@ impl_froms!( | |||
373 | TypeAliasId, | 357 | TypeAliasId, |
374 | MacroDefId, | 358 | MacroDefId, |
375 | ImplId | 359 | ImplId |
360 | for AttrDefId | ||
376 | ); | 361 | ); |
377 | 362 | ||
378 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 363 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -381,7 +366,7 @@ pub enum VariantId { | |||
381 | StructId(StructId), | 366 | StructId(StructId), |
382 | UnionId(UnionId), | 367 | UnionId(UnionId), |
383 | } | 368 | } |
384 | impl_froms!(VariantId: EnumVariantId, StructId, UnionId); | 369 | impl_from!(EnumVariantId, StructId, UnionId for VariantId); |
385 | 370 | ||
386 | trait Intern { | 371 | trait Intern { |
387 | type ID; | 372 | type ID; |
diff --git a/crates/ra_hir_ty/src/infer.rs b/crates/ra_hir_ty/src/infer.rs index 5c56c2eb0..2ce4f65cc 100644 --- a/crates/ra_hir_ty/src/infer.rs +++ b/crates/ra_hir_ty/src/infer.rs | |||
@@ -18,8 +18,6 @@ use std::mem; | |||
18 | use std::ops::Index; | 18 | use std::ops::Index; |
19 | use std::sync::Arc; | 19 | use std::sync::Arc; |
20 | 20 | ||
21 | use rustc_hash::FxHashMap; | ||
22 | |||
23 | use hir_def::{ | 21 | use hir_def::{ |
24 | body::Body, | 22 | body::Body, |
25 | data::{ConstData, FunctionData, StaticData}, | 23 | data::{ConstData, FunctionData, StaticData}, |
@@ -35,6 +33,8 @@ use hir_expand::{diagnostics::DiagnosticSink, name::name}; | |||
35 | use ra_arena::map::ArenaMap; | 33 | use ra_arena::map::ArenaMap; |
36 | use ra_prof::profile; | 34 | use ra_prof::profile; |
37 | use ra_syntax::SmolStr; | 35 | use ra_syntax::SmolStr; |
36 | use rustc_hash::FxHashMap; | ||
37 | use stdx::impl_from; | ||
38 | 38 | ||
39 | use super::{ | 39 | use super::{ |
40 | primitive::{FloatTy, IntTy}, | 40 | primitive::{FloatTy, IntTy}, |
@@ -84,8 +84,7 @@ enum ExprOrPatId { | |||
84 | ExprId(ExprId), | 84 | ExprId(ExprId), |
85 | PatId(PatId), | 85 | PatId(PatId), |
86 | } | 86 | } |
87 | 87 | impl_from!(ExprId, PatId for ExprOrPatId); | |
88 | impl_froms!(ExprOrPatId: ExprId, PatId); | ||
89 | 88 | ||
90 | /// Binding modes inferred for patterns. | 89 | /// Binding modes inferred for patterns. |
91 | /// https://doc.rust-lang.org/reference/patterns.html#binding-modes | 90 | /// https://doc.rust-lang.org/reference/patterns.html#binding-modes |
diff --git a/crates/ra_hir_ty/src/lib.rs b/crates/ra_hir_ty/src/lib.rs index 34f0bd4ce..2652d200f 100644 --- a/crates/ra_hir_ty/src/lib.rs +++ b/crates/ra_hir_ty/src/lib.rs | |||
@@ -6,25 +6,6 @@ macro_rules! eprintln { | |||
6 | ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; | 6 | ($($tt:tt)*) => { stdx::eprintln!($($tt)*) }; |
7 | } | 7 | } |
8 | 8 | ||
9 | macro_rules! impl_froms { | ||
10 | ($e:ident: $($v:ident $(($($sv:ident),*))?),*) => { | ||
11 | $( | ||
12 | impl From<$v> for $e { | ||
13 | fn from(it: $v) -> $e { | ||
14 | $e::$v(it) | ||
15 | } | ||
16 | } | ||
17 | $($( | ||
18 | impl From<$sv> for $e { | ||
19 | fn from(it: $sv) -> $e { | ||
20 | $e::$v($v::$sv(it)) | ||
21 | } | ||
22 | } | ||
23 | )*)? | ||
24 | )* | ||
25 | } | ||
26 | } | ||
27 | |||
28 | mod autoderef; | 9 | mod autoderef; |
29 | pub mod primitive; | 10 | pub mod primitive; |
30 | pub mod traits; | 11 | pub mod traits; |
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs index 3af8d55a1..101b8aebe 100644 --- a/crates/ra_hir_ty/src/lower.rs +++ b/crates/ra_hir_ty/src/lower.rs | |||
@@ -5,10 +5,7 @@ | |||
5 | //! - Building the type for an item: This happens through the `type_for_def` query. | 5 | //! - Building the type for an item: This happens through the `type_for_def` query. |
6 | //! | 6 | //! |
7 | //! This usually involves resolving names, collecting generic arguments etc. | 7 | //! This usually involves resolving names, collecting generic arguments etc. |
8 | use std::iter; | 8 | use std::{iter, sync::Arc}; |
9 | use std::sync::Arc; | ||
10 | |||
11 | use smallvec::SmallVec; | ||
12 | 9 | ||
13 | use hir_def::{ | 10 | use hir_def::{ |
14 | adt::StructKind, | 11 | adt::StructKind, |
@@ -24,6 +21,8 @@ use hir_def::{ | |||
24 | use hir_expand::name::Name; | 21 | use hir_expand::name::Name; |
25 | use ra_arena::map::ArenaMap; | 22 | use ra_arena::map::ArenaMap; |
26 | use ra_db::CrateId; | 23 | use ra_db::CrateId; |
24 | use smallvec::SmallVec; | ||
25 | use stdx::impl_from; | ||
27 | use test_utils::mark; | 26 | use test_utils::mark; |
28 | 27 | ||
29 | use crate::{ | 28 | use crate::{ |
@@ -1110,7 +1109,7 @@ pub enum CallableDef { | |||
1110 | StructId(StructId), | 1109 | StructId(StructId), |
1111 | EnumVariantId(EnumVariantId), | 1110 | EnumVariantId(EnumVariantId), |
1112 | } | 1111 | } |
1113 | impl_froms!(CallableDef: FunctionId, StructId, EnumVariantId); | 1112 | impl_from!(FunctionId, StructId, EnumVariantId for CallableDef); |
1114 | 1113 | ||
1115 | impl CallableDef { | 1114 | impl CallableDef { |
1116 | pub fn krate(self, db: &dyn HirDatabase) -> CrateId { | 1115 | pub fn krate(self, db: &dyn HirDatabase) -> CrateId { |
@@ -1140,7 +1139,7 @@ pub enum TyDefId { | |||
1140 | AdtId(AdtId), | 1139 | AdtId(AdtId), |
1141 | TypeAliasId(TypeAliasId), | 1140 | TypeAliasId(TypeAliasId), |
1142 | } | 1141 | } |
1143 | impl_froms!(TyDefId: BuiltinType, AdtId(StructId, EnumId, UnionId), TypeAliasId); | 1142 | impl_from!(BuiltinType, AdtId(StructId, EnumId, UnionId), TypeAliasId for TyDefId); |
1144 | 1143 | ||
1145 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 1144 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
1146 | pub enum ValueTyDefId { | 1145 | pub enum ValueTyDefId { |
@@ -1150,7 +1149,7 @@ pub enum ValueTyDefId { | |||
1150 | ConstId(ConstId), | 1149 | ConstId(ConstId), |
1151 | StaticId(StaticId), | 1150 | StaticId(StaticId), |
1152 | } | 1151 | } |
1153 | impl_froms!(ValueTyDefId: FunctionId, StructId, EnumVariantId, ConstId, StaticId); | 1152 | impl_from!(FunctionId, StructId, EnumVariantId, ConstId, StaticId for ValueTyDefId); |
1154 | 1153 | ||
1155 | /// Build the declared type of an item. This depends on the namespace; e.g. for | 1154 | /// Build the declared type of an item. This depends on the namespace; e.g. for |
1156 | /// `struct Foo(usize)`, we have two types: The type of the struct itself, and | 1155 | /// `struct Foo(usize)`, we have two types: The type of the struct itself, and |
diff --git a/crates/ra_tt/Cargo.toml b/crates/ra_tt/Cargo.toml index f7230a9ca..b5b12e3af 100644 --- a/crates/ra_tt/Cargo.toml +++ b/crates/ra_tt/Cargo.toml | |||
@@ -8,6 +8,7 @@ authors = ["rust-analyzer developers"] | |||
8 | doctest = false | 8 | doctest = false |
9 | 9 | ||
10 | [dependencies] | 10 | [dependencies] |
11 | stdx = { path = "../stdx" } | ||
11 | # ideally, `serde` should be enabled by `rust-analyzer`, but we enable it here | 12 | # ideally, `serde` should be enabled by `rust-analyzer`, but we enable it here |
12 | # to reduce number of compilations | 13 | # to reduce number of compilations |
13 | smol_str = { version = "0.1.15", features = ["serde"] } | 14 | smol_str = { version = "0.1.15", features = ["serde"] } |
diff --git a/crates/ra_tt/src/lib.rs b/crates/ra_tt/src/lib.rs index 342ddbe32..8faf1cc67 100644 --- a/crates/ra_tt/src/lib.rs +++ b/crates/ra_tt/src/lib.rs | |||
@@ -1,24 +1,13 @@ | |||
1 | //! `tt` crate defines a `TokenTree` data structure: this is the interface (both | 1 | //! `tt` crate defines a `TokenTree` data structure: this is the interface (both |
2 | //! input and output) of macros. It closely mirrors `proc_macro` crate's | 2 | //! input and output) of macros. It closely mirrors `proc_macro` crate's |
3 | //! `TokenTree`. | 3 | //! `TokenTree`. |
4 | |||
5 | macro_rules! impl_froms { | ||
6 | ($e:ident: $($v:ident), *) => { | ||
7 | $( | ||
8 | impl From<$v> for $e { | ||
9 | fn from(it: $v) -> $e { | ||
10 | $e::$v(it) | ||
11 | } | ||
12 | } | ||
13 | )* | ||
14 | } | ||
15 | } | ||
16 | |||
17 | use std::{ | 4 | use std::{ |
18 | fmt::{self, Debug}, | 5 | fmt::{self, Debug}, |
19 | panic::RefUnwindSafe, | 6 | panic::RefUnwindSafe, |
20 | }; | 7 | }; |
21 | 8 | ||
9 | use stdx::impl_from; | ||
10 | |||
22 | pub use smol_str::SmolStr; | 11 | pub use smol_str::SmolStr; |
23 | 12 | ||
24 | /// Represents identity of the token. | 13 | /// Represents identity of the token. |
@@ -41,7 +30,7 @@ pub enum TokenTree { | |||
41 | Leaf(Leaf), | 30 | Leaf(Leaf), |
42 | Subtree(Subtree), | 31 | Subtree(Subtree), |
43 | } | 32 | } |
44 | impl_froms!(TokenTree: Leaf, Subtree); | 33 | impl_from!(Leaf, Subtree for TokenTree); |
45 | 34 | ||
46 | impl TokenTree { | 35 | impl TokenTree { |
47 | pub fn empty() -> Self { | 36 | pub fn empty() -> Self { |
@@ -55,7 +44,7 @@ pub enum Leaf { | |||
55 | Punct(Punct), | 44 | Punct(Punct), |
56 | Ident(Ident), | 45 | Ident(Ident), |
57 | } | 46 | } |
58 | impl_froms!(Leaf: Literal, Punct, Ident); | 47 | impl_from!(Literal, Punct, Ident for Leaf); |
59 | 48 | ||
60 | #[derive(Clone, PartialEq, Eq, Hash, Default)] | 49 | #[derive(Clone, PartialEq, Eq, Hash, Default)] |
61 | pub struct Subtree { | 50 | pub struct Subtree { |
diff --git a/crates/stdx/src/macros.rs b/crates/stdx/src/macros.rs index 0f7690a67..bf298460f 100644 --- a/crates/stdx/src/macros.rs +++ b/crates/stdx/src/macros.rs | |||
@@ -17,3 +17,24 @@ macro_rules! format_to { | |||
17 | { use ::std::fmt::Write as _; let _ = ::std::write!($buf, $lit $($arg)*); } | 17 | { use ::std::fmt::Write as _; let _ = ::std::write!($buf, $lit $($arg)*); } |
18 | }; | 18 | }; |
19 | } | 19 | } |
20 | |||
21 | // Generates `From` impls for `Enum E { Foo(Foo), Bar(Bar) }` enums | ||
22 | #[macro_export] | ||
23 | macro_rules! impl_from { | ||
24 | ($($variant:ident $(($($sub_variant:ident),*))?),* for $enum:ident) => { | ||
25 | $( | ||
26 | impl From<$variant> for $enum { | ||
27 | fn from(it: $variant) -> $enum { | ||
28 | $enum::$variant(it) | ||
29 | } | ||
30 | } | ||
31 | $($( | ||
32 | impl From<$sub_variant> for $enum { | ||
33 | fn from(it: $sub_variant) -> $enum { | ||
34 | $enum::$variant($variant::$sub_variant(it)) | ||
35 | } | ||
36 | } | ||
37 | )*)? | ||
38 | )* | ||
39 | } | ||
40 | } | ||