Starting support for tags
This commit is contained in:
101
oxydsp-flowgraph/src/tag.rs
Normal file
101
oxydsp-flowgraph/src/tag.rs
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user