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
// Copyright (c) 2021 The vulkano developers
// Licensed under the Apache License, Version 2.0
// <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>,
// at your option. All files in the project carrying such
// notice may not be copied, modified, or distributed except
// according to those terms.

use crate::buffer::BufferAccess;
use std::sync::Arc;

/// A collection of vertex buffers.
pub unsafe trait VertexBuffersCollection {
    /// Converts `self` into a list of buffers.
    // TODO: better than a Vec
    fn into_vec(self) -> Vec<Arc<dyn BufferAccess>>;
}

unsafe impl VertexBuffersCollection for () {
    #[inline]
    fn into_vec(self) -> Vec<Arc<dyn BufferAccess>> {
        vec![]
    }
}

unsafe impl<T> VertexBuffersCollection for Arc<T>
where
    T: BufferAccess + 'static,
{
    #[inline]
    fn into_vec(self) -> Vec<Arc<dyn BufferAccess>> {
        vec![self as Arc<_>]
    }
}

unsafe impl<T> VertexBuffersCollection for Vec<Arc<T>>
where
    T: BufferAccess + 'static,
{
    #[inline]
    fn into_vec(self) -> Vec<Arc<dyn BufferAccess>> {
        self.into_iter().map(|source| source as Arc<_>).collect()
    }
}

macro_rules! impl_collection {
    ($first:ident $(, $others:ident)+) => (
        unsafe impl<$first$(, $others)+> VertexBuffersCollection for (Arc<$first>, $(Arc<$others>),+)
            where $first: BufferAccess + 'static
                  $(, $others: BufferAccess + 'static)*
        {
            #[inline]
            fn into_vec(self) -> Vec<Arc<dyn BufferAccess>> {
                #![allow(non_snake_case)]

                let ($first, $($others,)*) = self;

                let mut list = Vec::new();
                list.push($first as Arc<_>);
                $(
                    list.push($others as Arc<_>);
                )+
                list
            }
        }

        impl_collection!($($others),+);
    );

    ($i:ident) => ();
}

impl_collection!(Z, Y, X, W, V, U, T, S, R, Q, P, O, N, M, L, K, J, I, H, G, F, E, D, C, B, A);