Adds ID trait

This commit is contained in:
Antoine Gersant 2024-07-29 20:03:25 -07:00
parent 93e8d7d94b
commit 8db6a2352b

View file

@ -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<T: Hash> ID for T {
fn id(&self) -> u64 {
let mut hasher = DefaultHasher::default();
self.hash(&mut hasher);
hasher.finish()
}
}
#[cfg(test)]
mod test {