@@ -5,11 +5,7 @@ use std::sync::Weak;
5
5
use strum:: IntoEnumIterator ;
6
6
7
7
use crate :: ops:: { ExtensionOp , OpName , OpNameRef } ;
8
- use crate :: {
9
- ops:: { NamedOp , OpType } ,
10
- types:: TypeArg ,
11
- Extension ,
12
- } ;
8
+ use crate :: { ops:: OpType , types:: TypeArg , Extension } ;
13
9
14
10
use super :: { op_def:: SignatureFunc , ExtensionBuildError , ExtensionId , OpDef , SignatureError } ;
15
11
use delegate:: delegate;
@@ -29,22 +25,24 @@ pub enum OpLoadError {
29
25
WrongExtension ( ExtensionId , ExtensionId ) ,
30
26
}
31
27
32
- impl < T > NamedOp for T
33
- where
34
- for < ' a > & ' a T : Into < & ' static str > ,
35
- {
36
- fn name ( & self ) -> OpName {
37
- let s = self . into ( ) ;
38
- s. into ( )
39
- }
40
- }
41
-
42
28
/// Traits implemented by types which can add themselves to [`Extension`]s as
43
29
/// [`OpDef`]s or load themselves from an [`OpDef`].
44
30
///
45
31
/// Particularly useful with C-style enums that implement [strum::IntoEnumIterator],
46
32
/// as then all definitions can be added to an extension at once.
47
- pub trait MakeOpDef : NamedOp {
33
+ ///
34
+ /// [MakeExtensionOp] has a blanket impl for types that impl [MakeOpDef].
35
+ pub trait MakeOpDef {
36
+ /// The [OpDef::name] which will be used when `Self` is added to an [Extension]
37
+ /// or when `Self` is loaded from an [OpDef].
38
+ ///
39
+ /// This identifer must be unique within the extension with which the
40
+ /// [OpDef] is registered. An [ExtensionOp] instantiating this [OpDef] will
41
+ /// report `self.opdef_id()` as its [ExtensionOp::unqualified_id].
42
+ ///
43
+ /// [MakeExtensionOp::op_id] must match this function.
44
+ fn opdef_id ( & self ) -> OpName ;
45
+
48
46
/// Try to load one of the operations of this set from an [OpDef].
49
47
fn from_def ( op_def : & OpDef ) -> Result < Self , OpLoadError >
50
48
where
@@ -68,9 +66,9 @@ pub trait MakeOpDef: NamedOp {
68
66
self . init_signature ( & self . extension_ref ( ) )
69
67
}
70
68
71
- /// Description of the operation. By default, the same as `self.name ()`.
69
+ /// Description of the operation. By default, the same as `self.opdef_id ()`.
72
70
fn description ( & self ) -> String {
73
- self . name ( ) . to_string ( )
71
+ self . opdef_id ( ) . to_string ( )
74
72
}
75
73
76
74
/// Edit the opdef before finalising. By default does nothing.
@@ -87,7 +85,7 @@ pub trait MakeOpDef: NamedOp {
87
85
extension_ref : & Weak < Extension > ,
88
86
) -> Result < ( ) , ExtensionBuildError > {
89
87
let def = extension. add_op (
90
- self . name ( ) ,
88
+ self . opdef_id ( ) ,
91
89
self . description ( ) ,
92
90
self . init_signature ( extension_ref) ,
93
91
extension_ref,
@@ -150,7 +148,14 @@ pub trait HasDef: MakeExtensionOp {
150
148
151
149
/// Traits implemented by types which can be loaded from [`ExtensionOp`]s,
152
150
/// i.e. concrete instances of [`OpDef`]s, with defined type arguments.
153
- pub trait MakeExtensionOp : NamedOp {
151
+ pub trait MakeExtensionOp {
152
+ /// The [OpDef::name] of [ExtensionOp]s from which `Self` can be loaded.
153
+ ///
154
+ /// This identifer must be unique within the extension with which the
155
+ /// [OpDef] is registered. An [ExtensionOp] instantiating this [OpDef] will
156
+ /// report `self.opdef_id()` as its [ExtensionOp::unqualified_id].
157
+ fn op_id ( & self ) -> OpName ;
158
+
154
159
/// Try to load one of the operations of this set from an [OpDef].
155
160
fn from_extension_op ( ext_op : & ExtensionOp ) -> Result < Self , OpLoadError >
156
161
where
@@ -188,6 +193,10 @@ pub trait MakeExtensionOp: NamedOp {
188
193
189
194
/// Blanket implementation for non-polymorphic operations - [OpDef]s with no type parameters.
190
195
impl < T : MakeOpDef > MakeExtensionOp for T {
196
+ fn op_id ( & self ) -> OpName {
197
+ self . opdef_id ( )
198
+ }
199
+
191
200
#[ inline]
192
201
fn from_extension_op ( ext_op : & ExtensionOp ) -> Result < Self , OpLoadError >
193
202
where
@@ -243,7 +252,7 @@ impl<T: MakeExtensionOp> RegisteredOp<T> {
243
252
/// Generate an [OpType].
244
253
pub fn to_extension_op ( & self ) -> Option < ExtensionOp > {
245
254
ExtensionOp :: new (
246
- self . extension . upgrade ( ) ?. get_op ( & self . name ( ) ) ?. clone ( ) ,
255
+ self . extension . upgrade ( ) ?. get_op ( & self . op_id ( ) ) ?. clone ( ) ,
247
256
self . type_args ( ) ,
248
257
)
249
258
. ok ( )
@@ -252,7 +261,7 @@ impl<T: MakeExtensionOp> RegisteredOp<T> {
252
261
delegate ! {
253
262
to self . op {
254
263
/// Name of the operation - derived from strum serialization.
255
- pub fn name ( & self ) -> OpName ;
264
+ pub fn op_id ( & self ) -> OpName ;
256
265
/// Any type args which define this operation. Default is no type arguments.
257
266
pub fn type_args( & self ) -> Vec <TypeArg >;
258
267
}
@@ -310,6 +319,10 @@ mod test {
310
319
}
311
320
312
321
impl MakeOpDef for DummyEnum {
322
+ fn opdef_id ( & self ) -> OpName {
323
+ <& ' static str >:: from ( self ) . into ( )
324
+ }
325
+
313
326
fn init_signature ( & self , _extension_ref : & Weak < Extension > ) -> SignatureFunc {
314
327
Signature :: new_endo ( type_row ! [ ] ) . into ( )
315
328
}
@@ -366,7 +379,7 @@ mod test {
366
379
let o = DummyEnum :: Dumb ;
367
380
368
381
assert_eq ! (
369
- DummyEnum :: from_def( EXT . get_op( & o. name ( ) ) . unwrap( ) ) . unwrap( ) ,
382
+ DummyEnum :: from_def( EXT . get_op( & o. opdef_id ( ) ) . unwrap( ) ) . unwrap( ) ,
370
383
o
371
384
) ;
372
385
0 commit comments