@@ -57,7 +57,7 @@ use crate::util::dyn_signer::{
57
57
use crate :: util:: logger:: { Logger , Record } ;
58
58
#[ cfg( feature = "std" ) ]
59
59
use crate :: util:: mut_global:: MutGlobal ;
60
- use crate :: util:: persist:: { KVStoreSync , MonitorName } ;
60
+ use crate :: util:: persist:: { KVStore , KVStoreSync , MonitorName } ;
61
61
use crate :: util:: ser:: { Readable , ReadableArgs , Writeable , Writer } ;
62
62
use crate :: util:: test_channel_signer:: { EnforcementState , TestChannelSigner } ;
63
63
@@ -84,7 +84,10 @@ use crate::io;
84
84
use crate :: prelude:: * ;
85
85
use crate :: sign:: { EntropySource , NodeSigner , RandomBytes , Recipient , SignerProvider } ;
86
86
use crate :: sync:: { Arc , Mutex } ;
87
+ use alloc:: boxed:: Box ;
88
+ use core:: future:: Future ;
87
89
use core:: mem;
90
+ use core:: pin:: Pin ;
88
91
use core:: sync:: atomic:: { AtomicBool , AtomicUsize , Ordering } ;
89
92
use core:: time:: Duration ;
90
93
@@ -950,6 +953,33 @@ impl TestStore {
950
953
}
951
954
}
952
955
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
+
953
983
impl KVStoreSync for TestStore {
954
984
fn read (
955
985
& self , primary_namespace : & str , secondary_namespace : & str , key : & str ,
@@ -974,6 +1004,37 @@ impl KVStoreSync for TestStore {
974
1004
}
975
1005
}
976
1006
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
+
977
1038
unsafe impl Sync for TestStore { }
978
1039
unsafe impl Send for TestStore { }
979
1040
0 commit comments