Switching things to tagged type

This commit is contained in:
2026-03-20 20:22:36 +01:00
parent 822cdc1587
commit ac5c9eeaa0
6 changed files with 254 additions and 154 deletions

View File

@ -77,35 +77,44 @@ impl TagMergable<Option<Tag>> for Option<Tag>
}
}
impl<T: Clone> TagMergable<Option<Tag>> for Tagged<T>
{
fn merge(&self, other: &Option<Tag>) -> Self
{
Tagged::new(self.0.clone(), self.1.merge(other))
}
}
/// Represents a data, with a potential tag attached to it.
#[derive(Clone)]
pub struct Tagged<T>
{
inner: T,
tag: Option<Tag>,
}
pub struct Tagged<T>(pub T, pub Option<Tag>);
impl<T> Tagged<T>
{
pub fn new(inner: T, tag: Option<Tag>) -> Self
{
Self { inner, tag }
Self(inner, tag)
}
pub fn has_tag(&self) -> bool
{
self.tag.is_some()
self.1.is_some()
}
pub fn strip(&mut self)
{
self.tag = None;
self.1 = None;
}
pub fn into_inner(self) -> T
{
self.0
}
pub fn tag(&mut self, tag: Tag) -> Option<Tag>
{
let t = self.tag.take();
self.tag = Some(tag);
let t = self.1.take();
self.1 = Some(tag);
t
}
}
@ -114,12 +123,12 @@ impl<T: Clone> Tagged<T>
{
pub fn stripped(&self) -> Self
{
self.inner.clone().into()
self.0.clone().into()
}
pub fn tagged(&self, tag: Tag) -> Self
{
(self.inner.clone(), tag).into()
(self.0.clone(), tag).into()
}
}
@ -139,11 +148,27 @@ impl<T> From<(T, Tag)> for Tagged<T>
}
}
impl<T> From<(T, Option<Tag>)> for Tagged<T>
{
fn from((value, tag): (T, Option<Tag>)) -> Self
{
Self::new(value, tag)
}
}
impl<T> Into<(T, Option<Tag>)> for Tagged<T>
{
fn into(self) -> (T, Option<Tag>)
{
(self.0, self.1)
}
}
impl<T> DerefMut for Tagged<T>
{
fn deref_mut(&mut self) -> &mut Self::Target
{
&mut self.inner
&mut self.0
}
}
@ -153,6 +178,6 @@ impl<T> Deref for Tagged<T>
fn deref(&self) -> &Self::Target
{
&self.inner
&self.0
}
}