@@ -38,11 +38,11 @@ pub mod declarative;
3838
3939/// Extension Registries store extensions to be looked up e.g. during validation.
4040#[ derive( Clone , Debug , PartialEq ) ]
41- pub struct ExtensionRegistry ( BTreeMap < ExtensionId , Extension > ) ;
41+ pub struct ExtensionRegistry ( BTreeMap < ExtensionId , Arc < Extension > > ) ;
4242
4343impl ExtensionRegistry {
4444 /// 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 > > {
4646 self . 0 . get ( name)
4747 }
4848
@@ -51,9 +51,9 @@ impl ExtensionRegistry {
5151 self . 0 . contains_key ( name)
5252 }
5353
54- /// Makes a new ExtensionRegistry, validating all the extensions in it
54+ /// Makes a new [ ExtensionRegistry] , validating all the extensions in it.
5555 pub fn try_new (
56- value : impl IntoIterator < Item = Extension > ,
56+ value : impl IntoIterator < Item = Arc < Extension > > ,
5757 ) -> Result < Self , ExtensionRegistryError > {
5858 let mut res = ExtensionRegistry ( BTreeMap :: new ( ) ) ;
5959
@@ -70,20 +70,28 @@ impl ExtensionRegistry {
7070 ext. validate ( & res)
7171 . map_err ( |e| ExtensionRegistryError :: InvalidSignature ( ext. name ( ) . clone ( ) , e) ) ?;
7272 }
73+
7374 Ok ( res)
7475 }
7576
7677 /// Registers a new extension to the registry.
7778 ///
7879 /// 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 ( ) ;
8085 match self . 0 . entry ( extension. name ( ) . clone ( ) ) {
8186 btree_map:: Entry :: Occupied ( prev) => Err ( ExtensionRegistryError :: AlreadyRegistered (
8287 extension. name ( ) . clone ( ) ,
8388 prev. get ( ) . version ( ) . clone ( ) ,
8489 extension. version ( ) . clone ( ) ,
8590 ) ) ,
86- btree_map:: Entry :: Vacant ( ve) => Ok ( ve. insert ( extension) ) ,
91+ btree_map:: Entry :: Vacant ( ve) => {
92+ ve. insert ( extension) ;
93+ Ok ( ( ) )
94+ }
8795 }
8896 }
8997
@@ -93,21 +101,24 @@ impl ExtensionRegistry {
93101 /// If versions match, the original extension is kept.
94102 /// Returns a reference to the registered extension if successful.
95103 ///
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
97105 /// [`ExtensionRegistry::register_updated_ref`].
98106 pub fn register_updated (
99107 & mut self ,
100- extension : Extension ,
101- ) -> Result < & Extension , ExtensionRegistryError > {
108+ extension : impl Into < Arc < Extension > > ,
109+ ) -> Result < ( ) , ExtensionRegistryError > {
110+ let extension = extension. into ( ) ;
102111 match self . 0 . entry ( extension. name ( ) . clone ( ) ) {
103112 btree_map:: Entry :: Occupied ( mut prev) => {
104113 if prev. get ( ) . version ( ) < extension. version ( ) {
105114 * prev. get_mut ( ) = extension;
106115 }
107- Ok ( prev. into_mut ( ) )
108116 }
109- btree_map:: Entry :: Vacant ( ve) => Ok ( ve. insert ( extension) ) ,
117+ btree_map:: Entry :: Vacant ( ve) => {
118+ ve. insert ( extension) ;
119+ }
110120 }
121+ Ok ( ( ) )
111122 }
112123
113124 /// Registers a new extension to the registry, keeping most up to date if
@@ -117,21 +128,23 @@ impl ExtensionRegistry {
117128 /// If versions match, the original extension is kept. Returns a reference
118129 /// to the registered extension if successful.
119130 ///
120- /// Clones the extension if required. For no-cloning version see
131+ /// Clones the Arc only when required. For no-cloning version see
121132 /// [`ExtensionRegistry::register_updated`].
122133 pub fn register_updated_ref (
123134 & mut self ,
124- extension : & Extension ,
125- ) -> Result < & Extension , ExtensionRegistryError > {
135+ extension : & Arc < Extension > ,
136+ ) -> Result < ( ) , ExtensionRegistryError > {
126137 match self . 0 . entry ( extension. name ( ) . clone ( ) ) {
127138 btree_map:: Entry :: Occupied ( mut prev) => {
128139 if prev. get ( ) . version ( ) < extension. version ( ) {
129140 * prev. get_mut ( ) = extension. clone ( ) ;
130141 }
131- Ok ( prev. into_mut ( ) )
132142 }
133- btree_map:: Entry :: Vacant ( ve) => Ok ( ve. insert ( extension. clone ( ) ) ) ,
143+ btree_map:: Entry :: Vacant ( ve) => {
144+ ve. insert ( extension. clone ( ) ) ;
145+ }
134146 }
147+ Ok ( ( ) )
135148 }
136149
137150 /// Returns the number of extensions in the registry.
@@ -145,20 +158,20 @@ impl ExtensionRegistry {
145158 }
146159
147160 /// 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 > ) > {
149162 self . 0 . iter ( )
150163 }
151164
152165 /// 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 > > {
154167 self . 0 . remove ( name)
155168 }
156169}
157170
158171impl IntoIterator for ExtensionRegistry {
159- type Item = ( ExtensionId , Extension ) ;
172+ type Item = ( ExtensionId , Arc < Extension > ) ;
160173
161- type IntoIter = <BTreeMap < ExtensionId , Extension > as IntoIterator >:: IntoIter ;
174+ type IntoIter = <BTreeMap < ExtensionId , Arc < Extension > > as IntoIterator >:: IntoIter ;
162175
163176 fn into_iter ( self ) -> Self :: IntoIter {
164177 self . 0 . into_iter ( )
@@ -646,10 +659,10 @@ pub mod test {
646659
647660 let ext_1_id = ExtensionId :: new ( "ext1" ) . unwrap ( ) ;
648661 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 ) ) ) ;
653666
654667 reg. register ( ext1. clone ( ) ) . unwrap ( ) ;
655668 reg_ref. register ( ext1. clone ( ) ) . unwrap ( ) ;
0 commit comments