From 7ec348a0ac52bf35b3244a211ce16ec09067b84b Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Mon, 10 Apr 2023 21:59:47 +0200 Subject: [PATCH] Remove unsafe from BeetsLibraryCommandExecutor --- src/library/beets.rs | 35 +++++++++-------------------------- src/library/mod.rs | 1 + src/main.rs | 6 +++--- tests/library/beets.rs | 12 ++++++------ 4 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/library/beets.rs b/src/library/beets.rs index c6a3e7b..77e71fe 100644 --- a/src/library/beets.rs +++ b/src/library/beets.rs @@ -237,13 +237,6 @@ impl LibraryPrivate for BeetsLibrary { } /// Beets library executor that executes beets commands in their own process. -/// -/// # Safety -/// -/// The beets executable is not safe to call concurrently for operations on the same -/// database/library. Therefore, all functions that create a [BeetsLibraryCommandExecutor] or modify -/// which library it works with are marked unsafe. It is the caller's responsibility to make sure -/// the library is not being concurrently accessed from anywhere else. pub struct BeetsLibraryCommandExecutor { bin: String, config: Option, @@ -251,37 +244,27 @@ pub struct BeetsLibraryCommandExecutor { impl BeetsLibraryCommandExecutor { /// Create a new [BeetsLibraryCommandExecutor] that uses the provided beets executable. - /// - /// # Safety - /// - /// The caller must ensure the library is not being concurrently accessed from anywhere else. - pub unsafe fn new(bin: &str) -> Self { + pub fn new(bin: &str) -> Self { BeetsLibraryCommandExecutor { bin: bin.to_string(), config: None, } } - /// Create a new [BeetsLibraryCommandExecutor] that uses the system's default beets executable. - /// - /// # Safety - /// - /// The caller must ensure the library is not being concurrently accessed from anywhere else. - pub unsafe fn default() -> Self { - BeetsLibraryCommandExecutor::new("beet") - } - /// Update the configuration file passed to the beets executable. - /// - /// # Safety - /// - /// The caller must ensure the library is not being concurrently accessed from anywhere else. - pub unsafe fn config(mut self, path: Option<&Path>) -> Self { + pub fn config(mut self, path: Option<&Path>) -> Self { self.config = path.map(|p| p.to_path_buf()); self } } +impl Default for BeetsLibraryCommandExecutor { + /// Create a new [BeetsLibraryCommandExecutor] that uses the system's default beets executable. + fn default() -> Self { + BeetsLibraryCommandExecutor::new("beet") + } +} + impl BeetsLibraryExecutor for BeetsLibraryCommandExecutor { fn exec(&mut self, arguments: &[String]) -> Result, Error> { let mut cmd = Command::new(&self.bin); diff --git a/src/library/mod.rs b/src/library/mod.rs index 6fa2634..3c1584b 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -29,6 +29,7 @@ impl QueryOption { } impl Default for QueryOption { + /// Create a [QueryOption::None] for type `T`. fn default() -> Self { Self::None } diff --git a/src/main.rs b/src/main.rs index 44c2f3c..069d0b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,9 +36,9 @@ struct Opt { fn main() { let opt = Opt::from_args(); - let mut beets = BeetsLibrary::new(Box::new(unsafe { - BeetsLibraryCommandExecutor::default().config(opt.beets_config_file_path.as_deref()) - })); + let mut beets = BeetsLibrary::new(Box::new( + BeetsLibraryCommandExecutor::default().config(opt.beets_config_file_path.as_deref()), + )); let collection = beets .list(&Query::new()) diff --git a/tests/library/beets.rs b/tests/library/beets.rs index 4b8e9b8..efdecbf 100644 --- a/tests/library/beets.rs +++ b/tests/library/beets.rs @@ -16,17 +16,17 @@ use musichoard::{ use crate::COLLECTION; static BEETS_EMPTY_CONFIG: Lazy>> = Lazy::new(|| { - Arc::new(Mutex::new(BeetsLibrary::new(Box::new(unsafe { - BeetsLibraryCommandExecutor::default() - })))) + Arc::new(Mutex::new(BeetsLibrary::new(Box::new( + BeetsLibraryCommandExecutor::default(), + )))) }); static BEETS_TEST_CONFIG: Lazy>> = Lazy::new(|| { - Arc::new(Mutex::new(BeetsLibrary::new(Box::new(unsafe { + Arc::new(Mutex::new(BeetsLibrary::new(Box::new( BeetsLibraryCommandExecutor::default().config(Some( &fs::canonicalize("./tests/files/library/config.yml").unwrap(), - )) - })))) + )), + )))) }); #[test] -- 2.45.2