17
17
18
18
//! Convert a value implementing [`StarlarkValue`] into a type usable in type expression.
19
19
20
+ use std:: fmt;
21
+ use std:: fmt:: Debug ;
22
+ use std:: fmt:: Display ;
23
+ use std:: fmt:: Formatter ;
20
24
use std:: marker:: PhantomData ;
21
25
22
26
use allocative:: Allocative ;
23
27
use starlark_derive:: starlark_value;
24
28
use starlark_derive:: NoSerialize ;
25
- use starlark_derive:: ProvidesStaticType ;
26
29
27
30
use crate as starlark;
31
+ use crate :: any:: ProvidesStaticType ;
28
32
use crate :: typing:: Ty ;
29
33
use crate :: values:: layout:: avalue:: alloc_static;
30
34
use crate :: values:: layout:: avalue:: AValueImpl ;
31
35
use crate :: values:: layout:: avalue:: Basic ;
32
36
use crate :: values:: layout:: heap:: repr:: AValueRepr ;
33
- use crate :: values:: string:: StarlarkStr ;
34
37
use crate :: values:: type_repr:: StarlarkTypeRepr ;
35
38
use crate :: values:: AllocFrozenValue ;
36
39
use crate :: values:: FrozenHeap ;
37
40
use crate :: values:: FrozenValue ;
38
41
use crate :: values:: StarlarkValue ;
39
42
43
+ #[ derive( Debug , NoSerialize , Allocative , ProvidesStaticType ) ]
44
+ struct StarlarkValueAsTypeStarlarkValue ( fn ( ) -> Ty ) ;
45
+
46
+ #[ starlark_value( type = "type" ) ]
47
+ impl < ' v > StarlarkValue < ' v > for StarlarkValueAsTypeStarlarkValue {
48
+ fn eval_type ( & self ) -> Option < Ty > {
49
+ Some ( ( self . 0 ) ( ) )
50
+ }
51
+ }
52
+
53
+ impl Display for StarlarkValueAsTypeStarlarkValue {
54
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
55
+ Display :: fmt ( & ( self . 0 ) ( ) , f)
56
+ }
57
+ }
58
+
40
59
/// Utility to declare a value usable in type expression.
41
60
///
42
61
/// # Example
@@ -63,44 +82,50 @@ use crate::values::StarlarkValue;
63
82
/// const Temperature: StarlarkValueAsType<Temperature> = StarlarkValueAsType::new();
64
83
/// }
65
84
/// ```
66
- #[ derive(
67
- Debug ,
68
- derive_more:: Display ,
69
- Allocative ,
70
- ProvidesStaticType ,
71
- NoSerialize
72
- ) ]
73
- #[ display( fmt = "{}" , T :: TYPE ) ]
74
- pub struct StarlarkValueAsType < T : StarlarkValue < ' static > + ' static > ( PhantomData < fn ( & T ) > ) ;
75
-
76
- impl < T : StarlarkValue < ' static > > StarlarkValueAsType < T > {
85
+ pub struct StarlarkValueAsType < T : StarlarkTypeRepr > ( PhantomData < fn ( & T ) > ) ;
86
+
87
+ impl < T : StarlarkTypeRepr > Debug for StarlarkValueAsType < T > {
88
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
89
+ f. debug_tuple ( "StarlarkValueAsType" )
90
+ . field ( & T :: starlark_type_repr ( ) )
91
+ . finish ( )
92
+ }
93
+ }
94
+
95
+ impl < T : StarlarkTypeRepr > Display for StarlarkValueAsType < T > {
96
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
97
+ Display :: fmt ( & T :: starlark_type_repr ( ) , f)
98
+ }
99
+ }
100
+
101
+ impl < T : StarlarkTypeRepr > StarlarkValueAsType < T > {
77
102
/// Constructor.
78
103
pub const fn new ( ) -> Self {
79
104
Self ( PhantomData )
80
105
}
81
106
82
- const INSTANCE : AValueRepr < AValueImpl < Basic , StarlarkValueAsType < T > > > =
83
- alloc_static ( Basic , StarlarkValueAsType :: < T > ( PhantomData ) ) ;
107
+ const INSTANCE : AValueRepr < AValueImpl < Basic , StarlarkValueAsTypeStarlarkValue > > = alloc_static (
108
+ Basic ,
109
+ StarlarkValueAsTypeStarlarkValue ( T :: starlark_type_repr) ,
110
+ ) ;
84
111
}
85
112
86
- impl < T : StarlarkValue < ' static > > Default for StarlarkValueAsType < T > {
113
+ impl < T : StarlarkTypeRepr > Default for StarlarkValueAsType < T > {
87
114
fn default ( ) -> Self {
88
115
Self :: new ( )
89
116
}
90
117
}
91
118
92
- impl < T : StarlarkValue < ' static > > AllocFrozenValue for StarlarkValueAsType < T > {
93
- fn alloc_frozen_value ( self , _heap : & FrozenHeap ) -> FrozenValue {
94
- FrozenValue :: new_repr ( & Self :: INSTANCE )
119
+ impl < T : StarlarkTypeRepr > StarlarkTypeRepr for StarlarkValueAsType < T > {
120
+ fn starlark_type_repr ( ) -> Ty {
121
+ // TODO(nga): make it proper type.
122
+ Ty :: name_static ( "type" )
95
123
}
96
124
}
97
125
98
- #[ starlark_value( type = "type" ) ]
99
- impl < ' v , T : StarlarkValue < ' static > > StarlarkValue < ' v > for StarlarkValueAsType < T > {
100
- type Canonical = StarlarkValueAsType < StarlarkStr > ;
101
-
102
- fn eval_type ( & self ) -> Option < Ty > {
103
- Some ( T :: starlark_type_repr ( ) )
126
+ impl < T : StarlarkTypeRepr > AllocFrozenValue for StarlarkValueAsType < T > {
127
+ fn alloc_frozen_value ( self , _heap : & FrozenHeap ) -> FrozenValue {
128
+ FrozenValue :: new_repr ( & Self :: INSTANCE )
104
129
}
105
130
}
106
131
0 commit comments