pub trait VectorLike< E >
{
fn push( &mut self, e : E );
}
impl< E > VectorLike< E > for std::vec::Vec< E >
{
fn push( &mut self, e : E )
{
std::vec::Vec::push( self, e );
}
}
#[derive( Debug, Default )]
pub struct VectorFormer< E, Vector, Former, ContainerEnd >
where
Vector : VectorLike< E > + core::fmt::Debug + core::cmp::PartialEq + std::default::Default,
ContainerEnd : Fn( &mut Former, core::option::Option< Vector > ),
{
container : Option< Vector >,
former : Former,
on_end : ContainerEnd,
_phantom : core::marker::PhantomData< E >,
}
impl< E, Vector, Former, ContainerEnd > VectorFormer< E, Vector, Former, ContainerEnd >
where
Vector : VectorLike< E > + core::fmt::Debug + core::cmp::PartialEq + std::default::Default,
ContainerEnd : Fn( &mut Former, core::option::Option< Vector > ),
{
pub fn new( former : Former, container : core::option::Option< Vector >, on_end : ContainerEnd ) -> Self
{
Self
{
former,
container,
on_end,
_phantom : core::marker::PhantomData,
}
}
pub fn replace( mut self, vector : Vector ) -> Self
{
debug_assert!( self.container.is_none() );
self.container = Some( vector );
self
}
pub fn end( mut self ) -> Former
{
let container = self.container.take();
( self.on_end )( &mut self.former, container );
self.former
}
pub fn push< E2 >( mut self, e : E2 ) -> Self
where E2 : core::convert::Into< E >,
{
if self.container.is_none()
{
self.container = core::option::Option::Some( Default::default() );
}
if let core::option::Option::Some( ref mut container ) = self.container
{
container.push( e.into() );
}
self
}
}