former_runtime/
vector.rs

1
2///
3/// Trait VectorLike adopter for Vector-like containers.
4///
5
6pub trait VectorLike< E >
7{
8  /// Appends an element to the back of a container.
9  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///
21/// Class for forming vector-like fields.
22///
23
24#[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  /// Make a new VectorFormer. It should be called by a former generated for your structure.
43  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  /// Set the whole container instead of setting each element individually.
55  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  /// Return former of your struct moving container there. Should be called after configuring the container.
63  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  /// Appends an element to the back of a container. Make a new container if it was not made so far.
71  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// pub type VectorFormerStdVec< Former, E > =
88//   VectorFormer< E, std::vec::Vec< E >, Former, impl Fn( &mut Former, core::option::Option< std::vec::Vec< E > > ) >;