Replace as many Box<dyn Trait> with generics as possible #32

Merged
wojtek merged 7 commits from 31---replace-as-many-Box-dyn-trait-with-generics-as-possible into main 2023-04-13 15:29:14 +02:00
2 changed files with 9 additions and 15 deletions
Showing only changes of commit 45e65073e9 - Show all commits

View File

@ -56,18 +56,15 @@ pub trait CollectionManager {
/// The collection manager. It is responsible for pulling information from both the library and the /// The collection manager. It is responsible for pulling information from both the library and the
/// database, ensuring its consistent and writing back any changes. /// database, ensuring its consistent and writing back any changes.
pub struct MhCollectionManager { pub struct MhCollectionManager<L> {
library: Box<dyn Library + Send + Sync>, library: L,
database: Box<dyn Database + Send + Sync>, database: Box<dyn Database + Send + Sync>,
collection: Collection, collection: Collection,
} }
impl MhCollectionManager { impl<L: Library> MhCollectionManager<L> {
/// Create a new [`CollectionManager`] with the provided [`Library`] and [`Database`]. /// Create a new [`CollectionManager`] with the provided [`Library`] and [`Database`].
pub fn new( pub fn new(library: L, database: Box<dyn Database + Send + Sync>) -> Self {
library: Box<dyn Library + Send + Sync>,
database: Box<dyn Database + Send + Sync>,
) -> Self {
MhCollectionManager { MhCollectionManager {
library, library,
database, database,
@ -76,7 +73,7 @@ impl MhCollectionManager {
} }
} }
impl CollectionManager for MhCollectionManager { impl<L: Library> CollectionManager for MhCollectionManager<L> {
fn rescan_library(&mut self) -> Result<(), Error> { fn rescan_library(&mut self) -> Result<(), Error> {
self.collection = self.library.list(&Query::default())?; self.collection = self.library.list(&Query::default())?;
Ok(()) Ok(())
@ -127,8 +124,7 @@ mod tests {
.times(1) .times(1)
.return_once(|_| database_result); .return_once(|_| database_result);
let mut collection_manager = let mut collection_manager = MhCollectionManager::new(library, Box::new(database));
MhCollectionManager::new(Box::new(library), Box::new(database));
collection_manager.rescan_library().unwrap(); collection_manager.rescan_library().unwrap();
assert_eq!(collection_manager.get_collection(), &*COLLECTION); assert_eq!(collection_manager.get_collection(), &*COLLECTION);
@ -147,8 +143,7 @@ mod tests {
.times(1) .times(1)
.return_once(|_| library_result); .return_once(|_| library_result);
let mut collection_manager = let mut collection_manager = MhCollectionManager::new(library, Box::new(database));
MhCollectionManager::new(Box::new(library), Box::new(database));
let actual_err = collection_manager.rescan_library().unwrap_err(); let actual_err = collection_manager.rescan_library().unwrap_err();
let expected_err = Error::LibraryError( let expected_err = Error::LibraryError(
@ -171,8 +166,7 @@ mod tests {
.times(1) .times(1)
.return_once(|_| database_result); .return_once(|_| database_result);
let mut collection_manager = let mut collection_manager = MhCollectionManager::new(library, Box::new(database));
MhCollectionManager::new(Box::new(library), Box::new(database));
let actual_err = collection_manager.save_to_database().unwrap_err(); let actual_err = collection_manager.save_to_database().unwrap_err();
let expected_err = let expected_err =

View File

@ -49,7 +49,7 @@ fn main() {
&opt.database_file_path, &opt.database_file_path,
))); )));
let collection_manager = MhCollectionManager::new(Box::new(beets), Box::new(database)); let collection_manager = MhCollectionManager::new(beets, Box::new(database));
// Initialize the terminal user interface. // Initialize the terminal user interface.
let backend = CrosstermBackend::new(io::stdout()); let backend = CrosstermBackend::new(io::stdout());