1- use super :: DataTree ;
21use crate :: size:: Size ;
3- use rayon:: prelude:: * ;
42use std:: {
53 collections:: VecDeque ,
64 ffi:: OsStr ,
75 fmt:: { Debug , Display , Error , Formatter } ,
8- iter:: once,
96 path:: PathBuf ,
107} ;
118
129#[ cfg( feature = "json" ) ]
1310use serde:: { Deserialize , Serialize } ;
1411
15- /// Intermediate format used for construction and inspection of [`DataTree`]'s internal content.
12+ /// Intermediate format used for construction and inspection of
13+ /// [`DataTree`](crate::data_tree::DataTree)'s internal content.
1614///
1715/// Unlike `Tree` where the fields are all private, the fields of `TreeReflection`
1816/// are all public to allow construction in tests.
@@ -21,7 +19,8 @@ use serde::{Deserialize, Serialize};
2119/// * Any `DataTree` can be safely [transmuted](std::mem::transmute) to a valid `Reflection`.
2220/// * Any `Reflection` can be safely transmuted to a potentially invalid `DataTree`.
2321/// * To safely convert a `DataTree` into a `Reflection` without the `unsafe` keyword, use
24- /// [`DataTree::into_reflection`] (it would be slower than using `transmute`).
22+ /// [`DataTree::into_reflection`](crate::data_tree::DataTree::into_reflection)
23+ /// (it would be slower than using `transmute`).
2524/// * To safely convert a `Reflection` into a valid `DataTree`,
2625/// use [`par_try_into_tree`](Self::par_try_into_tree).
2726///
@@ -37,30 +36,8 @@ pub struct Reflection<Name, Data: Size> {
3736 pub children : Vec < Self > ,
3837}
3938
40- impl < Name , Data : Size > From < DataTree < Name , Data > > for Reflection < Name , Data > {
41- fn from ( source : DataTree < Name , Data > ) -> Self {
42- let DataTree {
43- name,
44- data,
45- children,
46- } = source;
47- let children: Vec < _ > = children. into_iter ( ) . map ( Reflection :: from) . collect ( ) ;
48- Reflection {
49- name,
50- data,
51- children,
52- }
53- }
54- }
55-
56- impl < Name , Data : Size > DataTree < Name , Data > {
57- /// Create reflection.
58- pub fn into_reflection ( self ) -> Reflection < Name , Data > {
59- self . into ( )
60- }
61- }
62-
63- /// Error that occurs when an attempt to convert a [`Reflection`] into a [`DataTree`] fails.
39+ /// Error that occurs when an attempt to convert a [`Reflection`] into a
40+ /// [`DataTree`](crate::data_tree::DataTree) fails.
6441#[ derive( Debug , Clone , PartialEq , Eq ) ]
6542#[ non_exhaustive]
6643pub enum ConversionError < Name , Data : Size > {
@@ -107,100 +84,5 @@ where
10784 }
10885}
10986
110- impl < Name , Data > Reflection < Name , Data >
111- where
112- Name : Send ,
113- Data : Size + Send ,
114- {
115- /// Attempting to convert a [`Reflection`] into a valid [`DataTree`].
116- pub fn par_try_into_tree ( self ) -> Result < DataTree < Name , Data > , ConversionError < Name , Data > > {
117- let Reflection {
118- name,
119- data,
120- children,
121- } = self ;
122- let children_sum = children. iter ( ) . map ( |child| child. data ) . sum ( ) ;
123- if data < children_sum {
124- return Err ( ConversionError :: ExcessiveChildren {
125- path : once ( name) . collect ( ) ,
126- data,
127- children,
128- children_sum,
129- } ) ;
130- }
131- let children: Result < Vec < _ > , _ > = children
132- . into_par_iter ( )
133- . map ( Self :: par_try_into_tree)
134- . collect ( ) ;
135- let children = match children {
136- Ok ( children) => children,
137- Err ( ConversionError :: ExcessiveChildren {
138- mut path,
139- data,
140- children,
141- children_sum,
142- } ) => {
143- path. push_front ( name) ;
144- return Err ( ConversionError :: ExcessiveChildren {
145- path,
146- data,
147- children,
148- children_sum,
149- } ) ;
150- }
151- } ;
152- Ok ( DataTree {
153- name,
154- data,
155- children,
156- } )
157- }
158- }
159-
160- impl < Name , Data > Reflection < Name , Data >
161- where
162- Name : Send ,
163- Data : Size + Send ,
164- {
165- /// Attempt to transform names and data.
166- pub fn par_try_map < TargetName , TargetData , Error , Transform > (
167- self ,
168- transform : Transform ,
169- ) -> Result < Reflection < TargetName , TargetData > , Error >
170- where
171- TargetName : Send ,
172- TargetData : Size + Send + Sync ,
173- Error : Send ,
174- Transform : Fn ( Name , Data ) -> Result < ( TargetName , TargetData ) , Error > + Copy + Sync ,
175- {
176- let Reflection {
177- name,
178- data,
179- children,
180- } = self ;
181- let children = children
182- . into_par_iter ( )
183- . map ( |child| child. par_try_map ( transform) )
184- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
185- let ( name, data) = transform ( name, data) ?;
186- Ok ( Reflection {
187- name,
188- data,
189- children,
190- } )
191- }
192-
193- /// Attempt to convert all names from `OsString` to `String`.
194- pub fn par_convert_names_to_utf8 ( self ) -> Result < Reflection < String , Data > , Name >
195- where
196- Name : AsRef < OsStr > ,
197- Data : Sync ,
198- {
199- self . par_try_map ( |name, data| {
200- name. as_ref ( )
201- . to_str ( )
202- . map ( |name| ( name. to_string ( ) , data) )
203- . ok_or ( name)
204- } )
205- }
206- }
87+ mod convert;
88+ mod par_methods;
0 commit comments