Change unnecessary usage of Arc to borrow instead

Some basic cleanup of function signatures that took ownership of their
parameters, even though they didn't need ownership. Switching over the
usage of `Arc` to a normal borrow has the added benefit of cleaning up
the code a bit since now a reference can be given instead of having to
clone the values. The other benefit is that a lot of clones aren't
necessary anymore. It's not going to have noticable performance
benefits, but it is still a good thing to have less clones all over the
code.
This commit is contained in:
Thomas Frans
2023-03-05 00:02:25 +01:00
committed by GitHub
parent e68f50ddff
commit 98a0596c70
16 changed files with 135 additions and 141 deletions

View File

@@ -152,7 +152,7 @@ impl fmt::Debug for Album {
}
impl ListItem for Album {
fn is_playing(&self, queue: Arc<Queue>) -> bool {
fn is_playing(&self, queue: &Queue) -> bool {
if let Some(tracks) = self.tracks.as_ref() {
let playing: Vec<String> = queue
.queue
@@ -169,11 +169,11 @@ impl ListItem for Album {
}
}
fn display_left(&self, _library: Arc<Library>) -> String {
fn display_left(&self, _library: &Library) -> String {
format!("{self}")
}
fn display_right(&self, library: Arc<Library>) -> String {
fn display_right(&self, library: &Library) -> String {
let saved = if library.is_saved_album(self) {
if library.cfg.values().use_nerdfont.unwrap_or(false) {
"\u{f62b} "
@@ -186,7 +186,7 @@ impl ListItem for Album {
format!("{}{}", saved, self.year)
}
fn play(&mut self, queue: Arc<Queue>) {
fn play(&mut self, queue: &Queue) {
self.load_all_tracks(queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
@@ -199,7 +199,7 @@ impl ListItem for Album {
}
}
fn play_next(&mut self, queue: Arc<Queue>) {
fn play_next(&mut self, queue: &Queue) {
self.load_all_tracks(queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
@@ -209,7 +209,7 @@ impl ListItem for Album {
}
}
fn queue(&mut self, queue: Arc<Queue>) {
fn queue(&mut self, queue: &Queue) {
self.load_all_tracks(queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
@@ -219,7 +219,7 @@ impl ListItem for Album {
}
}
fn toggle_saved(&mut self, library: Arc<Library>) {
fn toggle_saved(&mut self, library: &Library) {
if library.is_saved_album(self) {
library.unsave_album(self);
} else {
@@ -227,11 +227,11 @@ impl ListItem for Album {
}
}
fn save(&mut self, library: Arc<Library>) {
fn save(&mut self, library: &Library) {
library.save_album(self);
}
fn unsave(&mut self, library: Arc<Library>) {
fn unsave(&mut self, library: &Library) {
library.unsave_album(self);
}
@@ -303,7 +303,7 @@ impl ListItem for Album {
}
#[inline]
fn is_saved(&self, library: Arc<Library>) -> Option<bool> {
fn is_saved(&self, library: &Library) -> Option<bool> {
Some(library.is_saved_album(self))
}

View File

@@ -78,7 +78,7 @@ impl fmt::Debug for Artist {
}
impl ListItem for Artist {
fn is_playing(&self, queue: Arc<Queue>) -> bool {
fn is_playing(&self, queue: &Queue) -> bool {
if let Some(tracks) = &self.tracks {
let playing: Vec<String> = queue
.queue
@@ -94,11 +94,11 @@ impl ListItem for Artist {
}
}
fn display_left(&self, _library: Arc<Library>) -> String {
fn display_left(&self, _library: &Library) -> String {
format!("{self}")
}
fn display_right(&self, library: Arc<Library>) -> String {
fn display_right(&self, library: &Library) -> String {
let followed = if library.is_followed_artist(self) {
if library.cfg.values().use_nerdfont.unwrap_or(false) {
"\u{f62b} "
@@ -118,7 +118,7 @@ impl ListItem for Artist {
format!("{followed}{tracks}")
}
fn play(&mut self, queue: Arc<Queue>) {
fn play(&mut self, queue: &Queue) {
self.load_top_tracks(queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
@@ -131,7 +131,7 @@ impl ListItem for Artist {
}
}
fn play_next(&mut self, queue: Arc<Queue>) {
fn play_next(&mut self, queue: &Queue) {
self.load_top_tracks(queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
@@ -141,7 +141,7 @@ impl ListItem for Artist {
}
}
fn queue(&mut self, queue: Arc<Queue>) {
fn queue(&mut self, queue: &Queue) {
self.load_top_tracks(queue.get_spotify());
if let Some(tracks) = &self.tracks {
@@ -151,7 +151,7 @@ impl ListItem for Artist {
}
}
fn toggle_saved(&mut self, library: Arc<Library>) {
fn toggle_saved(&mut self, library: &Library) {
if library.is_followed_artist(self) {
library.unfollow_artist(self);
} else {
@@ -159,11 +159,11 @@ impl ListItem for Artist {
}
}
fn save(&mut self, library: Arc<Library>) {
fn save(&mut self, library: &Library) {
library.follow_artist(self);
}
fn unsave(&mut self, library: Arc<Library>) {
fn unsave(&mut self, library: &Library) {
library.unfollow_artist(self);
}
@@ -203,7 +203,7 @@ impl ListItem for Artist {
}
#[inline]
fn is_saved(&self, library: Arc<Library>) -> Option<bool> {
fn is_saved(&self, library: &Library) -> Option<bool> {
Some(library.is_followed_artist(self))
}

View File

@@ -1,6 +1,8 @@
use std::sync::Arc;
use crate::{
library::Library,
queue::Queue,
traits::{IntoBoxedViewExt, ListItem},
ui::listview::ListView,
};
@@ -21,29 +23,29 @@ impl From<&rspotify::model::Category> for Category {
}
impl ListItem for Category {
fn is_playing(&self, _queue: Arc<crate::queue::Queue>) -> bool {
fn is_playing(&self, _queue: &Queue) -> bool {
false
}
fn display_left(&self, _library: Arc<crate::library::Library>) -> String {
fn display_left(&self, _library: &Library) -> String {
self.name.clone()
}
fn display_right(&self, _library: Arc<crate::library::Library>) -> String {
fn display_right(&self, _library: &Library) -> String {
"".to_string()
}
fn play(&mut self, _queue: Arc<crate::queue::Queue>) {}
fn play(&mut self, _queue: &Queue) {}
fn play_next(&mut self, _queue: Arc<crate::queue::Queue>) {}
fn play_next(&mut self, _queue: &Queue) {}
fn queue(&mut self, _queue: Arc<crate::queue::Queue>) {}
fn queue(&mut self, _queue: &Queue) {}
fn toggle_saved(&mut self, _library: Arc<crate::library::Library>) {}
fn toggle_saved(&mut self, _library: &Library) {}
fn save(&mut self, _library: Arc<crate::library::Library>) {}
fn save(&mut self, _library: &Library) {}
fn unsave(&mut self, _library: Arc<crate::library::Library>) {}
fn unsave(&mut self, _library: &Library) {}
fn open(
&self,

View File

@@ -67,39 +67,39 @@ impl fmt::Display for Episode {
}
impl ListItem for Episode {
fn is_playing(&self, queue: Arc<Queue>) -> bool {
fn is_playing(&self, queue: &Queue) -> bool {
let current = queue.get_current();
current
.map(|t| t.id() == Some(self.id.clone()))
.unwrap_or(false)
}
fn display_left(&self, _library: Arc<Library>) -> String {
fn display_left(&self, _library: &Library) -> String {
self.name.clone()
}
fn display_right(&self, _library: Arc<Library>) -> String {
fn display_right(&self, _library: &Library) -> String {
format!("{} [{}]", self.duration_str(), self.release_date)
}
fn play(&mut self, queue: Arc<Queue>) {
fn play(&mut self, queue: &Queue) {
let index = queue.append_next(&vec![Playable::Episode(self.clone())]);
queue.play(index, true, false);
}
fn play_next(&mut self, queue: Arc<Queue>) {
fn play_next(&mut self, queue: &Queue) {
queue.insert_after_current(Playable::Episode(self.clone()));
}
fn queue(&mut self, queue: Arc<Queue>) {
fn queue(&mut self, queue: &Queue) {
queue.append(Playable::Episode(self.clone()));
}
fn toggle_saved(&mut self, _library: Arc<Library>) {}
fn toggle_saved(&mut self, _library: &Library) {}
fn save(&mut self, _library: Arc<Library>) {}
fn save(&mut self, _library: &Library) {}
fn unsave(&mut self, _library: Arc<Library>) {}
fn unsave(&mut self, _library: &Library) {}
fn open(&self, _queue: Arc<Queue>, _library: Arc<Library>) -> Option<Box<dyn ViewExt>> {
None

View File

@@ -20,7 +20,7 @@ pub enum Playable {
}
impl Playable {
pub fn format(playable: &Playable, formatting: &str, library: Arc<Library>) -> String {
pub fn format(playable: &Playable, formatting: &str, library: &Library) -> String {
formatting
.replace(
"%artists",
@@ -162,43 +162,43 @@ impl fmt::Display for Playable {
}
impl ListItem for Playable {
fn is_playing(&self, queue: Arc<Queue>) -> bool {
fn is_playing(&self, queue: &Queue) -> bool {
self.as_listitem().is_playing(queue)
}
fn display_left(&self, library: Arc<Library>) -> String {
fn display_left(&self, library: &Library) -> String {
self.as_listitem().display_left(library)
}
fn display_center(&self, library: Arc<Library>) -> String {
fn display_center(&self, library: &Library) -> String {
self.as_listitem().display_center(library)
}
fn display_right(&self, library: Arc<Library>) -> String {
fn display_right(&self, library: &Library) -> String {
self.as_listitem().display_right(library)
}
fn play(&mut self, queue: Arc<Queue>) {
fn play(&mut self, queue: &Queue) {
self.as_listitem().play(queue)
}
fn play_next(&mut self, queue: Arc<Queue>) {
fn play_next(&mut self, queue: &Queue) {
self.as_listitem().play_next(queue)
}
fn queue(&mut self, queue: Arc<Queue>) {
fn queue(&mut self, queue: &Queue) {
self.as_listitem().queue(queue)
}
fn toggle_saved(&mut self, library: Arc<Library>) {
fn toggle_saved(&mut self, library: &Library) {
self.as_listitem().toggle_saved(library)
}
fn save(&mut self, library: Arc<Library>) {
fn save(&mut self, library: &Library) {
self.as_listitem().save(library)
}
fn unsave(&mut self, library: Arc<Library>) {
fn unsave(&mut self, library: &Library) {
self.as_listitem().unsave(library)
}
@@ -210,7 +210,7 @@ impl ListItem for Playable {
self.as_listitem().share_url()
}
fn album(&self, queue: Arc<Queue>) -> Option<Album> {
fn album(&self, queue: &Queue) -> Option<Album> {
self.as_listitem().album(queue)
}

View File

@@ -55,7 +55,7 @@ impl Playlist {
})
}
pub fn delete_track(&mut self, index: usize, spotify: Spotify, library: Arc<Library>) -> bool {
pub fn delete_track(&mut self, index: usize, spotify: Spotify, library: &Library) -> bool {
let track = self.tracks.as_ref().unwrap()[index].clone();
debug!("deleting track: {} {:?}", index, track);
match spotify
@@ -74,12 +74,7 @@ impl Playlist {
}
}
pub fn append_tracks(
&mut self,
new_tracks: &[Playable],
spotify: Spotify,
library: Arc<Library>,
) {
pub fn append_tracks(&mut self, new_tracks: &[Playable], spotify: &Spotify, library: &Library) {
let mut has_modified = false;
if spotify.api.append_tracks(&self.id, new_tracks, None) {
@@ -175,7 +170,7 @@ impl From<&FullPlaylist> for Playlist {
}
impl ListItem for Playlist {
fn is_playing(&self, queue: Arc<Queue>) -> bool {
fn is_playing(&self, queue: &Queue) -> bool {
if let Some(tracks) = self.tracks.as_ref() {
let playing: Vec<String> = queue
.queue
@@ -191,7 +186,7 @@ impl ListItem for Playlist {
}
}
fn display_left(&self, library: Arc<Library>) -> String {
fn display_left(&self, library: &Library) -> String {
let hide_owners = library.cfg.values().hide_display_names.unwrap_or(false);
match (self.owner_name.as_ref(), hide_owners) {
(Some(owner), false) => format!("{}{}", self.name, owner),
@@ -199,7 +194,7 @@ impl ListItem for Playlist {
}
}
fn display_right(&self, library: Arc<Library>) -> String {
fn display_right(&self, library: &Library) -> String {
let saved = if library.is_saved_playlist(self) {
if library.cfg.values().use_nerdfont.unwrap_or(false) {
"\u{f62b} "
@@ -219,7 +214,7 @@ impl ListItem for Playlist {
format!("{saved}{num_tracks:>4} tracks")
}
fn play(&mut self, queue: Arc<Queue>) {
fn play(&mut self, queue: &Queue) {
self.load_tracks(queue.get_spotify());
if let Some(tracks) = &self.tracks {
@@ -228,7 +223,7 @@ impl ListItem for Playlist {
}
}
fn play_next(&mut self, queue: Arc<Queue>) {
fn play_next(&mut self, queue: &Queue) {
self.load_tracks(queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
@@ -238,7 +233,7 @@ impl ListItem for Playlist {
}
}
fn queue(&mut self, queue: Arc<Queue>) {
fn queue(&mut self, queue: &Queue) {
self.load_tracks(queue.get_spotify());
if let Some(tracks) = self.tracks.as_ref() {
@@ -248,7 +243,7 @@ impl ListItem for Playlist {
}
}
fn toggle_saved(&mut self, library: Arc<Library>) {
fn toggle_saved(&mut self, library: &Library) {
// Don't allow users to unsave their own playlists with one keypress
if !library.is_followed_playlist(self) {
return;
@@ -261,11 +256,11 @@ impl ListItem for Playlist {
}
}
fn save(&mut self, library: Arc<Library>) {
fn save(&mut self, library: &Library) {
library.follow_playlist(self);
}
fn unsave(&mut self, library: Arc<Library>) {
fn unsave(&mut self, library: &Library) {
library.delete_playlist(&self.id);
}
@@ -324,7 +319,7 @@ impl ListItem for Playlist {
))
}
fn is_saved(&self, library: Arc<Library>) -> Option<bool> {
fn is_saved(&self, library: &Library) -> Option<bool> {
// save status of personal playlists can't be toggled for safety
if !library.is_followed_playlist(self) {
return None;

View File

@@ -72,15 +72,15 @@ impl fmt::Display for Show {
}
impl ListItem for Show {
fn is_playing(&self, _queue: Arc<Queue>) -> bool {
fn is_playing(&self, _queue: &Queue) -> bool {
false
}
fn display_left(&self, _library: Arc<Library>) -> String {
fn display_left(&self, _library: &Library) -> String {
format!("{self}")
}
fn display_right(&self, library: Arc<Library>) -> String {
fn display_right(&self, library: &Library) -> String {
let saved = if library.is_saved_show(self) {
if library.cfg.values().use_nerdfont.unwrap_or(false) {
"\u{f62b} "
@@ -93,7 +93,7 @@ impl ListItem for Show {
saved.to_owned()
}
fn play(&mut self, queue: Arc<Queue>) {
fn play(&mut self, queue: &Queue) {
self.load_all_episodes(queue.get_spotify());
let playables = self
@@ -108,7 +108,7 @@ impl ListItem for Show {
queue.play(index, true, true);
}
fn play_next(&mut self, queue: Arc<Queue>) {
fn play_next(&mut self, queue: &Queue) {
self.load_all_episodes(queue.get_spotify());
if let Some(episodes) = self.episodes.as_ref() {
@@ -118,7 +118,7 @@ impl ListItem for Show {
}
}
fn queue(&mut self, queue: Arc<Queue>) {
fn queue(&mut self, queue: &Queue) {
self.load_all_episodes(queue.get_spotify());
for ep in self.episodes.as_ref().unwrap_or(&Vec::new()) {
@@ -126,7 +126,7 @@ impl ListItem for Show {
}
}
fn toggle_saved(&mut self, library: Arc<Library>) {
fn toggle_saved(&mut self, library: &Library) {
if library.is_saved_show(self) {
self.unsave(library);
} else {
@@ -134,11 +134,11 @@ impl ListItem for Show {
}
}
fn save(&mut self, library: Arc<Library>) {
fn save(&mut self, library: &Library) {
library.save_show(self);
}
fn unsave(&mut self, library: Arc<Library>) {
fn unsave(&mut self, library: &Library) {
library.unsave_show(self);
}
@@ -151,7 +151,7 @@ impl ListItem for Show {
}
#[inline]
fn is_saved(&self, library: Arc<Library>) -> Option<bool> {
fn is_saved(&self, library: &Library) -> Option<bool> {
Some(library.is_saved_show(self))
}

View File

@@ -176,12 +176,12 @@ impl fmt::Debug for Track {
}
impl ListItem for Track {
fn is_playing(&self, queue: Arc<Queue>) -> bool {
fn is_playing(&self, queue: &Queue) -> bool {
let current = queue.get_current();
current.map(|t| t.id() == self.id).unwrap_or(false)
}
fn display_left(&self, library: Arc<Library>) -> String {
fn display_left(&self, library: &Library) -> String {
let formatting = library
.cfg
.values()
@@ -197,7 +197,7 @@ impl ListItem for Track {
}
}
fn display_center(&self, library: Arc<Library>) -> String {
fn display_center(&self, library: &Library) -> String {
let formatting = library
.cfg
.values()
@@ -213,7 +213,7 @@ impl ListItem for Track {
}
}
fn display_right(&self, library: Arc<Library>) -> String {
fn display_right(&self, library: &Library) -> String {
let formatting = library
.cfg
.values()
@@ -238,20 +238,20 @@ impl ListItem for Track {
}
}
fn play(&mut self, queue: Arc<Queue>) {
fn play(&mut self, queue: &Queue) {
let index = queue.append_next(&vec![Playable::Track(self.clone())]);
queue.play(index, true, false);
}
fn play_next(&mut self, queue: Arc<Queue>) {
fn play_next(&mut self, queue: &Queue) {
queue.insert_after_current(Playable::Track(self.clone()));
}
fn queue(&mut self, queue: Arc<Queue>) {
fn queue(&mut self, queue: &Queue) {
queue.append(Playable::Track(self.clone()));
}
fn toggle_saved(&mut self, library: Arc<Library>) {
fn toggle_saved(&mut self, library: &Library) {
if library.is_saved_track(&Playable::Track(self.clone())) {
library.unsave_tracks(vec![self], true);
} else {
@@ -259,11 +259,11 @@ impl ListItem for Track {
}
}
fn save(&mut self, library: Arc<Library>) {
fn save(&mut self, library: &Library) {
library.save_tracks(vec![self], true);
}
fn unsave(&mut self, library: Arc<Library>) {
fn unsave(&mut self, library: &Library) {
library.unsave_tracks(vec![self], true);
}
@@ -309,7 +309,7 @@ impl ListItem for Track {
.map(|id| format!("https://open.spotify.com/track/{id}"))
}
fn album(&self, queue: Arc<Queue>) -> Option<Album> {
fn album(&self, queue: &Queue) -> Option<Album> {
let spotify = queue.get_spotify();
match self.album_id {
@@ -333,7 +333,7 @@ impl ListItem for Track {
}
#[inline]
fn is_saved(&self, library: Arc<Library>) -> Option<bool> {
fn is_saved(&self, library: &Library) -> Option<bool> {
Some(library.is_saved_track(&Playable::Track(self.clone())))
}