Starting support for tags

This commit is contained in:
2026-03-18 16:38:23 +01:00
parent 3cdc0e613a
commit 4aef173c7c
5 changed files with 296 additions and 1 deletions

101
oxydsp-flowgraph/src/tag.rs Normal file
View File

@ -0,0 +1,101 @@
use std::{
any::Any,
collections::HashMap,
ops::{Deref, DerefMut},
sync::{Arc, Mutex},
};
// Tags a particular sample within a specific stream
#[derive(Clone)]
pub struct Tag
{
// Position of the sample this tag is tied to.
// The position is in terms of the stream front index when the
// sample was added
pub position: usize,
// TODO: Make it such that when a tag is duplicated, the data seems to be too:
// When adding on a duplicate, it should not replicate on others, but without
// requiring a deep copy.
pub data: Arc<Mutex<HashMap<String, Box<dyn Any>>>>,
}
/// Represents a data, with a potential tag attached to it.
#[derive(Clone)]
pub struct Tagged<T>
{
inner: T,
tag: Option<Tag>,
}
impl<T> Tagged<T>
{
pub fn new(inner: T, tag: Option<Tag>) -> Self
{
Self { inner, tag }
}
pub fn has_tag(&self) -> bool
{
self.tag.is_some()
}
pub fn strip(&mut self)
{
self.tag = None;
}
pub fn tag(&mut self, tag: Tag) -> Option<Tag>
{
let t = self.tag.take();
self.tag = Some(tag);
t
}
}
impl<T: Clone> Tagged<T>
{
pub fn stripped(&self) -> Self
{
self.inner.clone().into()
}
pub fn tagged(&self, tag: Tag) -> Self
{
(self.inner.clone(), tag).into()
}
}
impl<T> From<T> for Tagged<T>
{
fn from(value: T) -> Self
{
Self::new(value, None)
}
}
impl<T> From<(T, Tag)> for Tagged<T>
{
fn from((value, tag): (T, Tag)) -> Self
{
Self::new(value, Some(tag))
}
}
impl<T> DerefMut for Tagged<T>
{
fn deref_mut(&mut self) -> &mut Self::Target
{
&mut self.inner
}
}
impl<T> Deref for Tagged<T>
{
type Target = T;
fn deref(&self) -> &Self::Target
{
&self.inner
}
}