From d662b9a23624ec93a43bcf1fd9403078dbc99940 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Fri, 16 Oct 2020 16:39:04 +0200 Subject: [PATCH] perf: use Cow to avoid some PathBuf allocations Signed-off-by: ljedrz --- compiler/src/output/output_file.rs | 16 +++++++++------- leo/commands/build.rs | 4 ++-- package/src/imports/directory.rs | 16 ++++++++-------- package/src/inputs/directory.rs | 5 +++-- package/src/inputs/input.rs | 20 ++++++++++++-------- package/src/inputs/state.rs | 20 ++++++++++++-------- package/src/outputs/checksum.rs | 16 +++++++++------- package/src/outputs/circuit.rs | 16 +++++++++------- package/src/outputs/directory.rs | 10 +++++----- package/src/outputs/proof.rs | 16 +++++++++------- package/src/outputs/proving_key.rs | 20 +++++++++++--------- package/src/outputs/verification_key.rs | 24 +++++++++++++++--------- package/src/root/gitignore.rs | 10 +++++----- package/src/root/manifest.rs | 13 +++++++------ package/src/root/readme.rs | 10 +++++----- package/src/root/zip.rs | 18 ++++++++++-------- package/src/source/directory.rs | 9 +++++---- package/src/source/library.rs | 14 +++++++------- package/src/source/main.rs | 14 +++++++------- 19 files changed, 150 insertions(+), 121 deletions(-) diff --git a/compiler/src/output/output_file.rs b/compiler/src/output/output_file.rs index 9c326437bc..fabe70b585 100644 --- a/compiler/src/output/output_file.rs +++ b/compiler/src/output/output_file.rs @@ -19,9 +19,10 @@ use crate::errors::OutputFileError; use std::{ + borrow::Cow, fs::{self, File}, io::Write, - path::{Path, PathBuf}, + path::Path, }; pub static OUTPUTS_DIRECTORY_NAME: &str = "outputs/"; @@ -47,7 +48,7 @@ impl OutputFile { pub fn read_from(&self, path: &Path) -> Result { let path = self.setup_file_path(path); - let output = fs::read_to_string(&path).map_err(|_| OutputFileError::FileReadError(path.clone()))?; + let output = fs::read_to_string(&path).map_err(|_| OutputFileError::FileReadError(path.into_owned()))?; Ok(output) } @@ -68,17 +69,18 @@ impl OutputFile { return Ok(false); } - fs::remove_file(&path).map_err(|_| OutputFileError::FileRemovalError(path.clone()))?; + fs::remove_file(&path).map_err(|_| OutputFileError::FileRemovalError(path.into_owned()))?; Ok(true) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, OUTPUT_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, OUTPUT_FILE_EXTENSION)); } path } diff --git a/leo/commands/build.rs b/leo/commands/build.rs index c48b8ec85f..9d19bd2352 100644 --- a/leo/commands/build.rs +++ b/leo/commands/build.rs @@ -124,9 +124,9 @@ impl CLI for BuildCommand { main_file_path, output_directory, &input_string, - input_path, + input_path.into_owned(), &state_string, - state_path, + state_path.into_owned(), )?; // Compute the current program checksum diff --git a/package/src/imports/directory.rs b/package/src/imports/directory.rs index 03fad61dd0..d301aded63 100644 --- a/package/src/imports/directory.rs +++ b/package/src/imports/directory.rs @@ -16,7 +16,7 @@ use crate::errors::ImportsDirectoryError; -use std::{fs, path::Path}; +use std::{borrow::Cow, fs, path::Path}; pub static IMPORTS_DIRECTORY_NAME: &str = "imports/"; @@ -25,9 +25,9 @@ pub struct ImportsDirectory; impl ImportsDirectory { /// Creates a directory at the provided path with the default directory name. pub fn create(path: &Path) -> Result<(), ImportsDirectoryError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) { - path.push(IMPORTS_DIRECTORY_NAME); + path.to_mut().push(IMPORTS_DIRECTORY_NAME); } fs::create_dir_all(&path).map_err(ImportsDirectoryError::Creating) @@ -35,9 +35,9 @@ impl ImportsDirectory { /// Removes the directory at the provided path. pub fn remove(path: &Path) -> Result<(), ImportsDirectoryError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) { - path.push(IMPORTS_DIRECTORY_NAME); + path.to_mut().push(IMPORTS_DIRECTORY_NAME); } if path.exists() { @@ -49,12 +49,12 @@ impl ImportsDirectory { /// Removes an imported package in the imports directory at the provided path. pub fn remove_import(path: &Path, package_name: &str) -> Result<(), ImportsDirectoryError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() && !path.ends_with(IMPORTS_DIRECTORY_NAME) { - path.push(IMPORTS_DIRECTORY_NAME); + path.to_mut().push(IMPORTS_DIRECTORY_NAME); } - path.push(package_name); + path.to_mut().push(package_name); if !path.exists() || !path.is_dir() { return Err(ImportsDirectoryError::ImportDoesNotExist(package_name.into())); diff --git a/package/src/inputs/directory.rs b/package/src/inputs/directory.rs index 7741349ac4..73b8443856 100644 --- a/package/src/inputs/directory.rs +++ b/package/src/inputs/directory.rs @@ -17,6 +17,7 @@ use crate::errors::InputsDirectoryError; use std::{ + borrow::Cow, fs, fs::ReadDir, path::{Path, PathBuf}, @@ -29,9 +30,9 @@ pub struct InputsDirectory; impl InputsDirectory { /// Creates a directory at the provided path with the default directory name. pub fn create(path: &Path) -> Result<(), InputsDirectoryError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() && !path.ends_with(INPUTS_DIRECTORY_NAME) { - path.push(INPUTS_DIRECTORY_NAME); + path.to_mut().push(INPUTS_DIRECTORY_NAME); } fs::create_dir_all(&path).map_err(InputsDirectoryError::Creating) diff --git a/package/src/inputs/input.rs b/package/src/inputs/input.rs index 4b4f4229e1..dedf7a3177 100644 --- a/package/src/inputs/input.rs +++ b/package/src/inputs/input.rs @@ -20,9 +20,10 @@ use crate::{errors::InputFileError, inputs::INPUTS_DIRECTORY_NAME}; use serde::Deserialize; use std::{ + borrow::Cow, fs::{self, File}, io::Write, - path::{Path, PathBuf}, + path::Path, }; pub static INPUT_FILE_EXTENSION: &str = ".in"; @@ -49,11 +50,13 @@ impl InputFile { } /// Reads the program input variables from the given file path if it exists. - pub fn read_from(&self, path: &Path) -> Result<(String, PathBuf), InputFileError> { + pub fn read_from<'a>(&self, path: &'a Path) -> Result<(String, Cow<'a, Path>), InputFileError> { let path = self.setup_file_path(path); - let input = fs::read_to_string(&path).map_err(|_| InputFileError::FileReadError(path.clone()))?; - Ok((input, path)) + match fs::read_to_string(&path) { + Ok(input) => Ok((input, path)), + Err(_) => Err(InputFileError::FileReadError(path.into_owned())), + } } /// Writes the standard input format to a file. @@ -78,13 +81,14 @@ r0: u32 = 0; ) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(INPUTS_DIRECTORY_NAME) { - path.push(INPUTS_DIRECTORY_NAME); + path.to_mut().push(INPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, INPUT_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, INPUT_FILE_EXTENSION)); } path } diff --git a/package/src/inputs/state.rs b/package/src/inputs/state.rs index 2418425f88..5809a9b278 100644 --- a/package/src/inputs/state.rs +++ b/package/src/inputs/state.rs @@ -20,9 +20,10 @@ use crate::{errors::StateFileError, inputs::INPUTS_DIRECTORY_NAME}; use serde::Deserialize; use std::{ + borrow::Cow, fs::{self, File}, io::Write, - path::{Path, PathBuf}, + path::Path, }; pub static STATE_FILE_EXTENSION: &str = ".state"; @@ -49,11 +50,13 @@ impl StateFile { } /// Reads the state input variables from the given file path if it exists. - pub fn read_from(&self, path: &Path) -> Result<(String, PathBuf), StateFileError> { + pub fn read_from<'a>(&self, path: &'a Path) -> Result<(String, Cow<'a, Path>), StateFileError> { let path = self.setup_file_path(path); - let input = fs::read_to_string(&path).map_err(|_| StateFileError::FileReadError(path.clone()))?; - Ok((input, path)) + match fs::read_to_string(&path) { + Ok(input) => Ok((input, path)), + Err(_) => Err(StateFileError::FileReadError(path.into_owned())), + } } /// Writes the standard input format to a file. @@ -97,13 +100,14 @@ leaf_randomness: [u8; 32] = [0; 32]; ) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(INPUTS_DIRECTORY_NAME) { - path.push(INPUTS_DIRECTORY_NAME); + path.to_mut().push(INPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, STATE_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, STATE_FILE_EXTENSION)); } path } diff --git a/package/src/outputs/checksum.rs b/package/src/outputs/checksum.rs index fc029075ac..b5f37b6ca6 100644 --- a/package/src/outputs/checksum.rs +++ b/package/src/outputs/checksum.rs @@ -20,9 +20,10 @@ use crate::{errors::ChecksumFileError, outputs::OUTPUTS_DIRECTORY_NAME}; use serde::Deserialize; use std::{ + borrow::Cow, fs::{self, File}, io::Write, - path::{Path, PathBuf}, + path::Path, }; pub static CHECKSUM_FILE_EXTENSION: &str = ".sum"; @@ -48,7 +49,7 @@ impl ChecksumFile { pub fn read_from(&self, path: &Path) -> Result { let path = self.setup_file_path(path); - Ok(fs::read_to_string(&path).map_err(|_| ChecksumFileError::FileReadError(path.clone()))?) + Ok(fs::read_to_string(&path).map_err(|_| ChecksumFileError::FileReadError(path.into_owned()))?) } /// Writes the given checksum to a file. @@ -69,17 +70,18 @@ impl ChecksumFile { return Ok(false); } - fs::remove_file(&path).map_err(|_| ChecksumFileError::FileRemovalError(path.clone()))?; + fs::remove_file(&path).map_err(|_| ChecksumFileError::FileRemovalError(path.into_owned()))?; Ok(true) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, CHECKSUM_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, CHECKSUM_FILE_EXTENSION)); } path } diff --git a/package/src/outputs/circuit.rs b/package/src/outputs/circuit.rs index f7f02be048..b6fb52bf31 100644 --- a/package/src/outputs/circuit.rs +++ b/package/src/outputs/circuit.rs @@ -20,9 +20,10 @@ use crate::{errors::CircuitFileError, outputs::OUTPUTS_DIRECTORY_NAME}; use serde::Deserialize; use std::{ + borrow::Cow, fs::{self, File}, io::Write, - path::{Path, PathBuf}, + path::Path, }; pub static CIRCUIT_FILE_EXTENSION: &str = ".json"; @@ -48,7 +49,7 @@ impl CircuitFile { pub fn read_from(&self, path: &Path) -> Result { let path = self.setup_file_path(path); - Ok(fs::read_to_string(&path).map_err(|_| CircuitFileError::FileReadError(path.clone()))?) + Ok(fs::read_to_string(&path).map_err(|_| CircuitFileError::FileReadError(path.into_owned()))?) } /// Writes the given serialized circuit to a file. @@ -69,17 +70,18 @@ impl CircuitFile { return Ok(false); } - fs::remove_file(&path).map_err(|_| CircuitFileError::FileRemovalError(path.clone()))?; + fs::remove_file(&path).map_err(|_| CircuitFileError::FileRemovalError(path.into_owned()))?; Ok(true) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, CIRCUIT_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, CIRCUIT_FILE_EXTENSION)); } path } diff --git a/package/src/outputs/directory.rs b/package/src/outputs/directory.rs index 60da1d0e45..2e0d810add 100644 --- a/package/src/outputs/directory.rs +++ b/package/src/outputs/directory.rs @@ -16,7 +16,7 @@ use crate::errors::OutputsDirectoryError; -use std::{fs, path::Path}; +use std::{borrow::Cow, fs, path::Path}; pub static OUTPUTS_DIRECTORY_NAME: &str = "outputs/"; @@ -25,9 +25,9 @@ pub struct OutputsDirectory; impl OutputsDirectory { /// Creates a directory at the provided path with the default directory name. pub fn create(path: &Path) -> Result<(), OutputsDirectoryError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } fs::create_dir_all(&path).map_err(OutputsDirectoryError::Creating) @@ -35,9 +35,9 @@ impl OutputsDirectory { /// Removes the directory at the provided path. pub fn remove(path: &Path) -> Result<(), OutputsDirectoryError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() && !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } if path.exists() { diff --git a/package/src/outputs/proof.rs b/package/src/outputs/proof.rs index 10b4532774..161b84e218 100644 --- a/package/src/outputs/proof.rs +++ b/package/src/outputs/proof.rs @@ -20,9 +20,10 @@ use crate::{errors::ProofFileError, outputs::OUTPUTS_DIRECTORY_NAME}; use serde::Deserialize; use std::{ + borrow::Cow, fs::{self, File}, io::Write, - path::{Path, PathBuf}, + path::Path, }; pub static PROOF_FILE_EXTENSION: &str = ".proof"; @@ -48,7 +49,7 @@ impl ProofFile { pub fn read_from(&self, path: &Path) -> Result { let path = self.setup_file_path(path); - let proof = fs::read_to_string(&path).map_err(|_| ProofFileError::FileReadError(path.clone()))?; + let proof = fs::read_to_string(&path).map_err(|_| ProofFileError::FileReadError(path.into_owned()))?; Ok(proof) } @@ -72,17 +73,18 @@ impl ProofFile { return Ok(false); } - fs::remove_file(&path).map_err(|_| ProofFileError::FileRemovalError(path.clone()))?; + fs::remove_file(&path).map_err(|_| ProofFileError::FileRemovalError(path.into_owned()))?; Ok(true) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, PROOF_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, PROOF_FILE_EXTENSION)); } path } diff --git a/package/src/outputs/proving_key.rs b/package/src/outputs/proving_key.rs index e011ba411d..d5d6d74e24 100644 --- a/package/src/outputs/proving_key.rs +++ b/package/src/outputs/proving_key.rs @@ -20,9 +20,10 @@ use crate::{errors::ProvingKeyFileError, outputs::OUTPUTS_DIRECTORY_NAME}; use serde::Deserialize; use std::{ + borrow::Cow, fs::{self, File}, io::Write, - path::{Path, PathBuf}, + path::Path, }; pub static PROVING_KEY_FILE_EXTENSION: &str = ".lpk"; @@ -39,7 +40,7 @@ impl ProvingKeyFile { } } - pub fn full_path(&self, path: &Path) -> PathBuf { + pub fn full_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { self.setup_file_path(path) } @@ -52,11 +53,11 @@ impl ProvingKeyFile { pub fn read_from(&self, path: &Path) -> Result, ProvingKeyFileError> { let path = self.setup_file_path(path); - Ok(fs::read(&path).map_err(|_| ProvingKeyFileError::FileReadError(path.clone()))?) + Ok(fs::read(&path).map_err(|_| ProvingKeyFileError::FileReadError(path.into_owned()))?) } /// Writes the given proving key to a file. - pub fn write_to(&self, path: &Path, proving_key: &[u8]) -> Result { + pub fn write_to<'a>(&self, path: &'a Path, proving_key: &[u8]) -> Result, ProvingKeyFileError> { let path = self.setup_file_path(path); let mut file = File::create(&path)?; @@ -73,17 +74,18 @@ impl ProvingKeyFile { return Ok(false); } - fs::remove_file(&path).map_err(|_| ProvingKeyFileError::FileRemovalError(path.clone()))?; + fs::remove_file(&path).map_err(|_| ProvingKeyFileError::FileRemovalError(path.into_owned()))?; Ok(true) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, PROVING_KEY_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, PROVING_KEY_FILE_EXTENSION)); } path } diff --git a/package/src/outputs/verification_key.rs b/package/src/outputs/verification_key.rs index c0267c7072..966c48846f 100644 --- a/package/src/outputs/verification_key.rs +++ b/package/src/outputs/verification_key.rs @@ -20,9 +20,10 @@ use crate::{errors::VerificationKeyFileError, outputs::OUTPUTS_DIRECTORY_NAME}; use serde::Deserialize; use std::{ + borrow::Cow, fs::{self, File}, io::Write, - path::{Path, PathBuf}, + path::Path, }; pub static VERIFICATION_KEY_FILE_EXTENSION: &str = ".lvk"; @@ -39,7 +40,7 @@ impl VerificationKeyFile { } } - pub fn full_path(&self, path: &Path) -> PathBuf { + pub fn full_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { self.setup_file_path(path) } @@ -52,11 +53,15 @@ impl VerificationKeyFile { pub fn read_from(&self, path: &Path) -> Result, VerificationKeyFileError> { let path = self.setup_file_path(path); - Ok(fs::read(&path).map_err(|_| VerificationKeyFileError::FileReadError(path.clone()))?) + Ok(fs::read(&path).map_err(|_| VerificationKeyFileError::FileReadError(path.into_owned()))?) } /// Writes the given verification key to a file. - pub fn write_to(&self, path: &Path, verification_key: &[u8]) -> Result { + pub fn write_to<'a>( + &self, + path: &'a Path, + verification_key: &[u8], + ) -> Result, VerificationKeyFileError> { let path = self.setup_file_path(path); let mut file = File::create(&path)?; @@ -73,17 +78,18 @@ impl VerificationKeyFile { return Ok(false); } - fs::remove_file(&path).map_err(|_| VerificationKeyFileError::FileRemovalError(path.clone()))?; + fs::remove_file(&path).map_err(|_| VerificationKeyFileError::FileRemovalError(path.into_owned()))?; Ok(true) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, VERIFICATION_KEY_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, VERIFICATION_KEY_FILE_EXTENSION)); } path } diff --git a/package/src/root/gitignore.rs b/package/src/root/gitignore.rs index babe568209..7414e9303e 100644 --- a/package/src/root/gitignore.rs +++ b/package/src/root/gitignore.rs @@ -19,7 +19,7 @@ use crate::errors::GitignoreError; use serde::Deserialize; -use std::{fs::File, io::Write, path::Path}; +use std::{borrow::Cow, fs::File, io::Write, path::Path}; pub static GITIGNORE_FILENAME: &str = ".gitignore"; @@ -32,17 +32,17 @@ impl Gitignore { } pub fn exists_at(path: &Path) -> bool { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { - path.push(GITIGNORE_FILENAME); + path.to_mut().push(GITIGNORE_FILENAME); } path.exists() } pub fn write_to(self, path: &Path) -> Result<(), GitignoreError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { - path.push(GITIGNORE_FILENAME); + path.to_mut().push(GITIGNORE_FILENAME); } let mut file = File::create(&path)?; diff --git a/package/src/root/manifest.rs b/package/src/root/manifest.rs index 400daed49b..235bcd6ebc 100644 --- a/package/src/root/manifest.rs +++ b/package/src/root/manifest.rs @@ -18,6 +18,7 @@ use crate::{errors::ManifestError, package::Package}; use serde::Deserialize; use std::{ + borrow::Cow, convert::TryFrom, fs::File, io::{Read, Write}, @@ -50,9 +51,9 @@ impl Manifest { } pub fn exists_at(path: &Path) -> bool { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { - path.push(MANIFEST_FILENAME); + path.to_mut().push(MANIFEST_FILENAME); } path.exists() } @@ -78,9 +79,9 @@ impl Manifest { } pub fn write_to(self, path: &Path) -> Result<(), ManifestError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { - path.push(MANIFEST_FILENAME); + path.to_mut().push(MANIFEST_FILENAME); } let mut file = File::create(&path).map_err(|error| ManifestError::Creating(MANIFEST_FILENAME, error))?; @@ -108,9 +109,9 @@ impl TryFrom<&Path> for Manifest { type Error = ManifestError; fn try_from(path: &Path) -> Result { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { - path.push(MANIFEST_FILENAME); + path.to_mut().push(MANIFEST_FILENAME); } let mut file = File::open(path.clone()).map_err(|error| ManifestError::Opening(MANIFEST_FILENAME, error))?; diff --git a/package/src/root/readme.rs b/package/src/root/readme.rs index 9fac44ed1c..d06c6b3fe9 100644 --- a/package/src/root/readme.rs +++ b/package/src/root/readme.rs @@ -19,7 +19,7 @@ use crate::errors::READMEError; use serde::Deserialize; -use std::{fs::File, io::Write, path::Path}; +use std::{borrow::Cow, fs::File, io::Write, path::Path}; pub static README_FILENAME: &str = "README.md"; @@ -40,17 +40,17 @@ impl README { } pub fn exists_at(path: &Path) -> bool { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { - path.push(README_FILENAME); + path.to_mut().push(README_FILENAME); } path.exists() } pub fn write_to(self, path: &Path) -> Result<(), READMEError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { - path.push(README_FILENAME); + path.to_mut().push(README_FILENAME); } let mut file = File::create(&path)?; diff --git a/package/src/root/zip.rs b/package/src/root/zip.rs index 2c3b11646b..a8ff66b39c 100644 --- a/package/src/root/zip.rs +++ b/package/src/root/zip.rs @@ -34,9 +34,10 @@ use crate::{ use serde::Deserialize; use std::{ + borrow::Cow, fs::{self, File}, io::{Read, Write}, - path::{Path, PathBuf}, + path::Path, }; use walkdir::WalkDir; use zip::write::{FileOptions, ZipWriter}; @@ -60,7 +61,7 @@ impl ZipFile { path.exists() } - pub fn get_file_path(&self, current_dir: &Path) -> PathBuf { + pub fn get_file_path<'a>(&self, current_dir: &'a Path) -> Cow<'a, Path> { self.setup_file_path(current_dir) } @@ -74,7 +75,7 @@ impl ZipFile { /// Writes the current package contents to a zip file. pub fn write(&self, src_dir: &Path) -> Result<(), ZipFileError> { // Build walkdir iterator from current package - let walkdir = WalkDir::new(src_dir.clone()); + let walkdir = WalkDir::new(src_dir); // Create zip file let path = self.setup_file_path(src_dir); @@ -131,17 +132,18 @@ impl ZipFile { return Ok(false); } - fs::remove_file(&path).map_err(|_| ZipFileError::FileRemovalError(path.clone()))?; + fs::remove_file(&path).map_err(|_| ZipFileError::FileRemovalError(path.into_owned()))?; Ok(true) } - fn setup_file_path(&self, path: &Path) -> PathBuf { - let mut path = path.to_owned(); + fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> { + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(OUTPUTS_DIRECTORY_NAME) { - path.push(OUTPUTS_DIRECTORY_NAME); + path.to_mut().push(OUTPUTS_DIRECTORY_NAME); } - path.push(format!("{}{}", self.package_name, ZIP_FILE_EXTENSION)); + path.to_mut() + .push(format!("{}{}", self.package_name, ZIP_FILE_EXTENSION)); } path } diff --git a/package/src/source/directory.rs b/package/src/source/directory.rs index 0bb5c372a1..0b63d615de 100644 --- a/package/src/source/directory.rs +++ b/package/src/source/directory.rs @@ -17,6 +17,7 @@ use crate::errors::SourceDirectoryError; use std::{ + borrow::Cow, fs, path::{Path, PathBuf}, }; @@ -30,9 +31,9 @@ pub struct SourceDirectory; impl SourceDirectory { /// Creates a directory at the provided path with the default directory name. pub fn create(path: &Path) -> Result<(), SourceDirectoryError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() && !path.ends_with(SOURCE_DIRECTORY_NAME) { - path.push(SOURCE_DIRECTORY_NAME); + path.to_mut().push(SOURCE_DIRECTORY_NAME); } fs::create_dir_all(&path).map_err(SourceDirectoryError::Creating) @@ -40,8 +41,8 @@ impl SourceDirectory { /// Returns a list of files in the source directory. pub fn files(path: &Path) -> Result, SourceDirectoryError> { - let mut path = path.to_owned(); - path.push(SOURCE_DIRECTORY_NAME); + let mut path = Cow::from(path); + path.to_mut().push(SOURCE_DIRECTORY_NAME); let directory = fs::read_dir(&path).map_err(SourceDirectoryError::Reading)?; let mut file_paths = Vec::new(); diff --git a/package/src/source/library.rs b/package/src/source/library.rs index cd7de735d7..275f2db1dc 100644 --- a/package/src/source/library.rs +++ b/package/src/source/library.rs @@ -19,7 +19,7 @@ use crate::{errors::LibraryFileError, source::directory::SOURCE_DIRECTORY_NAME}; use serde::Deserialize; -use std::{fs::File, io::Write, path::Path}; +use std::{borrow::Cow, fs::File, io::Write, path::Path}; pub static LIBRARY_FILENAME: &str = "lib.leo"; @@ -40,23 +40,23 @@ impl LibraryFile { } pub fn exists_at(path: &Path) -> bool { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(SOURCE_DIRECTORY_NAME) { - path.push(SOURCE_DIRECTORY_NAME); + path.to_mut().push(SOURCE_DIRECTORY_NAME); } - path.push(LIBRARY_FILENAME); + path.to_mut().push(LIBRARY_FILENAME); } path.exists() } pub fn write_to(self, path: &Path) -> Result<(), LibraryFileError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(SOURCE_DIRECTORY_NAME) { - path.push(SOURCE_DIRECTORY_NAME); + path.to_mut().push(SOURCE_DIRECTORY_NAME); } - path.push(LIBRARY_FILENAME); + path.to_mut().push(LIBRARY_FILENAME); } let mut file = File::create(&path)?; diff --git a/package/src/source/main.rs b/package/src/source/main.rs index 15a71506f9..149341e92c 100644 --- a/package/src/source/main.rs +++ b/package/src/source/main.rs @@ -19,7 +19,7 @@ use crate::{errors::MainFileError, source::directory::SOURCE_DIRECTORY_NAME}; use serde::Deserialize; -use std::{fs::File, io::Write, path::Path}; +use std::{borrow::Cow, fs::File, io::Write, path::Path}; pub static MAIN_FILENAME: &str = "main.leo"; @@ -40,23 +40,23 @@ impl MainFile { } pub fn exists_at(path: &Path) -> bool { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(SOURCE_DIRECTORY_NAME) { - path.push(SOURCE_DIRECTORY_NAME); + path.to_mut().push(SOURCE_DIRECTORY_NAME); } - path.push(MAIN_FILENAME); + path.to_mut().push(MAIN_FILENAME); } path.exists() } pub fn write_to(self, path: &Path) -> Result<(), MainFileError> { - let mut path = path.to_owned(); + let mut path = Cow::from(path); if path.is_dir() { if !path.ends_with(SOURCE_DIRECTORY_NAME) { - path.push(SOURCE_DIRECTORY_NAME); + path.to_mut().push(SOURCE_DIRECTORY_NAME); } - path.push(MAIN_FILENAME); + path.to_mut().push(MAIN_FILENAME); } let mut file = File::create(&path)?;