@@ -840,7 +840,7 @@ where
840840pub struct SigBufFn < F , T >
841841where
842842 F : FnMut ( & SigCtx , & mut Vec < T > ) ,
843- T : Clone + Default ,
843+ T : Clone ,
844844{
845845 f : F ,
846846 buf : Vec < T > ,
@@ -849,12 +849,11 @@ where
849849impl < F , T > SigT for SigBufFn < F , T >
850850where
851851 F : FnMut ( & SigCtx , & mut Vec < T > ) ,
852- T : Clone + Default ,
852+ T : Clone ,
853853{
854854 type Item = T ;
855855
856856 fn sample ( & mut self , ctx : & SigCtx ) -> impl Buf < Self :: Item > {
857- self . buf . resize_with ( ctx. num_samples , Default :: default) ;
858857 ( self . f ) ( ctx, & mut self . buf ) ;
859858 & self . buf
860859 }
@@ -863,9 +862,46 @@ where
863862impl < F , T > Sig < SigBufFn < F , T > >
864863where
865864 F : FnMut ( & SigCtx , & mut Vec < T > ) ,
866- T : Clone + Default ,
865+ T : Clone ,
867866{
868867 pub fn from_buf_fn ( f : F ) -> Self {
869868 Self ( SigBufFn { f, buf : Vec :: new ( ) } )
870869 }
871870}
871+
872+ #[ derive( Default ) ]
873+ pub struct SigVar < T > ( Arc < RwLock < T > > ) ;
874+
875+ impl < T > SigVar < T > {
876+ pub fn new ( value : T ) -> Self {
877+ Self ( Arc :: new ( RwLock :: new ( value) ) )
878+ }
879+
880+ pub fn set ( & self , value : T ) {
881+ * self . 0 . write ( ) . unwrap ( ) = value;
882+ }
883+ }
884+
885+ pub fn sig_var < T : Clone > ( value : T ) -> Sig < SigVar < T > > {
886+ Sig ( SigVar :: new ( value) )
887+ }
888+
889+ impl < T > Clone for SigVar < T > {
890+ fn clone ( & self ) -> Self {
891+ Self ( Arc :: clone ( & self . 0 ) )
892+ }
893+ }
894+
895+ impl < T > SigT for SigVar < T >
896+ where
897+ T : Clone ,
898+ {
899+ type Item = T ;
900+
901+ fn sample ( & mut self , ctx : & SigCtx ) -> impl Buf < Self :: Item > {
902+ ConstBuf {
903+ count : ctx. num_samples ,
904+ value : self . 0 . read ( ) . unwrap ( ) . clone ( ) ,
905+ }
906+ }
907+ }
0 commit comments