ash/
prelude.rs

1use std::convert::TryInto;
2#[cfg(feature = "debug")]
3use std::fmt;
4
5use crate::vk;
6pub type VkResult<T> = Result<T, vk::Result>;
7
8impl vk::Result {
9    pub fn result(self) -> VkResult<()> {
10        self.result_with_success(())
11    }
12
13    pub fn result_with_success<T>(self, v: T) -> VkResult<T> {
14        match self {
15            Self::SUCCESS => Ok(v),
16            _ => Err(self),
17        }
18    }
19}
20
21/// Repeatedly calls `f` until it does not return [`vk::Result::INCOMPLETE`] anymore,
22/// ensuring all available data has been read into the vector.
23///
24/// See for example [`vkEnumerateInstanceExtensionProperties`]: the number of available
25/// items may change between calls; [`vk::Result::INCOMPLETE`] is returned when the count
26/// increased (and the vector is not large enough after querying the initial size),
27/// requiring Ash to try again.
28///
29/// [`vkEnumerateInstanceExtensionProperties`]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkEnumerateInstanceExtensionProperties.html
30pub(crate) unsafe fn read_into_uninitialized_vector<N: Copy + Default + TryInto<usize>, T>(
31    f: impl Fn(&mut N, *mut T) -> vk::Result,
32) -> VkResult<Vec<T>>
33where
34    <N as TryInto<usize>>::Error: std::fmt::Debug,
35{
36    loop {
37        let mut count = N::default();
38        f(&mut count, std::ptr::null_mut()).result()?;
39        let mut data =
40            Vec::with_capacity(count.try_into().expect("`N` failed to convert to `usize`"));
41
42        let err_code = f(&mut count, data.as_mut_ptr());
43        if err_code != vk::Result::INCOMPLETE {
44            data.set_len(count.try_into().expect("`N` failed to convert to `usize`"));
45            break err_code.result_with_success(data);
46        }
47    }
48}
49
50/// Repeatedly calls `f` until it does not return [`vk::Result::INCOMPLETE`] anymore,
51/// ensuring all available data has been read into the vector.
52///
53/// Items in the target vector are [`default()`][`Default::default()`]-initialized which
54/// is required for [`vk::BaseOutStructure`]-like structs where [`vk::BaseOutStructure::s_type`]
55/// needs to be a valid type and [`vk::BaseOutStructure::p_next`] a valid or
56/// [`null`][`std::ptr::null_mut()`] pointer.
57///
58/// See for example [`vkEnumerateInstanceExtensionProperties`]: the number of available
59/// items may change between calls; [`vk::Result::INCOMPLETE`] is returned when the count
60/// increased (and the vector is not large enough after querying the initial size),
61/// requiring Ash to try again.
62///
63/// [`vkEnumerateInstanceExtensionProperties`]: https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkEnumerateInstanceExtensionProperties.html
64pub(crate) unsafe fn read_into_defaulted_vector<
65    N: Copy + Default + TryInto<usize>,
66    T: Default + Clone,
67>(
68    f: impl Fn(&mut N, *mut T) -> vk::Result,
69) -> VkResult<Vec<T>>
70where
71    <N as TryInto<usize>>::Error: std::fmt::Debug,
72{
73    loop {
74        let mut count = N::default();
75        f(&mut count, std::ptr::null_mut()).result()?;
76        let mut data =
77            vec![Default::default(); count.try_into().expect("`N` failed to convert to `usize`")];
78
79        let err_code = f(&mut count, data.as_mut_ptr());
80        if err_code != vk::Result::INCOMPLETE {
81            data.set_len(count.try_into().expect("`N` failed to convert to `usize`"));
82            break err_code.result_with_success(data);
83        }
84    }
85}
86
87#[cfg(feature = "debug")]
88pub(crate) fn debug_flags<Value: Into<u64> + Copy>(
89    f: &mut fmt::Formatter,
90    known: &[(Value, &'static str)],
91    value: Value,
92) -> fmt::Result {
93    let mut first = true;
94    let mut accum = value.into();
95    for &(bit, name) in known {
96        let bit = bit.into();
97        if bit != 0 && accum & bit == bit {
98            if !first {
99                f.write_str(" | ")?;
100            }
101            f.write_str(name)?;
102            first = false;
103            accum &= !bit;
104        }
105    }
106    if accum != 0 {
107        if !first {
108            f.write_str(" | ")?;
109        }
110        write!(f, "{:b}", accum)?;
111    }
112    Ok(())
113}