@@ -30,9 +30,18 @@ pub enum OpLoadError {
30
30
///
31
31
/// Particularly useful with C-style enums that implement [strum::IntoEnumIterator],
32
32
/// as then all definitions can be added to an extension at once.
33
+ ///
34
+ /// [MakeExtensionOp] has a blanket impl for types that impl [MakeOpDef].
33
35
pub trait MakeOpDef {
34
- /// TODO docs
35
- fn opdef_name ( & self ) -> OpName ;
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 ;
36
45
37
46
/// Try to load one of the operations of this set from an [OpDef].
38
47
fn from_def ( op_def : & OpDef ) -> Result < Self , OpLoadError >
@@ -57,9 +66,9 @@ pub trait MakeOpDef {
57
66
self . init_signature ( & self . extension_ref ( ) )
58
67
}
59
68
60
- /// Description of the operation. By default, the same as `self.name ()`.
69
+ /// Description of the operation. By default, the same as `self.opdef_id ()`.
61
70
fn description ( & self ) -> String {
62
- self . opdef_name ( ) . to_string ( )
71
+ self . opdef_id ( ) . to_string ( )
63
72
}
64
73
65
74
/// Edit the opdef before finalising. By default does nothing.
@@ -76,7 +85,7 @@ pub trait MakeOpDef {
76
85
extension_ref : & Weak < Extension > ,
77
86
) -> Result < ( ) , ExtensionBuildError > {
78
87
let def = extension. add_op (
79
- self . opdef_name ( ) ,
88
+ self . opdef_id ( ) ,
80
89
self . description ( ) ,
81
90
self . init_signature ( extension_ref) ,
82
91
extension_ref,
@@ -140,8 +149,12 @@ pub trait HasDef: MakeExtensionOp {
140
149
/// Traits implemented by types which can be loaded from [`ExtensionOp`]s,
141
150
/// i.e. concrete instances of [`OpDef`]s, with defined type arguments.
142
151
pub trait MakeExtensionOp {
143
- /// The name of the operation
144
- fn name ( & self ) -> OpName ;
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 ;
145
158
146
159
/// Try to load one of the operations of this set from an [OpDef].
147
160
fn from_extension_op ( ext_op : & ExtensionOp ) -> Result < Self , OpLoadError >
@@ -180,8 +193,8 @@ pub trait MakeExtensionOp {
180
193
181
194
/// Blanket implementation for non-polymorphic operations - [OpDef]s with no type parameters.
182
195
impl < T : MakeOpDef > MakeExtensionOp for T {
183
- fn name ( & self ) -> OpName {
184
- < Self as MakeOpDef > :: opdef_name ( self )
196
+ fn op_id ( & self ) -> OpName {
197
+ self . opdef_id ( )
185
198
}
186
199
187
200
#[ inline]
@@ -239,7 +252,7 @@ impl<T: MakeExtensionOp> RegisteredOp<T> {
239
252
/// Generate an [OpType].
240
253
pub fn to_extension_op ( & self ) -> Option < ExtensionOp > {
241
254
ExtensionOp :: new (
242
- self . extension . upgrade ( ) ?. get_op ( & self . name ( ) ) ?. clone ( ) ,
255
+ self . extension . upgrade ( ) ?. get_op ( & self . op_id ( ) ) ?. clone ( ) ,
243
256
self . type_args ( ) ,
244
257
)
245
258
. ok ( )
@@ -248,7 +261,7 @@ impl<T: MakeExtensionOp> RegisteredOp<T> {
248
261
delegate ! {
249
262
to self . op {
250
263
/// Name of the operation - derived from strum serialization.
251
- pub fn name ( & self ) -> OpName ;
264
+ pub fn op_id ( & self ) -> OpName ;
252
265
/// Any type args which define this operation. Default is no type arguments.
253
266
pub fn type_args( & self ) -> Vec <TypeArg >;
254
267
}
@@ -306,8 +319,8 @@ mod test {
306
319
}
307
320
308
321
impl MakeOpDef for DummyEnum {
309
- fn opdef_name ( & self ) -> OpName {
310
- <& Self as Into < & ' static str > > :: into ( self ) . into ( )
322
+ fn opdef_id ( & self ) -> OpName {
323
+ <& ' static str >:: from ( self ) . into ( )
311
324
}
312
325
313
326
fn init_signature ( & self , _extension_ref : & Weak < Extension > ) -> SignatureFunc {
@@ -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. opdef_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