photonic_output_null/
lib.rs

1use anyhow::Result;
2use std::marker::PhantomData;
3
4use photonic::{BufferReader, Output, OutputDecl};
5
6#[derive(Default)]
7pub struct Null<E> {
8    size: usize,
9    phantom: PhantomData<E>,
10}
11
12pub struct NullOutput<E> {
13    size: usize,
14    phantom: PhantomData<E>,
15}
16
17impl<E> Null<E> {
18    pub fn with_size(size: usize) -> Self {
19        return Self {
20            size,
21            phantom: PhantomData,
22        };
23    }
24}
25
26impl<E> OutputDecl for Null<E> {
27    const KIND: &'static str = "null";
28    type Output = NullOutput<E>;
29
30    async fn materialize(self) -> Result<Self::Output>
31    where Self::Output: Sized {
32        return Ok(Self::Output {
33            size: self.size,
34            phantom: self.phantom,
35        });
36    }
37}
38
39impl<E> Output for NullOutput<E> {
40    const KIND: &'static str = "null";
41
42    type Element = E;
43
44    async fn render(&mut self, _: impl BufferReader<Element = Self::Element>) -> Result<()> {
45        return Ok(());
46    }
47
48    fn size(&self) -> usize {
49        return self.size;
50    }
51}
52
53#[cfg(feature = "dynamic")]
54pub mod dynamic {
55    use anyhow::Result;
56    use palette::rgb::Rgb;
57    use serde::Deserialize;
58
59    use photonic::boxed::DynOutputDecl;
60    use photonic_dynamic::factory::{factory, OutputFactory, Producible};
61    use photonic_dynamic::{builder, registry};
62
63    use crate::Null;
64
65    #[derive(Deserialize)]
66    pub struct Config {
67        size: usize,
68    }
69
70    impl Producible<dyn DynOutputDecl> for Config {
71        type Product = Null<Rgb>;
72
73        fn produce<Reg: registry::Registry>(
74            config: Self,
75            _builder: builder::OutputBuilder<'_, Reg>,
76        ) -> Result<Self::Product> {
77            return Ok(Null::with_size(config.size));
78        }
79    }
80
81    pub struct Registry;
82
83    impl registry::Registry for Registry {
84        fn output<Reg: registry::Registry>(kind: &str) -> Option<OutputFactory<Reg>> {
85            return match kind {
86                "null" => Some(factory::<Config>()),
87                _ => return None,
88            };
89        }
90    }
91}