11//! Plugin manager with dependency resolution.
22
33use derivative:: Derivative ;
4- use once_cell:: sync:: OnceCell ;
54use std:: collections:: { HashMap , HashSet } ;
65use std:: fmt:: Debug ;
76use std:: hash:: Hash ;
8- use std:: slice:: Iter ;
97
108///
119/// Ruler allows you to implement a plugin system with dependency management and ensure that
@@ -35,7 +33,7 @@ use std::slice::Iter;
3533///
3634/// // now we run this chain
3735/// let mut result = String::new();
38- /// for f in chain.iter () { f(&mut result); }
36+ /// for f in chain.compile () { f(&mut result); }
3937/// assert_eq!(result, "[ hello, world! ]");
4038/// ```
4139///
@@ -50,7 +48,6 @@ use std::slice::Iter;
5048///
5149pub struct Ruler < M , T > {
5250 deps : Vec < RuleItem < M , T > > ,
53- compiled : OnceCell < ( Vec < usize > , Vec < T > ) > ,
5451}
5552
5653impl < M , T > Ruler < M , T > {
@@ -62,7 +59,6 @@ impl<M, T> Ruler<M, T> {
6259impl < M : Eq + Hash + Copy + Debug , T : Clone > Ruler < M , T > {
6360 /// Add a new rule identified by `mark` with payload `value`.
6461 pub fn add ( & mut self , mark : M , value : T ) -> & mut RuleItem < M , T > {
65- self . compiled = OnceCell :: new ( ) ;
6662 let dep = RuleItem :: new ( mark, value) ;
6763 self . deps . push ( dep) ;
6864 self . deps . last_mut ( ) . unwrap ( )
@@ -74,17 +70,12 @@ impl<M: Eq + Hash + Copy + Debug, T: Clone> Ruler<M, T> {
7470 }
7571
7672 /// Check if there are any rules identified by `mark`.
77- pub fn contains ( & mut self , mark : M ) -> bool {
73+ pub fn contains ( & self , mark : M ) -> bool {
7874 self . deps . iter ( ) . any ( |dep| dep. marks . contains ( & mark) )
7975 }
8076
81- /// Ordered iteration through rules.
82- #[ inline]
83- pub fn iter ( & self ) -> Iter < T > {
84- self . compiled . get_or_init ( || self . compile ( ) ) . 1 . iter ( )
85- }
86-
87- fn compile ( & self ) -> ( Vec < usize > , Vec < T > ) {
77+ /// Convert dependency tree into an ordered list.
78+ pub fn compile ( & self ) -> Vec < T > {
8879 // ID -> [RuleItem index]
8980 let mut idhash = HashMap :: < M , Vec < usize > > :: new ( ) ;
9081
@@ -206,20 +197,15 @@ impl<M: Eq + Hash + Copy + Debug, T: Clone> Ruler<M, T> {
206197 panic ! ( "cyclic dependency: (use debug mode for more details)" ) ;
207198 }
208199
209- ( result_idx, result)
200+ //(result_idx, result)
201+ result
210202 }
211203}
212204
213205impl < M : Eq + Hash + Copy + Debug , T : Clone > Debug for Ruler < M , T > {
214206 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
215- let vec: Vec < ( usize , M ) > = self . compiled . get_or_init ( || self . compile ( ) ) . 0
216- . iter ( )
217- . map ( |idx| ( * idx, * self . deps . get ( * idx) . unwrap ( ) . marks . get ( 0 ) . unwrap ( ) ) )
218- . collect ( ) ;
219-
220207 f. debug_struct ( "Ruler" )
221208 . field ( "deps" , & self . deps )
222- . field ( "compiled" , & vec)
223209 . finish ( )
224210 }
225211}
@@ -228,7 +214,6 @@ impl<M, T> Default for Ruler<M, T> {
228214 fn default ( ) -> Self {
229215 Self {
230216 deps : Vec :: new ( ) ,
231- compiled : OnceCell :: new ( ) ,
232217 }
233218 }
234219}
@@ -267,7 +252,7 @@ impl<M: Copy, T> RuleItem<M, T> {
267252 /// chain.add("b", |s| s.push_str("foo")).before("a");
268253 ///
269254 /// let mut result = String::new();
270- /// for f in chain.iter () { f(&mut result); }
255+ /// for f in chain.compile () { f(&mut result); }
271256 /// assert_eq!(result, "foobar");
272257 /// ```
273258 pub fn before ( & mut self , mark : M ) -> & mut Self {
@@ -293,7 +278,7 @@ impl<M: Copy, T> RuleItem<M, T> {
293278 /// chain.add("b", |s| s.push_str("B")).after("a").before_all();
294279 ///
295280 /// let mut result = String::new();
296- /// for f in chain.iter () { f(&mut result); }
281+ /// for f in chain.compile () { f(&mut result); }
297282 /// // without before_all order will be ACB
298283 /// assert_eq!(result, "ABC");
299284 /// ```
@@ -321,7 +306,7 @@ impl<M: Copy, T> RuleItem<M, T> {
321306 /// chain.add("a", |s| s.push_str("A")).before("BorC");
322307 ///
323308 /// let mut result = String::new();
324- /// for f in chain.iter () { f(&mut result); }
309+ /// for f in chain.compile () { f(&mut result); }
325310 /// assert_eq!(result, "ABC");
326311 /// ```
327312 pub fn alias ( & mut self , mark : M ) -> & mut Self {
0 commit comments