From 387b6241a235c7142b6f972cbd76aa8b3909d7b9 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 11 Aug 2019 13:48:04 +0200 Subject: [PATCH] Remove some clones by using Cow --- backend/src/cdn.rs | 6 +++--- backend/src/data.rs | 6 +++--- backend/src/main.rs | 4 ++-- backend/src/service.rs | 13 +++++++----- backend/src/statics.rs | 48 ++++++++++++++++++++++++++---------------- 5 files changed, 46 insertions(+), 31 deletions(-) diff --git a/backend/src/cdn.rs b/backend/src/cdn.rs index c7052db..916e890 100644 --- a/backend/src/cdn.rs +++ b/backend/src/cdn.rs @@ -24,7 +24,7 @@ impl Cloudflare { "https://api.cloudflare.com/client/v4/zones/{}/purge_cache", Self::identifier() )) - .header(header::USER_AGENT, statics::USER_AGENT.as_str()) + .header(header::USER_AGENT, &*statics::USER_AGENT.as_str()) .header("X-Auth-Email", Self::auth_email()) .header("X-Auth-Key", Self::auth_key()) .content_type("application/json") @@ -36,7 +36,7 @@ impl Cloudflare { // "https://api.cloudflare.com/client/v4/zones/{}/purge_cache", // Self::identifier() // )) - // .header(header::USER_AGENT, statics::USER_AGENT.as_str()) + // .header(header::USER_AGENT, statics::USER_AGENT.as_ref()) // .header("X-Auth-Email", Self::auth_email()) // .header("X-Auth-Key", Self::auth_key()) // .content_type("application/json") @@ -69,7 +69,7 @@ impl CfPurgeRequest { Self { files: vec![format!( "https://{}/{}/{}", - statics::HOSTNAME.as_str(), + statics::HOSTNAME.as_ref(), T::path(), file )], diff --git a/backend/src/data.rs b/backend/src/data.rs index 4c8395d..2a6780f 100644 --- a/backend/src/data.rs +++ b/backend/src/data.rs @@ -20,9 +20,9 @@ impl FilePath { pub(crate) fn to_key(&self) -> Key { Key::new( T::cache_service(), - self.user.clone(), - self.repo.clone(), - self.commit.clone(), + Arc::clone(&self.user), + Arc::clone(&self.repo), + Arc::clone(&self.commit), ) } } diff --git a/backend/src/main.rs b/backend/src/main.rs index 19112bc..6b202ba 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -158,7 +158,7 @@ fn purge_local_cache( cache: web::Data, data: web::Path, ) -> impl Future { - let cache = cache.clone(); + let cache = Arc::clone(&cache); futures::future::ok(()).map(move |_| { if let Ok(mut cache) = cache.write() { info!("Invalidating local cache for {}/{}", T::path(), data.path()); @@ -188,7 +188,7 @@ fn main() -> Result<()> { Ok(HttpServer::new(move || { App::new() .data(Client::new()) - .data(state.clone()) + .data(Arc::clone(&state)) .wrap(middleware::Logger::default()) .wrap(middleware::NormalizePath) .service(favicon32) diff --git a/backend/src/service.rs b/backend/src/service.rs index 19fda9c..6bf1022 100644 --- a/backend/src/service.rs +++ b/backend/src/service.rs @@ -12,6 +12,7 @@ use actix_web::{ use awc::{error::PayloadError, Client, ClientResponse}; use bytes::Bytes; use futures::{Future, Stream}; +use std::borrow::Cow; pub(crate) trait ApiResponse { fn commit_ref(&self) -> &str; @@ -126,15 +127,17 @@ pub(crate) trait Service: Sized { pub(crate) struct Github; impl Github { - pub(crate) fn auth_query() -> Option { + pub(crate) fn auth_query() -> Option> { OPT.github_id - .clone() + .as_ref() + .map(Cow::from) .or_else(|| load_env_var("GITHUB_CLIENT_ID")) .and_then(|id| { OPT.github_secret - .clone() + .as_ref() + .map(Cow::from) .or_else(|| load_env_var("GITHUB_CLIENT_SECRET")) - .map(|secret| format!("?client_id={}&client_secret={}", id, secret)) + .map(|secret| format!("?client_id={}&client_secret={}", id, secret).into()) }) } } @@ -167,7 +170,7 @@ impl Service for Github { path.user, path.repo, path.commit, - GITHUB_AUTH_QUERY.as_str() + GITHUB_AUTH_QUERY.as_ref() ) } diff --git a/backend/src/statics.rs b/backend/src/statics.rs index 4c62603..58c0fa6 100644 --- a/backend/src/statics.rs +++ b/backend/src/statics.rs @@ -1,5 +1,5 @@ use crate::{config::Opt, service::Github}; -use std::{env, time::Duration}; +use std::{borrow::Cow, env, time::Duration}; use structopt::StructOpt; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -8,31 +8,43 @@ pub(crate) const FAVICON: &[u8] = include_bytes!("../static/favicon32.png"); lazy_static! { pub(crate) static ref USER_AGENT: String = format!("yagcdn/{}", VERSION); pub(crate) static ref OPT: Opt = Opt::from_args(); - pub(crate) static ref GITHUB_AUTH_QUERY: String = Github::auth_query().unwrap_or_default(); - pub(crate) static ref CF_ZONE_IDENT: String = OPT + pub(crate) static ref GITHUB_AUTH_QUERY: Cow<'static, str> = + Github::auth_query().unwrap_or_default().into(); + pub(crate) static ref CF_ZONE_IDENT: Cow<'static, str> = OPT .cf_zone - .clone() + .as_ref() + .map(Cow::from) .or_else(|| load_env_var("CF_ZONE_IDENT")) - .expect("Cloudflare zone identifier not set"); - pub(crate) static ref CF_AUTH_KEY: String = OPT + .expect("Cloudflare zone identifier not set") + .into(); + pub(crate) static ref CF_AUTH_KEY: Cow<'static, str> = OPT .cf_auth_key - .clone() + .as_ref() + .map(Cow::from) .or_else(|| load_env_var("CF_AUTH_KEY")) - .expect("Cloudflare auth key not set"); - pub(crate) static ref CF_AUTH_USER: String = OPT + .expect("Cloudflare auth key not set") + .into(); + pub(crate) static ref CF_AUTH_USER: Cow<'static, str> = OPT .cf_auth_user - .clone() + .as_ref() + .map(Cow::from) .or_else(|| load_env_var("CF_AUTH_USER")) - .expect("Cloudflare auth user not set"); - pub(crate) static ref HOSTNAME: String = OPT + .expect("Cloudflare auth user not set") + .into(); + pub(crate) static ref HOSTNAME: Cow<'static, str> = OPT .hostname - .clone() + .as_ref() + .map(Cow::from) .or_else(|| load_env_var("YAGCDN_HOSTNAME")) - .unwrap_or_else(|| "yagcdn.tk".to_string()); + .unwrap_or_else(|| "yagcdn.tk".into()); } -pub(crate) fn load_env_var(key: &str) -> Option { - env::var(key) - .ok() - .and_then(|val| if val.is_empty() { None } else { Some(val) }) +pub(crate) fn load_env_var(key: &str) -> Option> { + env::var(key).ok().and_then(|val| { + if val.is_empty() { + None + } else { + Some(val.into()) + } + }) }