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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Rhyoea. Vulkan FFI bindings for Rust
// Copyright © 2019 Adrien Jeser <adrien@jeser.me>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

//! TODO: Doc

use crate::extends::Extends;
use crate::extends::{Extension, Layer};
use crate::instance::bind::{VkApplicationInfo, VkInstanceCreateInfo};
use crate::instance::Result;
use std::ffi::CString;

/// Specifying parameters of a newly created instance
#[derive(Debug, Clone)]
pub(super) struct Information {
    /// Structure specifying parameters of a newly created instance
    pub(super) inner: VkInstanceCreateInfo,

    /// Extensions to enable, in the local state
    extensions_information: ExtendsInformation<Extension>,

    /// Layers to enable, in the local state
    layers_information: ExtendsInformation<Layer>,
}

impl Information {
    /// Create a new instance `Information`
    pub(super) fn new(
        application: &VkApplicationInfo,
        extensions: Vec<Extension>,
        layers: Vec<Layer>,
    ) -> Result<Self> {
        let extensions_information = ExtendsInformation::new(extensions)?;
        let layers_information = ExtendsInformation::new(layers)?;
        let inner =
            VkInstanceCreateInfo::new(application, &extensions_information, &layers_information);

        Ok(Self {
            inner,
            extensions_information,
            layers_information,
        })
    }
}

impl From<Information> for VkInstanceCreateInfo {
    fn from(information: Information) -> Self {
        information.inner
    }
}

/// The `ExtendsInformation` type. See [the module level documentation](index.html) for more
///
/// Specifying extensions parameters of a newly created instance
///
/// It wrap a Vulkan property to be safe
#[derive(Debug, Clone)]
pub struct ExtendsInformation<T: Extends> {
    extends: Vec<T>,
    extends_names: Vec<CString>,
    extends_names_ptr: Vec<*const i8>,
}

impl<T: Extends> ExtendsInformation<T> {
    /// Create a new `ExtendsInformation<T>`
    ///
    /// # Example
    ///
    /// ```
    /// use rhyoea::instance::information::ExtendsInformation;
    /// use rhyoea::extends::{Extends, Extensions};
    /// use rhyoea::result::InstanceError;
    ///
    /// pub fn main() -> Result<(), InstanceError> {
    ///     let extensions = Extensions::available();
    ///     let _ = ExtendsInformation::new(extensions);
    ///     Ok(())
    /// }
    /// ```
    pub fn new(extends: Vec<T>) -> Result<Self> {
        let extends_names: std::result::Result<Vec<_>, _> = extends
            .iter()
            .map(|property| CString::new(property.name().clone()))
            .collect();
        let extends_names = extends_names?;
        let extends_names_ptr: Vec<_> = extends_names.iter().map(|q| q.as_ptr()).collect();

        Ok(Self {
            extends,
            extends_names,
            extends_names_ptr,
        })
    }

    /// Returns a raw pointer to the underlying data
    ///
    ///
    /// # Safety
    ///
    /// TODO: Doc
    ///
    /// # Example
    ///
    /// ```
    /// use rhyoea::instance::information::ExtendsInformation;
    /// use rhyoea::extends::{Extends, Extensions};
    /// use rhyoea::result::InstanceError;
    ///
    /// let extensions = Extensions::available();
    /// let extensions_extends = ExtendsInformation::new(extensions)?;
    ///
    /// unsafe {
    ///   assert_ne!(std::ptr::null(), extensions_extends.as_ptr());
    /// }
    ///
    /// # Ok::<(), InstanceError>(())
    /// ```
    #[must_use]
    pub unsafe fn as_ptr(&self) -> *const *const i8 {
        self.extends_names_ptr.as_ptr()
    }

    /// Returns the number of elements in the property vector, also referred to as its 'length'
    ///
    /// # Example
    ///
    /// ```
    /// use rhyoea::instance::information::ExtendsInformation;
    /// use rhyoea::extends::{Extends, Extensions};
    /// use rhyoea::result::InstanceError;
    ///
    /// let extensions = Extensions::available();
    /// let extensions_extends = ExtendsInformation::new(extensions)?;
    /// assert!(extensions_extends.len() > 0);
    ///
    /// # Ok::<(), InstanceError>(())
    /// ```
    #[cfg_attr(feature = "cargo-clippy", allow(clippy::cast_possible_truncation))]
    #[must_use]
    pub fn len(&self) -> u32 {
        self.extends.len() as u32
    }

    /// Returns true if self has not `Extends`
    ///
    /// # Example
    /// ```
    /// use rhyoea::instance::information::ExtendsInformation;
    /// use rhyoea::extends::{Extends, Extensions};
    /// use rhyoea::result::InstanceError;
    ///
    /// let extensions = Extensions::available();
    /// let extensions_extends = ExtendsInformation::new(extensions)?;
    /// assert!(!extensions_extends.is_empty());
    ///
    /// # Ok::<(), InstanceError>(())
    /// ```
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}