The library provides enum wrappers to treat different kinds of IDs generically when an endpoint accepts multiple types.
PlayableId<'a>
Used for items that can be played. It wraps:
LibraryId<'a>
A comprehensive wrapper for all major library items. It wraps:
TrackId<'a>, AlbumId<'a>, EpisodeId<'a>, ShowId<'a>, ArtistId<'a>, UserId<'a>, and PlaylistId<'a>.
PlayContextId<'a>
Used for context-based playback. It wraps:
ArtistId<'a>, AlbumId<'a>, PlaylistId<'a>, and ShowId<'a>.
All these enums implement the Id trait and provide .as_ref(), .into_static(), and .clone_static() methods to manage lifetimes.
use rspotify_model::{TrackId, EpisodeId, PlayableId};
fn add_to_queue(id: &[PlayableId]) { /* ... */ }
let tracks = [
PlayableId::Track(TrackId::from_id("track_id_1").unwrap()),
PlayableId::Track(TrackId::from_id("track_id_2").unwrap()),
];
let episodes = [
PlayableId::Episode(EpisodeId::from_id("ep_id_1").unwrap()),
];
// Combine different types into a single vector of PlayableId
let playable: Vec<PlayableId> = tracks.into_iter().chain(episodes.into_iter()).collect();
add_to_queue(&playable);