Merge pull request #1040 from dnlmlr/metadata-iterators-instead-of-vecs

Return iterators instead of collected Vecs
This commit is contained in:
Roderick van Domburg 2022-08-03 20:02:51 +02:00 committed by GitHub
commit b2c7b25835
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 26 deletions

View file

@ -36,7 +36,7 @@ async fn main() {
let plist = Playlist::get(&session, plist_uri).await.unwrap(); let plist = Playlist::get(&session, plist_uri).await.unwrap();
println!("{:?}", plist); println!("{:?}", plist);
for track_id in plist.tracks() { for track_id in plist.tracks() {
let plist_track = Track::get(&session, track_id).await.unwrap(); let plist_track = Track::get(&session, *track_id).await.unwrap();
println!("track: {} ", plist_track.name); println!("track: {} ", plist_track.name);
} }
} }

View file

@ -67,13 +67,8 @@ pub struct Discs(pub Vec<Disc>);
impl_deref_wrapped!(Discs, Vec<Disc>); impl_deref_wrapped!(Discs, Vec<Disc>);
impl Album { impl Album {
pub fn tracks(&self) -> Tracks { pub fn tracks(&self) -> impl Iterator<Item = &SpotifyId> {
let result = self self.discs.iter().flat_map(|disc| disc.tracks.iter())
.discs
.iter()
.flat_map(|disc| disc.tracks.deref().clone())
.collect();
Tracks(result)
} }
} }

View file

@ -140,14 +140,14 @@ impl Artist {
/// Get the full list of albums, not containing duplicate variants of the same albums. /// Get the full list of albums, not containing duplicate variants of the same albums.
/// ///
/// See also [`AlbumGroups`](struct@AlbumGroups) and [`AlbumGroups::current_releases`] /// See also [`AlbumGroups`](struct@AlbumGroups) and [`AlbumGroups::current_releases`]
pub fn albums_current(&self) -> Albums { pub fn albums_current(&self) -> impl Iterator<Item = &SpotifyId> {
self.albums.current_releases() self.albums.current_releases()
} }
/// Get the full list of singles, not containing duplicate variants of the same singles. /// Get the full list of singles, not containing duplicate variants of the same singles.
/// ///
/// See also [`AlbumGroups`](struct@AlbumGroups) and [`AlbumGroups::current_releases`] /// See also [`AlbumGroups`](struct@AlbumGroups) and [`AlbumGroups::current_releases`]
pub fn singles_current(&self) -> Albums { pub fn singles_current(&self) -> impl Iterator<Item = &SpotifyId> {
self.singles.current_releases() self.singles.current_releases()
} }
@ -155,14 +155,14 @@ impl Artist {
/// compilations. /// compilations.
/// ///
/// See also [`AlbumGroups`](struct@AlbumGroups) and [`AlbumGroups::current_releases`] /// See also [`AlbumGroups`](struct@AlbumGroups) and [`AlbumGroups::current_releases`]
pub fn compilations_current(&self) -> Albums { pub fn compilations_current(&self) -> impl Iterator<Item = &SpotifyId> {
self.compilations.current_releases() self.compilations.current_releases()
} }
/// Get the full list of albums, not containing duplicate variants of the same albums. /// Get the full list of albums, not containing duplicate variants of the same albums.
/// ///
/// See also [`AlbumGroups`](struct@AlbumGroups) and [`AlbumGroups::current_releases`] /// See also [`AlbumGroups`](struct@AlbumGroups) and [`AlbumGroups::current_releases`]
pub fn appears_on_albums_current(&self) -> Albums { pub fn appears_on_albums_current(&self) -> impl Iterator<Item = &SpotifyId> {
self.appears_on_albums.current_releases() self.appears_on_albums.current_releases()
} }
} }
@ -245,13 +245,8 @@ impl AlbumGroups {
/// Get the contained albums. This will only use the latest release / variant of an album if /// Get the contained albums. This will only use the latest release / variant of an album if
/// multiple variants are available. This should be used if multiple variants of the same album /// multiple variants are available. This should be used if multiple variants of the same album
/// are not explicitely desired. /// are not explicitely desired.
pub fn current_releases(&self) -> Albums { pub fn current_releases(&self) -> impl Iterator<Item = &SpotifyId> {
let albums = self self.iter().filter_map(|agrp| agrp.first())
.iter()
.filter_map(|agrp| agrp.first())
.cloned()
.collect();
Albums(albums)
} }
} }

View file

@ -105,13 +105,8 @@ impl Playlist {
Self::parse(&msg, playlist_id) Self::parse(&msg, playlist_id)
} }
pub fn tracks(&self) -> Vec<SpotifyId> { pub fn tracks(&self) -> impl ExactSizeIterator<Item = &SpotifyId> {
let tracks = self let tracks = self.contents.items.iter().map(|item| &item.id);
.contents
.items
.iter()
.map(|item| item.id)
.collect::<Vec<_>>();
let length = tracks.len(); let length = tracks.len();
let expected_length = self.length as usize; let expected_length = self.length as usize;