ash/examples
Marijn Suijten f781777931 examples: Use slice::from_ref to not loose lifetime on nested slices
As per the readme `.build()` should only be called as late as possible,
and only if absolutely necessary; such cases include slices that are
passed directly to functions.  More precisely, such build calls and the
creation of temporary slices should happen inside the same expression as
the function call to be sound and completely lifetime-checked.

This pattern of `&[my_builder.build()]` is however not possible when
constructing intermediary Vulkan objects that reference the slice.  In
the first place this slice goes out of scope after the expression that
creates the Vulkan object, which is caught and disallowed by rustc
(unless this expression itself ends in `.build()`, which is completely
 unsound as it makes rustc unable to validate this lifetime dependency).

In the second place - and as is most relevant for this patch that
removes `.build()` calls that were not surrounded by temporary slice
constructors - said expression drops the lifetime checks on anything
held by `my_builder` which _could_ go out of scope before the newly
constructed Vulkan object is used, resulting yet again in Undefined
Behaviour.

Fortunately, for slices of size 1 which are typical in Vulkan,
`std::slice::as_ref` exists which is analogous to taking a pointer to an
object and considering it an array of length 1 in C(++).  This maintains
the lifetime through `Deref` and makes rustc able to fully check all
lifetimes and prevent unsound code.

Albeit improving overall consistency, the `&[my_builder.build()]`
pattern is not substituted in aforementioned Vulkan function-call
expressions as that is considered "extraneous" [1] and demonstrates the
various ways to safely construct Vulkan objects for the observant reader.

[1]: https://github.com/MaikKlein/ash/pull/506#discussion_r762630648
2021-12-19 16:17:56 -08:00
..
assets Update the texture example 2016-12-26 01:22:21 +01:00
shader Fix broken vert.spv 2017-09-17 10:31:17 +02:00
src examples: Use slice::from_ref to not loose lifetime on nested slices 2021-12-19 16:17:56 -08:00
Cargo.toml Update example to use ash-window 2020-09-04 14:05:16 -07:00