Skip to main content

id_effect/context/
tag.rs

1//! [`Tag`] — zero-sized type-level key (`Stratum 3.1`).
2
3use core::fmt;
4use core::hash::{Hash, Hasher};
5use core::marker::PhantomData;
6
7/// Pure phantom service identity (`K` is usually a ZST marker type).
8#[derive(Clone, Copy)]
9pub struct Tag<K: ?Sized>(PhantomData<fn() -> *const K>);
10
11impl<K: ?Sized> fmt::Debug for Tag<K> {
12  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13    f.write_str("Tag")
14  }
15}
16
17impl<K: ?Sized> Tag<K> {
18  /// Canonical [`Tag`] value for the key type `K`.
19  pub const fn new() -> Self {
20    Self(PhantomData)
21  }
22}
23
24impl<K: ?Sized> Default for Tag<K> {
25  fn default() -> Self {
26    Self::new()
27  }
28}
29
30impl<K: ?Sized> PartialEq for Tag<K> {
31  fn eq(&self, _other: &Self) -> bool {
32    true
33  }
34}
35
36impl<K: ?Sized> Eq for Tag<K> {}
37
38impl<K: ?Sized> Hash for Tag<K> {
39  fn hash<H: Hasher>(&self, state: &mut H) {
40    state.write_u8(0);
41  }
42}