@@ -5,11 +5,7 @@ use std::sync::Weak;
55use strum:: IntoEnumIterator ;
66
77use 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 } ;
139
1410use super :: { op_def:: SignatureFunc , ExtensionBuildError , ExtensionId , OpDef , SignatureError } ;
1511use delegate:: delegate;
@@ -29,22 +25,24 @@ pub enum OpLoadError {
2925 WrongExtension ( ExtensionId , ExtensionId ) ,
3026}
3127
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-
4228/// Traits implemented by types which can add themselves to [`Extension`]s as
4329/// [`OpDef`]s or load themselves from an [`OpDef`].
4430///
4531/// Particularly useful with C-style enums that implement [strum::IntoEnumIterator],
4632/// 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+
4846 /// Try to load one of the operations of this set from an [OpDef].
4947 fn from_def ( op_def : & OpDef ) -> Result < Self , OpLoadError >
5048 where
@@ -68,9 +66,9 @@ pub trait MakeOpDef: NamedOp {
6866 self . init_signature ( & self . extension_ref ( ) )
6967 }
7068
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 ()`.
7270 fn description ( & self ) -> String {
73- self . name ( ) . to_string ( )
71+ self . opdef_id ( ) . to_string ( )
7472 }
7573
7674 /// Edit the opdef before finalising. By default does nothing.
@@ -87,7 +85,7 @@ pub trait MakeOpDef: NamedOp {
8785 extension_ref : & Weak < Extension > ,
8886 ) -> Result < ( ) , ExtensionBuildError > {
8987 let def = extension. add_op (
90- self . name ( ) ,
88+ self . opdef_id ( ) ,
9189 self . description ( ) ,
9290 self . init_signature ( extension_ref) ,
9391 extension_ref,
@@ -150,7 +148,14 @@ pub trait HasDef: MakeExtensionOp {
150148
151149/// Traits implemented by types which can be loaded from [`ExtensionOp`]s,
152150/// 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+
154159 /// Try to load one of the operations of this set from an [OpDef].
155160 fn from_extension_op ( ext_op : & ExtensionOp ) -> Result < Self , OpLoadError >
156161 where
@@ -188,6 +193,10 @@ pub trait MakeExtensionOp: NamedOp {
188193
189194/// Blanket implementation for non-polymorphic operations - [OpDef]s with no type parameters.
190195impl < T : MakeOpDef > MakeExtensionOp for T {
196+ fn op_id ( & self ) -> OpName {
197+ self . opdef_id ( )
198+ }
199+
191200 #[ inline]
192201 fn from_extension_op ( ext_op : & ExtensionOp ) -> Result < Self , OpLoadError >
193202 where
@@ -243,7 +252,7 @@ impl<T: MakeExtensionOp> RegisteredOp<T> {
243252 /// Generate an [OpType].
244253 pub fn to_extension_op ( & self ) -> Option < ExtensionOp > {
245254 ExtensionOp :: new (
246- self . extension . upgrade ( ) ?. get_op ( & self . name ( ) ) ?. clone ( ) ,
255+ self . extension . upgrade ( ) ?. get_op ( & self . op_id ( ) ) ?. clone ( ) ,
247256 self . type_args ( ) ,
248257 )
249258 . ok ( )
@@ -252,7 +261,7 @@ impl<T: MakeExtensionOp> RegisteredOp<T> {
252261 delegate ! {
253262 to self . op {
254263 /// Name of the operation - derived from strum serialization.
255- pub fn name ( & self ) -> OpName ;
264+ pub fn op_id ( & self ) -> OpName ;
256265 /// Any type args which define this operation. Default is no type arguments.
257266 pub fn type_args( & self ) -> Vec <TypeArg >;
258267 }
@@ -310,6 +319,10 @@ mod test {
310319 }
311320
312321 impl MakeOpDef for DummyEnum {
322+ fn opdef_id ( & self ) -> OpName {
323+ <& ' static str >:: from ( self ) . into ( )
324+ }
325+
313326 fn init_signature ( & self , _extension_ref : & Weak < Extension > ) -> SignatureFunc {
314327 Signature :: new_endo ( type_row ! [ ] ) . into ( )
315328 }
@@ -366,7 +379,7 @@ mod test {
366379 let o = DummyEnum :: Dumb ;
367380
368381 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( ) ,
370383 o
371384 ) ;
372385
0 commit comments