#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Identified<Id, D> {
id: Id,
data: D,
}
impl<Id, D> Identified<Id, D> {
#[inline]
#[must_use]
pub const fn new(id: Id, data: D) -> Self {
Self { id, data }
}
#[inline]
pub const fn id_ref(&self) -> &Id {
&self.id
}
#[inline]
pub const fn data_ref(&self) -> &D {
&self.data
}
#[inline]
pub fn into_components(self) -> (Id, D) {
(self.id, self.data)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_and_accessors() {
let v = Identified::new(7u32, "hello");
assert_eq!(v.id_ref(), &7u32);
assert_eq!(v.data_ref(), &"hello");
}
#[test]
fn into_components_returns_pair() {
let v = Identified::new(42u64, [1u8, 2, 3]);
let (id, data) = v.into_components();
assert_eq!(id, 42u64);
assert_eq!(data, [1u8, 2, 3]);
}
#[test]
fn clone_when_both_clone() {
let v = Identified::new(1u8, 2u8);
#[allow(clippy::clone_on_copy)]
let w = v.clone();
assert_eq!(v, w);
}
#[test]
fn copy_when_both_copy() {
let v = Identified::new(1u8, 2u16);
fn take<T: Copy>(_: T) {}
take(v);
assert_eq!(v.id_ref(), &1u8);
assert_eq!(v.data_ref(), &2u16);
}
#[cfg(any(feature = "std", feature = "alloc"))]
#[test]
fn debug_format_smoke() {
use std::format;
let s = format!("{:?}", Identified::new(1u8, 2u8));
assert!(s.contains("id"));
assert!(s.contains("data"));
}
}