1
2pub trait VectorLike< E >
7{
8 fn push( &mut self, e : E );
10}
11
12impl< E > VectorLike< E > for std::vec::Vec< E >
13{
14 fn push( &mut self, e : E )
15 {
16 std::vec::Vec::push( self, e );
17 }
18}
19
20#[derive( Debug, Default )]
25pub struct VectorFormer< E, Vector, Former, ContainerEnd >
26where
27 Vector : VectorLike< E > + std::fmt::Debug + std::cmp::PartialEq + std::default::Default,
28 ContainerEnd : Fn( &mut Former, core::option::Option< Vector > ),
29{
30 container : Option< Vector >,
31 former : Former,
32 on_end : ContainerEnd,
33 _phantom : core::marker::PhantomData< E >,
34}
35
36impl< E, Vector, Former, ContainerEnd > VectorFormer< E, Vector, Former, ContainerEnd >
37where
38 Vector : VectorLike< E > + std::fmt::Debug + std::cmp::PartialEq + std::default::Default,
39 ContainerEnd : Fn( &mut Former, core::option::Option< Vector > ),
40{
41
42 pub fn new( former : Former, container : core::option::Option< Vector >, on_end : ContainerEnd ) -> Self
44 {
45 Self
46 {
47 former,
48 container,
49 on_end,
50 _phantom : core::marker::PhantomData,
51 }
52 }
53
54 pub fn replace( mut self, vector : Vector ) -> Self
56 {
57 debug_assert!( self.container.is_none() );
58 self.container = Some( vector );
59 self
60 }
61
62 pub fn end( mut self ) -> Former
64 {
65 let container = self.container.take();
66 ( self.on_end )( &mut self.former, container );
67 self.former
68 }
69
70 pub fn push< E2 >( mut self, e : E2 ) -> Self
72 where E2 : core::convert::Into< E >,
73 {
74 if self.container.is_none()
75 {
76 self.container = core::option::Option::Some( Default::default() );
77 }
78 if let core::option::Option::Some( ref mut container ) = self.container
79 {
80 container.push( e.into() );
81 }
82 self
83 }
84
85}
86
87