Skip to content

Commit bab0cf2

Browse files
committed
more borrowing, less owning
1 parent da00ef9 commit bab0cf2

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

lightning/src/util/persist.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -199,26 +199,26 @@ where
199199
Ok(res)
200200
}
201201

202-
enum KVStoreUpdatingPersisterError {
202+
enum KVStoreUpdatingPersisterError<'a> {
203203
/// The monitor name was improperly formatted.
204-
BadMonitorName { reason: String, context: String },
204+
BadMonitorName { reason: &'a str, context: &'a str },
205205
/// The monitor could not be decoded.
206206
MonitorDecodeFailed {
207207
reason: DecodeError,
208-
context: String,
208+
context: &'a str,
209209
},
210210
/// The update could not be decoded.
211211
UpdateDecodeFailed {
212212
reason: DecodeError,
213-
context: String,
213+
context: &'a str,
214214
},
215215
/// Storage could not be read.
216-
StorageReadFailed { reason: io::Error, context: String },
216+
StorageReadFailed { reason: io::Error, context: &'a str },
217217
/// An update could not be applied to a monitor.
218-
UpdateFailed { reason: String, context: String },
218+
UpdateFailed { reason: &'a str, context: &'a str },
219219
}
220220

221-
impl From<KVStoreUpdatingPersisterError> for io::Error {
221+
impl<'a> From<KVStoreUpdatingPersisterError<'a>> for io::Error {
222222
fn from(value: KVStoreUpdatingPersisterError) -> Self {
223223
match value {
224224
KVStoreUpdatingPersisterError::BadMonitorName { reason, context } => io::Error::new(
@@ -255,8 +255,8 @@ pub struct MonitorName(String);
255255

256256
impl MonitorName {
257257
/// The key to store this monitor with.
258-
fn storage_key(&self) -> String {
259-
self.0.clone()
258+
fn storage_key(&self) -> &str {
259+
&self.0
260260
}
261261
}
262262

@@ -269,25 +269,25 @@ impl TryFrom<MonitorName> for OutPoint {
269269
parts
270270
.next()
271271
.ok_or_else(|| KVStoreUpdatingPersisterError::BadMonitorName {
272-
reason: "no txid found, maybe there is no underscore".to_string(),
273-
context: value.0.clone(),
272+
reason: "no txid found, maybe there is no underscore",
273+
context: &value.0,
274274
})?;
275275
let index = parts
276276
.next()
277277
.ok_or_else(|| KVStoreUpdatingPersisterError::BadMonitorName {
278-
reason: "no index value found after underscore".to_string(),
279-
context: value.0.clone(),
278+
reason: "no index value found after underscore",
279+
context: &value.0,
280280
})?;
281281
let index = index
282282
.parse()
283-
.map_err(|e| KVStoreUpdatingPersisterError::BadMonitorName {
284-
reason: format!("bad index value, caused by {}", e),
285-
context: value.0.clone(),
283+
.map_err(|_| KVStoreUpdatingPersisterError::BadMonitorName {
284+
reason: "could not parse index value in monitor name",
285+
context: &value.0,
286286
})?;
287-
let txid = Txid::from_hex(txid_hex).map_err(|e| {
287+
let txid = Txid::from_hex(txid_hex).map_err(|_| {
288288
KVStoreUpdatingPersisterError::BadMonitorName {
289-
reason: format!("bad txid, caused by: {}", e),
290-
context: value.0.clone(),
289+
reason: "bad txid in monitor name",
290+
context: &value.0,
291291
}
292292
})?;
293293
Ok(OutPoint { txid, index })
@@ -306,8 +306,8 @@ pub struct UpdateName(String);
306306

307307
impl UpdateName {
308308
/// The key to store this update with.
309-
fn storage_key(&self) -> String {
310-
self.0.clone()
309+
fn storage_key(&self) -> &str {
310+
&self.0
311311
}
312312
}
313313

@@ -356,7 +356,7 @@ where
356356
let (bh, monitor) = self.deserialize_monitor(
357357
entropy_source.clone(),
358358
signer_provider.clone(),
359-
monitor_name.clone(),
359+
&monitor_name,
360360
)?;
361361
// ...parse and apply the updates with an id higher than the monitor.
362362
for update_name in self.list_update_names(&monitor_name)? {
@@ -367,8 +367,8 @@ where
367367
monitor
368368
.update_monitor(&update, broadcaster, fee_estimator.clone(), &self.logger)
369369
.map_err(|_| KVStoreUpdatingPersisterError::UpdateFailed {
370-
reason: "update_monitor returned Err(())".to_string(),
371-
context: format!("monitor: {:?}", monitor_name),
370+
reason: "update_monitor returned Err(())",
371+
context: &monitor_name.0,
372372
})?;
373373
}
374374
}
@@ -382,7 +382,7 @@ where
382382
fn monitor_update_namespace(&self, monitor_name: &MonitorName) -> String {
383383
// Append the monitor name to the namespace with an underscore.
384384
[
385-
CHANNEL_MONITOR_UPDATE_PERSISTENCE_NAMESPACE.to_owned(),
385+
CHANNEL_MONITOR_UPDATE_PERSISTENCE_NAMESPACE,
386386
monitor_name.storage_key(),
387387
]
388388
.join("_")
@@ -407,7 +407,7 @@ where
407407
&self,
408408
entropy_source: ES,
409409
signer_provider: SP,
410-
monitor_name: MonitorName,
410+
monitor_name: &MonitorName,
411411
) -> io::Result<(
412412
BlockHash,
413413
ChannelMonitor<<SP::Target as SignerProvider>::Signer>,
@@ -417,17 +417,17 @@ where
417417
SP::Target: SignerProvider + Sized,
418418
{
419419
let key = monitor_name.storage_key();
420-
let outpoint: OutPoint = monitor_name.try_into()?;
420+
let outpoint: OutPoint = monitor_name.to_owned().try_into()?;
421421
match <(
422422
BlockHash,
423423
ChannelMonitor<<SP::Target as SignerProvider>::Signer>,
424424
)>::read(
425425
&mut self
426426
.kv
427-
.read(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, &key)
427+
.read(CHANNEL_MONITOR_PERSISTENCE_NAMESPACE, key)
428428
.map_err(|e| KVStoreUpdatingPersisterError::StorageReadFailed {
429429
reason: e,
430-
context: key.clone(),
430+
context: key,
431431
})?,
432432
(&*entropy_source, &*signer_provider),
433433
) {
@@ -465,7 +465,7 @@ where
465465
ChannelMonitorUpdate::read(&mut self.kv.read(&ns, &key).map_err(|e| {
466466
KVStoreUpdatingPersisterError::StorageReadFailed {
467467
reason: e,
468-
context: key.clone(),
468+
context: key,
469469
}
470470
})?)
471471
.map_err(|e| KVStoreUpdatingPersisterError::UpdateDecodeFailed {
@@ -490,7 +490,7 @@ where
490490
{
491491
let ns = self.monitor_update_namespace(&monitor_name);
492492
let key = update_name.storage_key();
493-
self.kv.remove(&ns, &key)?;
493+
self.kv.remove(&ns, key)?;
494494
}
495495
}
496496
Ok(())

0 commit comments

Comments
 (0)