From ed14c3469b0010965cbb2e5ec4685d036f0a8ca6 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Wed, 4 May 2016 10:03:46 +0100 Subject: [PATCH] track: Use a linear map to store files by format. --- Cargo.lock | 6 ++++++ Cargo.toml | 1 + src/lib.rs | 1 + src/metadata.rs | 5 +++-- src/player.rs | 4 ++-- 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 190b12bc..b175bf20 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,7 @@ dependencies = [ "lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "libpulse-sys 0.0.0 (git+https://github.com/astro/libpulse-sys)", "librespot-protocol 0.1.0", + "linear-map 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "lmdb-rs 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", @@ -275,6 +276,11 @@ dependencies = [ "pnacl-build-helper 1.4.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "linear-map" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "lmdb-rs" version = "0.7.0" diff --git a/Cargo.toml b/Cargo.toml index 3cb40904..3f26cf84 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ getopts = "~0.2.14" hyper = { version = "0.9.1", default-features = false } #json_macros = "~0.3.0" lazy_static = "~0.2.0" +linear-map = "1.0" lmdb-rs = "0.7.0" num = "~0.1.30" protobuf = "~1.0.15" diff --git a/src/lib.rs b/src/lib.rs index fcaf8327..fcb4b408 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ extern crate crypto; extern crate eventual; extern crate getopts; extern crate hyper; +extern crate linear_map; extern crate lmdb_rs; extern crate num; extern crate protobuf; diff --git a/src/metadata.rs b/src/metadata.rs index 0e5111db..6ab1386f 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -1,4 +1,5 @@ use eventual::{Async, Future}; +use linear_map::LinearMap; use protobuf; use protocol; @@ -37,7 +38,7 @@ pub struct Track { pub name: String, pub album: SpotifyId, pub artists: Vec, - pub files: Vec<(FileId, FileFormat)>, + pub files: LinearMap, pub alternatives: Vec, pub available: bool, } @@ -85,7 +86,7 @@ impl MetadataTrait for Track { .map(|file| { let mut dst = [0u8; 20]; dst.clone_from_slice(&file.get_file_id()); - (FileId(dst), file.get_format()) + (file.get_format(), FileId(dst)) }) .collect(); diff --git a/src/player.rs b/src/player.rs index 432a81ba..72e391fe 100644 --- a/src/player.rs +++ b/src/player.rs @@ -196,8 +196,8 @@ fn load_track(session: &Session, track_id: SpotifyId) -> Option file_id, + let file_id = match track.files.get(&format) { + Some(&file_id) => file_id, None => { warn!("Track \"{}\" is not available in format {:?}", track.name, format); return None;