From a1c10938c527cf85d3f5c006901505d2b2f6e7a5 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Mon, 29 Apr 2019 20:37:20 +0200 Subject: [PATCH] Use Cow instead of String --- src/cache.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index 5ae46d3..8318b98 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -1,21 +1,22 @@ use crate::Error; use std::{ + borrow::Cow, fs::{create_dir_all, File, OpenOptions}, io::BufReader, path::Path, }; /// Enum to indicate the state of the cache -pub(crate) enum CacheState { +pub(crate) enum CacheState<'a> { /// Current head and cached head are the same Current(u64), /// Cached head is older than current head - Old(Cache), + Old(Cache<'a>), /// No cache was found No, } -impl CacheState { +impl<'a> CacheState<'a> { pub(crate) fn read_from_file(path: impl AsRef, head: &str) -> Result { if path.as_ref().exists() { let cache: Cache = serde_json::from_reader(BufReader::new(File::open(path)?))?; @@ -29,7 +30,7 @@ impl CacheState { } } - pub(crate) fn calculate_new_cache(self, count: u64, head: String) -> Cache { + pub(crate) fn calculate_new_cache(self, count: u64, head: Cow<'a, str>) -> Cache { match self { CacheState::Old(mut cache) => { cache.head = head; @@ -42,12 +43,12 @@ impl CacheState { } #[derive(Serialize, Deserialize)] -pub(crate) struct Cache { - pub head: String, +pub(crate) struct Cache<'a> { + pub head: Cow<'a, str>, pub count: u64, } -impl Cache { +impl<'a> Cache<'a> { pub(crate) fn write_to_file(&self, path: impl AsRef) -> Result<(), Error> { create_dir_all(path.as_ref().parent().ok_or(Error::Internal)?)?; serde_json::to_writer(