Skip to content

Commit f482d45

Browse files
committed
Drop unnecessary Oks and avoid allocating msgs
1 parent 578d7aa commit f482d45

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

src/io/fs_store.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,13 @@ impl KVStore for FilesystemStore {
7575
dest_file_path.push(namespace);
7676
dest_file_path.push(key);
7777

78-
let msg = format!("Could not retrieve parent directory of {}.", dest_file_path.display());
7978
let parent_directory = dest_file_path
8079
.parent()
81-
.ok_or(std::io::Error::new(std::io::ErrorKind::InvalidInput, msg))?
80+
.ok_or_else(|| {
81+
let msg =
82+
format!("Could not retrieve parent directory of {}.", dest_file_path.display());
83+
std::io::Error::new(std::io::ErrorKind::InvalidInput, msg)
84+
})?
8285
.to_path_buf();
8386
fs::create_dir_all(parent_directory.clone())?;
8487

@@ -151,11 +154,11 @@ impl KVStore for FilesystemStore {
151154
fs::remove_file(&dest_file_path)?;
152155
#[cfg(not(target_os = "windows"))]
153156
{
154-
let msg =
155-
format!("Could not retrieve parent directory of {}.", dest_file_path.display());
156-
let parent_directory = dest_file_path
157-
.parent()
158-
.ok_or(std::io::Error::new(std::io::ErrorKind::InvalidInput, msg))?;
157+
let parent_directory = dest_file_path.parent().ok_or_else(|| {
158+
let msg =
159+
format!("Could not retrieve parent directory of {}.", dest_file_path.display());
160+
std::io::Error::new(std::io::ErrorKind::InvalidInput, msg)
161+
})?;
159162
let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?;
160163
unsafe {
161164
// The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes
@@ -245,20 +248,21 @@ impl Read for FilesystemReader {
245248

246249
impl KVStorePersister for FilesystemStore {
247250
fn persist<W: Writeable>(&self, prefixed_key: &str, object: &W) -> lightning::io::Result<()> {
248-
let msg = format!("Could not persist file for key {}.", prefixed_key);
249251
let dest_file_path = PathBuf::from_str(prefixed_key).map_err(|_| {
250-
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg.clone())
252+
let msg = format!("Could not persist file for key {}.", prefixed_key);
253+
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
251254
})?;
252255

253-
let parent_directory = dest_file_path.parent().ok_or(lightning::io::Error::new(
254-
lightning::io::ErrorKind::InvalidInput,
255-
msg.clone(),
256-
))?;
256+
let parent_directory = dest_file_path.parent().ok_or_else(|| {
257+
let msg = format!("Could not persist file for key {}.", prefixed_key);
258+
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
259+
})?;
257260
let namespace = parent_directory.display().to_string();
258261

259-
let dest_without_namespace = dest_file_path
260-
.strip_prefix(&namespace)
261-
.map_err(|_| lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg))?;
262+
let dest_without_namespace = dest_file_path.strip_prefix(&namespace).map_err(|_| {
263+
let msg = format!("Could not persist file for key {}.", prefixed_key);
264+
lightning::io::Error::new(lightning::io::ErrorKind::InvalidInput, msg)
265+
})?;
262266
let key = dest_without_namespace.display().to_string();
263267

264268
self.write(&namespace, &key, &object.encode())?;

src/io/utils.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,9 @@ where
100100
{
101101
let mut reader =
102102
kv_store.read(NETWORK_GRAPH_PERSISTENCE_NAMESPACE, NETWORK_GRAPH_PERSISTENCE_KEY)?;
103-
let graph = NetworkGraph::read(&mut reader, logger).map_err(|_| {
103+
NetworkGraph::read(&mut reader, logger).map_err(|_| {
104104
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to deserialize NetworkGraph")
105-
})?;
106-
Ok(graph)
105+
})
107106
}
108107

109108
/// Read a previously persisted [`Scorer`] from the store.
@@ -116,10 +115,9 @@ where
116115
let params = ProbabilisticScoringParameters::default();
117116
let mut reader = kv_store.read(SCORER_PERSISTENCE_NAMESPACE, SCORER_PERSISTENCE_KEY)?;
118117
let args = (params, network_graph, logger);
119-
let scorer = ProbabilisticScorer::read(&mut reader, args).map_err(|_| {
118+
ProbabilisticScorer::read(&mut reader, args).map_err(|_| {
120119
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to deserialize Scorer")
121-
})?;
122-
Ok(scorer)
120+
})
123121
}
124122

125123
/// Read previously persisted events from the store.
@@ -131,10 +129,9 @@ where
131129
{
132130
let mut reader =
133131
kv_store.read(EVENT_QUEUE_PERSISTENCE_NAMESPACE, EVENT_QUEUE_PERSISTENCE_KEY)?;
134-
let event_queue = EventQueue::read(&mut reader, (kv_store, logger)).map_err(|_| {
132+
EventQueue::read(&mut reader, (kv_store, logger)).map_err(|_| {
135133
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to deserialize EventQueue")
136-
})?;
137-
Ok(event_queue)
134+
})
138135
}
139136

140137
/// Read previously persisted peer info from the store.
@@ -145,10 +142,9 @@ where
145142
L::Target: Logger,
146143
{
147144
let mut reader = kv_store.read(PEER_INFO_PERSISTENCE_NAMESPACE, PEER_INFO_PERSISTENCE_KEY)?;
148-
let peer_info = PeerStore::read(&mut reader, (kv_store, logger)).map_err(|_| {
145+
PeerStore::read(&mut reader, (kv_store, logger)).map_err(|_| {
149146
std::io::Error::new(std::io::ErrorKind::InvalidData, "Failed to deserialize PeerStore")
150-
})?;
151-
Ok(peer_info)
147+
})
152148
}
153149

154150
/// Read previously persisted payments information from the store.
@@ -200,8 +196,7 @@ where
200196
e
201197
);
202198
Error::PersistenceFailed
203-
})?;
204-
Ok(())
199+
})
205200
}
206201

207202
pub(crate) fn read_latest_node_ann_bcast_timestamp<K: KVStore + Sync + Send>(
@@ -239,6 +234,5 @@ where
239234
e
240235
);
241236
Error::PersistenceFailed
242-
})?;
243-
Ok(())
237+
})
244238
}

0 commit comments

Comments
 (0)