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
80
81
82
83
84
85
86
/* SPDX-License-Identifier: LGPL-2.1-or-later OR MPL-2.0

Copyright (c) 2024 DorotaC
*/

/*! Allow extending uniforms created using `glium::uniforms!`.
 */

use glium::uniforms::{AsUniformValue,EmptyUniforms, UniformsStorage, Uniforms};

#[macro_export]
macro_rules! uniform_extend {
    ($uniforms:expr, $($field:ident: $value:expr),+) => {
        {
            let uniforms = $uniforms;
            $(
                let uniforms = uniforms.add(stringify!($field), $value);
            )+
            uniforms
        }
    };
    ($uniforms:expr, $($field:ident: $value:expr),*,) => {
        $crate::uniform_extend!($uniforms, $($field: $value),*)
    };
}

pub trait ExtendUniforms<'n> {
    fn add<U: AsUniformValue>(self, name: &'n str, value: U)
    -> UniformsStorage<'n, U, impl Uniforms>;
}

impl<'n> ExtendUniforms<'n> for EmptyUniforms {
    fn add<U: AsUniformValue>(self, name: &'n str, value: U)
    -> UniformsStorage<'n, U, impl Uniforms> {
        UniformsStorage::new(name, value)
    }
}

impl<'n, T: AsUniformValue, U: Uniforms> ExtendUniforms<'n>
    for UniformsStorage<'n, T, U>
{
    fn add<V: AsUniformValue>(self, name: &'n str, value: V)
    -> UniformsStorage<'n, V, impl Uniforms> {
        UniformsStorage::<'n, _, _>::add(self, name, value)
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use glium::uniform;
    #[test]
    fn extend_empty() {
        let u = uniform! {};
        uniform_extend!(
            u,
            test: 1u32,
            another: 1.0f32,
        );
    }
    
    #[test]
    fn extend_one() {
        let u = uniform! {
            test: 1u32,
        };
        uniform_extend!(
            u,
            test2: 1u32,
            another: 1.0f32,
        );
    }
    
    #[test]
    fn extend_many() {
        let u = uniform! {
            test: 1u32,
            aaa: -1i32,
        };
        uniform_extend!(
            u,
            test2: 1u32,
            another: 1.0f32,
        );
    }
}