1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use locspan::Meta;
use locspan_derive::{StrippedEq, StrippedHash, StrippedPartialEq};
use std::ops::Deref;

#[derive(
	Clone,
	Copy,
	PartialEq,
	StrippedPartialEq,
	StrippedEq,
	StrippedHash,
	Eq,
	PartialOrd,
	Ord,
	Hash,
	Debug,
)]
#[locspan(ignore(M))]
pub struct Entry<T, M> {
	#[locspan(ignore)]
	pub key_metadata: M,
	pub value: Meta<T, M>,
}

impl<T, M> Entry<T, M> {
	pub fn new(key_metadata: M, value: Meta<T, M>) -> Self {
		Self {
			key_metadata,
			value,
		}
	}

	pub fn as_value(&self) -> &Meta<T, M> {
		&self.value
	}

	pub fn into_value(self) -> Meta<T, M> {
		self.value
	}

	pub fn borrow_value(&self) -> Entry<&T, M>
	where
		M: Clone,
	{
		Entry::new(self.key_metadata.clone(), self.value.borrow_value())
	}

	pub fn map<U>(self, f: impl Fn(T) -> U) -> Entry<U, M> {
		Entry::new(self.key_metadata, self.value.map(f))
	}

	pub fn cast<U: From<T>>(self) -> Entry<U, M> {
		Entry::new(self.key_metadata, self.value.cast())
	}
}

impl<'a, T, M> Entry<&'a T, M> {
	pub fn into_deref(self) -> Entry<&'a T::Target, M>
	where
		T: Deref,
	{
		let Meta(value, meta) = self.value;
		Entry::new(self.key_metadata, Meta(value.deref(), meta))
	}
}

impl<T, M> std::ops::Deref for Entry<T, M> {
	type Target = Meta<T, M>;

	fn deref(&self) -> &Self::Target {
		&self.value
	}
}

impl<T, M> std::ops::DerefMut for Entry<T, M> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		&mut self.value
	}
}