@@ -38,11 +38,11 @@ pub mod declarative;
38
38
39
39
/// Extension Registries store extensions to be looked up e.g. during validation.
40
40
#[ derive( Clone , Debug , PartialEq ) ]
41
- pub struct ExtensionRegistry ( BTreeMap < ExtensionId , Extension > ) ;
41
+ pub struct ExtensionRegistry ( BTreeMap < ExtensionId , Arc < Extension > > ) ;
42
42
43
43
impl ExtensionRegistry {
44
44
/// Gets the Extension with the given name
45
- pub fn get ( & self , name : & str ) -> Option < & Extension > {
45
+ pub fn get ( & self , name : & str ) -> Option < & Arc < Extension > > {
46
46
self . 0 . get ( name)
47
47
}
48
48
@@ -51,9 +51,9 @@ impl ExtensionRegistry {
51
51
self . 0 . contains_key ( name)
52
52
}
53
53
54
- /// Makes a new ExtensionRegistry, validating all the extensions in it
54
+ /// Makes a new [ ExtensionRegistry] , validating all the extensions in it.
55
55
pub fn try_new (
56
- value : impl IntoIterator < Item = Extension > ,
56
+ value : impl IntoIterator < Item = Arc < Extension > > ,
57
57
) -> Result < Self , ExtensionRegistryError > {
58
58
let mut res = ExtensionRegistry ( BTreeMap :: new ( ) ) ;
59
59
@@ -70,20 +70,28 @@ impl ExtensionRegistry {
70
70
ext. validate ( & res)
71
71
. map_err ( |e| ExtensionRegistryError :: InvalidSignature ( ext. name ( ) . clone ( ) , e) ) ?;
72
72
}
73
+
73
74
Ok ( res)
74
75
}
75
76
76
77
/// Registers a new extension to the registry.
77
78
///
78
79
/// Returns a reference to the registered extension if successful.
79
- pub fn register ( & mut self , extension : Extension ) -> Result < & Extension , ExtensionRegistryError > {
80
+ pub fn register (
81
+ & mut self ,
82
+ extension : impl Into < Arc < Extension > > ,
83
+ ) -> Result < ( ) , ExtensionRegistryError > {
84
+ let extension = extension. into ( ) ;
80
85
match self . 0 . entry ( extension. name ( ) . clone ( ) ) {
81
86
btree_map:: Entry :: Occupied ( prev) => Err ( ExtensionRegistryError :: AlreadyRegistered (
82
87
extension. name ( ) . clone ( ) ,
83
88
prev. get ( ) . version ( ) . clone ( ) ,
84
89
extension. version ( ) . clone ( ) ,
85
90
) ) ,
86
- btree_map:: Entry :: Vacant ( ve) => Ok ( ve. insert ( extension) ) ,
91
+ btree_map:: Entry :: Vacant ( ve) => {
92
+ ve. insert ( extension) ;
93
+ Ok ( ( ) )
94
+ }
87
95
}
88
96
}
89
97
@@ -93,21 +101,24 @@ impl ExtensionRegistry {
93
101
/// If versions match, the original extension is kept.
94
102
/// Returns a reference to the registered extension if successful.
95
103
///
96
- /// Avoids cloning the extension unless required. For a reference version see
104
+ /// Takes an Arc to the extension. To avoid cloning Arcs unless necessary, see
97
105
/// [`ExtensionRegistry::register_updated_ref`].
98
106
pub fn register_updated (
99
107
& mut self ,
100
- extension : Extension ,
101
- ) -> Result < & Extension , ExtensionRegistryError > {
108
+ extension : impl Into < Arc < Extension > > ,
109
+ ) -> Result < ( ) , ExtensionRegistryError > {
110
+ let extension = extension. into ( ) ;
102
111
match self . 0 . entry ( extension. name ( ) . clone ( ) ) {
103
112
btree_map:: Entry :: Occupied ( mut prev) => {
104
113
if prev. get ( ) . version ( ) < extension. version ( ) {
105
114
* prev. get_mut ( ) = extension;
106
115
}
107
- Ok ( prev. into_mut ( ) )
108
116
}
109
- btree_map:: Entry :: Vacant ( ve) => Ok ( ve. insert ( extension) ) ,
117
+ btree_map:: Entry :: Vacant ( ve) => {
118
+ ve. insert ( extension) ;
119
+ }
110
120
}
121
+ Ok ( ( ) )
111
122
}
112
123
113
124
/// Registers a new extension to the registry, keeping most up to date if
@@ -117,21 +128,23 @@ impl ExtensionRegistry {
117
128
/// If versions match, the original extension is kept. Returns a reference
118
129
/// to the registered extension if successful.
119
130
///
120
- /// Clones the extension if required. For no-cloning version see
131
+ /// Clones the Arc only when required. For no-cloning version see
121
132
/// [`ExtensionRegistry::register_updated`].
122
133
pub fn register_updated_ref (
123
134
& mut self ,
124
- extension : & Extension ,
125
- ) -> Result < & Extension , ExtensionRegistryError > {
135
+ extension : & Arc < Extension > ,
136
+ ) -> Result < ( ) , ExtensionRegistryError > {
126
137
match self . 0 . entry ( extension. name ( ) . clone ( ) ) {
127
138
btree_map:: Entry :: Occupied ( mut prev) => {
128
139
if prev. get ( ) . version ( ) < extension. version ( ) {
129
140
* prev. get_mut ( ) = extension. clone ( ) ;
130
141
}
131
- Ok ( prev. into_mut ( ) )
132
142
}
133
- btree_map:: Entry :: Vacant ( ve) => Ok ( ve. insert ( extension. clone ( ) ) ) ,
143
+ btree_map:: Entry :: Vacant ( ve) => {
144
+ ve. insert ( extension. clone ( ) ) ;
145
+ }
134
146
}
147
+ Ok ( ( ) )
135
148
}
136
149
137
150
/// Returns the number of extensions in the registry.
@@ -145,20 +158,20 @@ impl ExtensionRegistry {
145
158
}
146
159
147
160
/// Returns an iterator over the extensions in the registry.
148
- pub fn iter ( & self ) -> impl Iterator < Item = ( & ExtensionId , & Extension ) > {
161
+ pub fn iter ( & self ) -> impl Iterator < Item = ( & ExtensionId , & Arc < Extension > ) > {
149
162
self . 0 . iter ( )
150
163
}
151
164
152
165
/// Delete an extension from the registry and return it if it was present.
153
- pub fn remove_extension ( & mut self , name : & ExtensionId ) -> Option < Extension > {
166
+ pub fn remove_extension ( & mut self , name : & ExtensionId ) -> Option < Arc < Extension > > {
154
167
self . 0 . remove ( name)
155
168
}
156
169
}
157
170
158
171
impl IntoIterator for ExtensionRegistry {
159
- type Item = ( ExtensionId , Extension ) ;
172
+ type Item = ( ExtensionId , Arc < Extension > ) ;
160
173
161
- type IntoIter = <BTreeMap < ExtensionId , Extension > as IntoIterator >:: IntoIter ;
174
+ type IntoIter = <BTreeMap < ExtensionId , Arc < Extension > > as IntoIterator >:: IntoIter ;
162
175
163
176
fn into_iter ( self ) -> Self :: IntoIter {
164
177
self . 0 . into_iter ( )
@@ -646,10 +659,10 @@ pub mod test {
646
659
647
660
let ext_1_id = ExtensionId :: new ( "ext1" ) . unwrap ( ) ;
648
661
let ext_2_id = ExtensionId :: new ( "ext2" ) . unwrap ( ) ;
649
- let ext1 = Extension :: new ( ext_1_id. clone ( ) , Version :: new ( 1 , 0 , 0 ) ) ;
650
- let ext1_1 = Extension :: new ( ext_1_id. clone ( ) , Version :: new ( 1 , 1 , 0 ) ) ;
651
- let ext1_2 = Extension :: new ( ext_1_id. clone ( ) , Version :: new ( 0 , 2 , 0 ) ) ;
652
- let ext2 = Extension :: new ( ext_2_id, Version :: new ( 1 , 0 , 0 ) ) ;
662
+ let ext1 = Arc :: new ( Extension :: new ( ext_1_id. clone ( ) , Version :: new ( 1 , 0 , 0 ) ) ) ;
663
+ let ext1_1 = Arc :: new ( Extension :: new ( ext_1_id. clone ( ) , Version :: new ( 1 , 1 , 0 ) ) ) ;
664
+ let ext1_2 = Arc :: new ( Extension :: new ( ext_1_id. clone ( ) , Version :: new ( 0 , 2 , 0 ) ) ) ;
665
+ let ext2 = Arc :: new ( Extension :: new ( ext_2_id, Version :: new ( 1 , 0 , 0 ) ) ) ;
653
666
654
667
reg. register ( ext1. clone ( ) ) . unwrap ( ) ;
655
668
reg_ref. register ( ext1. clone ( ) ) . unwrap ( ) ;
0 commit comments