use std::cmp::{Eq, PartialEq};
use std::fmt;
use std::hash::{Hash, Hasher};
use ustr::Ustr;
#[derive(Debug, Clone, Copy)]
pub struct Item {
id: i64,
name: Ustr,
game: Ustr,
}
impl Item {
pub(crate) fn new(id: i64, name: Ustr, game: Ustr) -> Item {
Item { id, name, game }
}
pub fn id(&self) -> i64 {
self.id
}
pub fn name(&self) -> Ustr {
self.name
}
pub fn game(&self) -> Ustr {
self.game
}
}
impl fmt::Display for Item {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.name.fmt(f)
}
}
impl PartialEq for Item {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl Eq for Item {}
impl Hash for Item {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
pub trait AsItemId {
fn as_item_id(&self) -> i64;
fn same_item(&self, other: impl AsItemId) -> bool {
self.as_item_id() == other.as_item_id()
}
}
impl AsItemId for Item {
fn as_item_id(&self) -> i64 {
self.id
}
}
impl AsItemId for i64 {
fn as_item_id(&self) -> i64 {
*self
}
}