1
+ use crate :: lang_items:: TraitSolverLangItem :: { EffectsMaybe , EffectsRuntime , EffectsNoRuntime } ;
2
+ use crate :: Interner ;
3
+ use crate :: inherent:: { AdtDef , IntoKind , Ty } ;
4
+
5
+ #[ derive( Clone , Copy , PartialEq , Eq ) ]
6
+ pub enum EffectKind {
7
+ Maybe ,
8
+ Runtime ,
9
+ NoRuntime ,
10
+ }
11
+
12
+ impl EffectKind {
13
+ pub fn try_from_def_id < I : Interner > ( tcx : I , def_id : I :: DefId ) -> Option < EffectKind > {
14
+ if tcx. is_lang_item ( def_id, EffectsMaybe ) {
15
+ Some ( EffectKind :: Maybe )
16
+ } else if tcx. is_lang_item ( def_id, EffectsRuntime ) {
17
+ Some ( EffectKind :: Runtime )
18
+ } else if tcx. is_lang_item ( def_id, EffectsNoRuntime ) {
19
+ Some ( EffectKind :: NoRuntime )
20
+ } else {
21
+ None
22
+ }
23
+ }
24
+
25
+ pub fn to_def_id < I : Interner > ( self , tcx : I ) -> I :: DefId {
26
+ let lang_item = match self {
27
+ EffectKind :: Maybe => EffectsMaybe ,
28
+ EffectKind :: NoRuntime => EffectsNoRuntime ,
29
+ EffectKind :: Runtime => EffectsRuntime ,
30
+ } ;
31
+
32
+ tcx. require_lang_item ( lang_item)
33
+ }
34
+
35
+ pub fn try_from_ty < I : Interner > ( tcx : I , ty : I :: Ty ) -> Option < EffectKind > {
36
+ if let crate :: Adt ( def, _) = ty. kind ( ) {
37
+ Self :: try_from_def_id ( tcx, def. def_id ( ) )
38
+ } else {
39
+ None
40
+ }
41
+ }
42
+
43
+ pub fn to_ty < I : Interner > ( self , tcx : I ) -> I :: Ty {
44
+ I :: Ty :: new_adt ( tcx, tcx. adt_def ( self . to_def_id ( tcx) ) , Default :: default ( ) )
45
+ }
46
+
47
+ pub fn min ( a : Self , b : Self ) -> Option < Self > {
48
+ use EffectKind :: * ;
49
+ match ( a, b) {
50
+ ( Maybe , x) | ( x, Maybe ) => Some ( x) ,
51
+ ( Runtime , Runtime ) => Some ( Runtime ) ,
52
+ ( NoRuntime , NoRuntime ) => Some ( NoRuntime ) ,
53
+ ( Runtime , NoRuntime ) | ( NoRuntime , Runtime ) => None ,
54
+ }
55
+ }
56
+ }
0 commit comments