From 8db6a2352b1f1c11ff06b58fe8c999b202b5e59c Mon Sep 17 00:00:00 2001 From: Antoine Gersant Date: Mon, 29 Jul 2024 20:03:25 -0700 Subject: [PATCH] Adds ID trait --- src/app/collection/index.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/app/collection/index.rs b/src/app/collection/index.rs index b9c3616..d9f8416 100644 --- a/src/app/collection/index.rs +++ b/src/app/collection/index.rs @@ -164,9 +164,7 @@ impl From<&collection::Song> for SongKey { impl From<&SongKey> for SongID { fn from(key: &SongKey) -> Self { - let mut hasher = DefaultHasher::default(); - key.hash(&mut hasher); - SongID(hasher.finish()) + SongID(key.id()) } } @@ -212,9 +210,7 @@ impl From<&collection::Song> for AlbumKey { impl From<&AlbumKey> for AlbumID { fn from(key: &AlbumKey) -> Self { - let mut hasher = DefaultHasher::default(); - key.hash(&mut hasher); - AlbumID(hasher.finish()) + AlbumID(key.id()) } } @@ -225,6 +221,18 @@ impl From<&collection::Song> for AlbumID { } } +trait ID { + fn id(&self) -> u64; +} + +impl ID for T { + fn id(&self) -> u64 { + let mut hasher = DefaultHasher::default(); + self.hash(&mut hasher); + hasher.finish() + } +} + #[cfg(test)] mod test {