@@ -57,7 +57,7 @@ use crate::util::dyn_signer::{
5757use crate :: util:: logger:: { Logger , Record } ;
5858#[ cfg( feature = "std" ) ]
5959use crate :: util:: mut_global:: MutGlobal ;
60- use crate :: util:: persist:: { KVStoreSync , MonitorName } ;
60+ use crate :: util:: persist:: { KVStore , KVStoreSync , MonitorName } ;
6161use crate :: util:: ser:: { Readable , ReadableArgs , Writeable , Writer } ;
6262use crate :: util:: test_channel_signer:: { EnforcementState , TestChannelSigner } ;
6363
@@ -84,7 +84,10 @@ use crate::io;
8484use crate :: prelude:: * ;
8585use crate :: sign:: { EntropySource , NodeSigner , RandomBytes , Recipient , SignerProvider } ;
8686use crate :: sync:: { Arc , Mutex } ;
87+ use alloc:: boxed:: Box ;
88+ use core:: future:: Future ;
8789use core:: mem;
90+ use core:: pin:: Pin ;
8891use core:: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
8992use core:: time:: Duration ;
9093
@@ -950,6 +953,33 @@ impl TestStore {
950953 }
951954}
952955
956+ impl KVStore for TestStore {
957+ fn read (
958+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
959+ ) -> Pin < Box < dyn Future < Output = Result < Vec < u8 > , io:: Error > > + ' static + Send > > {
960+ let res = self . read_internal ( & primary_namespace, & secondary_namespace, & key) ;
961+ Box :: pin ( async move { TestStoreFuture :: new ( res) . await } )
962+ }
963+ fn write (
964+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , buf : Vec < u8 > ,
965+ ) -> Pin < Box < dyn Future < Output = Result < ( ) , io:: Error > > + ' static + Send > > {
966+ let res = self . write_internal ( & primary_namespace, & secondary_namespace, & key, buf) ;
967+ Box :: pin ( async move { TestStoreFuture :: new ( res) . await } )
968+ }
969+ fn remove (
970+ & self , primary_namespace : & str , secondary_namespace : & str , key : & str , lazy : bool ,
971+ ) -> Pin < Box < dyn Future < Output = Result < ( ) , io:: Error > > + ' static + Send > > {
972+ let res = self . remove_internal ( & primary_namespace, & secondary_namespace, & key, lazy) ;
973+ Box :: pin ( async move { TestStoreFuture :: new ( res) . await } )
974+ }
975+ fn list (
976+ & self , primary_namespace : & str , secondary_namespace : & str ,
977+ ) -> Pin < Box < dyn Future < Output = Result < Vec < String > , io:: Error > > + ' static + Send > > {
978+ let res = self . list_internal ( primary_namespace, secondary_namespace) ;
979+ Box :: pin ( async move { TestStoreFuture :: new ( res) . await } )
980+ }
981+ }
982+
953983impl KVStoreSync for TestStore {
954984 fn read (
955985 & self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
@@ -974,6 +1004,37 @@ impl KVStoreSync for TestStore {
9741004 }
9751005}
9761006
1007+ // A `Future` that returns the result only on the second poll.
1008+ pub ( crate ) struct TestStoreFuture < R > {
1009+ res : Mutex < Option < io:: Result < R > > > ,
1010+ first_poll : AtomicBool ,
1011+ }
1012+
1013+ impl < R > TestStoreFuture < R > {
1014+ fn new ( res : io:: Result < R > ) -> Self {
1015+ let res = Mutex :: new ( Some ( res) ) ;
1016+ let first_poll = AtomicBool :: new ( true ) ;
1017+ Self { res, first_poll }
1018+ }
1019+ }
1020+
1021+ impl < R > Future for TestStoreFuture < R > {
1022+ type Output = Result < R , io:: Error > ;
1023+ fn poll (
1024+ self : Pin < & mut Self > , _cx : & mut core:: task:: Context < ' _ > ,
1025+ ) -> core:: task:: Poll < Self :: Output > {
1026+ if self . first_poll . swap ( false , Ordering :: Relaxed ) {
1027+ core:: task:: Poll :: Pending
1028+ } else {
1029+ if let Some ( res) = self . res . lock ( ) . unwrap ( ) . take ( ) {
1030+ core:: task:: Poll :: Ready ( res)
1031+ } else {
1032+ unreachable ! ( "We should never poll more than twice" ) ;
1033+ }
1034+ }
1035+ }
1036+ }
1037+
9771038unsafe impl Sync for TestStore { }
9781039unsafe impl Send for TestStore { }
9791040
0 commit comments