Skip to content

Commit 68e223f

Browse files
committed
f Use associated type rather than dyn
1 parent 2125b60 commit 68e223f

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

src/event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use crate::{
33
PaymentInfo, PaymentInfoStorage, PaymentStatus, Wallet,
44
};
55

6-
use crate::io::{KVStore, EVENT_QUEUE_PERSISTENCE_KEY, EVENT_QUEUE_PERSISTENCE_NAMESPACE};
6+
use crate::io::{
7+
KVStore, TransactionalWrite, EVENT_QUEUE_PERSISTENCE_KEY, EVENT_QUEUE_PERSISTENCE_NAMESPACE,
8+
};
79
use crate::logger::{log_error, log_info, Logger};
810

911
use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};

src/io/fs_store.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,21 @@ impl FilesystemStore {
4747
}
4848

4949
impl KVStore for FilesystemStore {
50-
fn read(&self, namespace: &str, key: &str) -> std::io::Result<Box<dyn Read>> {
50+
type Reader = FilesystemReader;
51+
type Writer = FilesystemWriter;
52+
53+
fn read(&self, namespace: &str, key: &str) -> std::io::Result<Self::Reader> {
5154
let mut dest_file = self.dest_dir.clone();
5255
dest_file.push(namespace);
5356
dest_file.push(key);
54-
FilesystemReader::new(dest_file).map(|r| Box::new(r) as Box<dyn Read>)
57+
FilesystemReader::new(dest_file)
5558
}
5659

57-
fn write(&self, namespace: &str, key: &str) -> std::io::Result<Box<dyn TransactionalWrite>> {
60+
fn write(&self, namespace: &str, key: &str) -> std::io::Result<Self::Writer> {
5861
let mut dest_file = self.dest_dir.clone();
5962
dest_file.push(namespace);
6063
dest_file.push(key);
61-
FilesystemWriter::new(dest_file).map(|w| Box::new(w) as Box<dyn TransactionalWrite>)
64+
FilesystemWriter::new(dest_file)
6265
}
6366

6467
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<bool> {

src/io/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ pub(crate) const PAYMENT_INFO_PERSISTENCE_NAMESPACE: &str = "payments";
3636
/// Keys and namespaces are required to be valid ASCII strings and the empty namespace (`""`) is
3737
/// assumed to be valid namespace.
3838
pub trait KVStore {
39+
type Reader: Read;
40+
type Writer: TransactionalWrite;
3941
/// Returns a [`Read`] for the given `namespace` and `key` from which [`Readable`]s may be
4042
/// read.
4143
///
4244
/// Returns an `Err` if the given `key` could not be found in the given `namespace`.
4345
///
4446
/// [`Readable`]: lightning::util::ser::Readable
45-
fn read(&self, namespace: &str, key: &str) -> std::io::Result<Box<dyn Read>>;
47+
fn read(&self, namespace: &str, key: &str) -> std::io::Result<Self::Reader>;
4648
/// Returns a [`TransactionalWrite`] for the given `key` to which [`Writeable`]s may be written.
4749
///
4850
/// Will create the given `namespace` if not already present in the store.
@@ -51,7 +53,7 @@ pub trait KVStore {
5153
/// the changes won't be persisted.
5254
///
5355
/// [`Writeable`]: lightning::util::ser::Writeable
54-
fn write(&self, namespace: &str, key: &str) -> std::io::Result<Box<dyn TransactionalWrite>>;
56+
fn write(&self, namespace: &str, key: &str) -> std::io::Result<Self::Writer>;
5557
/// Removes any data that had previously been persisted under the given `key`.
5658
///
5759
/// Returns `true` if the `key` was present in the given `namespace`, and `false` otherwise.

src/payment_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::hex_utils;
2-
use crate::io::{KVStore, PAYMENT_INFO_PERSISTENCE_NAMESPACE};
2+
use crate::io::{KVStore, TransactionalWrite, PAYMENT_INFO_PERSISTENCE_NAMESPACE};
33
use crate::Error;
44

55
use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};

src/peer_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::hex_utils;
2-
use crate::io::{KVStore, PEER_INFO_PERSISTENCE_KEY, PEER_INFO_PERSISTENCE_NAMESPACE};
2+
use crate::io::{KVStore, TransactionalWrite, PEER_INFO_PERSISTENCE_KEY, PEER_INFO_PERSISTENCE_NAMESPACE};
33
use crate::Error;
44

55
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};

src/test/utils.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@ impl TestStore {
6666
}
6767

6868
impl KVStore for TestStore {
69-
fn read(&self, namespace: &str, key: &str) -> std::io::Result<Box<dyn Read>> {
69+
type Reader = TestReader;
70+
type Writer = TestWriter;
71+
72+
fn read(&self, namespace: &str, key: &str) -> std::io::Result<Self::Reader> {
7073
if let Some(outer_ref) = self.persisted_bytes.read().unwrap().get(namespace) {
7174
if let Some(inner_ref) = outer_ref.get(key) {
72-
Ok(Box::new(TestReader::new(Arc::clone(inner_ref))) as Box<dyn Read>)
75+
Ok(TestReader::new(Arc::clone(inner_ref)))
7376
} else {
7477
let msg = format!("Key not found: {}", key);
7578
Err(std::io::Error::new(std::io::ErrorKind::NotFound, msg))
@@ -80,11 +83,11 @@ impl KVStore for TestStore {
8083
}
8184
}
8285

83-
fn write(&self, namespace: &str, key: &str) -> std::io::Result<Box<dyn TransactionalWrite>> {
86+
fn write(&self, namespace: &str, key: &str) -> std::io::Result<Self::Writer> {
8487
let mut guard = self.persisted_bytes.write().unwrap();
8588
let outer_e = guard.entry(namespace.to_string()).or_insert(HashMap::new());
8689
let inner_e = outer_e.entry(key.to_string()).or_insert(Arc::new(RwLock::new(Vec::new())));
87-
Ok(Box::new(TestWriter::new(Arc::clone(&inner_e), Arc::clone(&self.did_persist))))
90+
Ok(TestWriter::new(Arc::clone(&inner_e), Arc::clone(&self.did_persist)))
8891
}
8992

9093
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<bool> {

0 commit comments

Comments
 (0)