quickgpu/builders/
operations_builder.rs1pub use super::super::Nested;
5pub use std::{borrow::Cow, num::NonZeroU32, ops::Range};
6pub trait Field {}
7pub trait IsOptional {}
8#[doc = "\nBuilder for [`wgpu::Operations`]\n \nSet all required fields and any optional fields, then call `build()`.\n\nBuilder field setters:\n - [load](OperationsBuilder::load) Optional, defaults to [wgpu::LoadOp::Clear(Default::default())]\n - [store](OperationsBuilder::store) Optional, defaults to [wgpu::StoreOp::Store]\n"]
9pub struct OperationsBuilder<V: Default, CS: State<V>> {
10 load: CS::Load,
11 store: CS::Store,
12}
13impl<V: Default> OperationsBuilder<V, Empty> {
14 pub fn new() -> OperationsBuilder<V, Empty> {
15 OperationsBuilder {
16 load: LoadEmpty,
17 store: StoreEmpty,
18 }
19 }
20}
21#[doc = "\nReturns [OperationsBuilder] for building [`wgpu::Operations`]\n \nSet all required fields and any optional fields, then call `build()`.\n\nBuilder field setters:\n - [load](OperationsBuilder::load) Optional, defaults to [wgpu::LoadOp::Clear(Default::default())]\n - [store](OperationsBuilder::store) Optional, defaults to [wgpu::StoreOp::Store]\n"]
22pub fn operations<V: Default>() -> OperationsBuilder<V, Empty> {
23 OperationsBuilder::new()
24}
25pub struct LoadEmpty;
26impl Field for LoadEmpty {}
27pub trait LoadIsEmpty {}
28impl LoadIsEmpty for LoadEmpty {}
29pub trait IsSetLoad<V: Default> {
30 fn get(self) -> wgpu::LoadOp<V>;
31}
32impl<V: Default> IsSetLoad<V> for LoadEmpty {
33 fn get(self) -> wgpu::LoadOp<V> {
34 wgpu::LoadOp::Clear(Default::default())
35 }
36}
37pub struct LoadValue<V: Default>(pub wgpu::LoadOp<V>);
38impl<V: Default> Field for LoadValue<V> {}
39impl<V: Default> IsSetLoad<V> for LoadValue<V> {
40 fn get(self) -> wgpu::LoadOp<V> {
41 self.0
42 }
43}
44pub struct StoreEmpty;
45impl Field for StoreEmpty {}
46pub trait StoreIsEmpty {}
47impl StoreIsEmpty for StoreEmpty {}
48pub trait IsSetStore {
49 fn get(self) -> wgpu::StoreOp;
50}
51impl IsSetStore for StoreEmpty {
52 fn get(self) -> wgpu::StoreOp {
53 wgpu::StoreOp::Store
54 }
55}
56pub struct StoreValue(pub wgpu::StoreOp);
57impl Field for StoreValue {}
58impl IsSetStore for StoreValue {
59 fn get(self) -> wgpu::StoreOp {
60 self.0
61 }
62}
63pub trait State<V: Default> {
64 type Load: Field;
65 type Store: Field;
66}
67pub struct Empty;
68impl<V: Default> State<V> for Empty {
69 type Load = LoadEmpty;
70 type Store = StoreEmpty;
71}
72pub struct SetLoad<CS>(CS);
73impl<V: Default, CS: State<V>> State<V> for SetLoad<CS> {
74 type Load = LoadValue<V>;
75 type Store = CS::Store;
76}
77pub struct SetStore<CS>(CS);
78impl<V: Default, CS: State<V>> State<V> for SetStore<CS> {
79 type Load = CS::Load;
80 type Store = StoreValue;
81}
82impl<V: Default, CS: State<V>> OperationsBuilder<V, CS> {
83 #[doc = "Setter for [wgpu::Operations::load]. Optional, defaults to [wgpu::LoadOp::Clear(Default::default())].\n"]
84 pub fn load(self, load: wgpu::LoadOp<V>) -> OperationsBuilder<V, SetLoad<CS>>
85 where
86 CS::Load: LoadIsEmpty,
87 {
88 OperationsBuilder {
89 load: LoadValue(load),
90 store: self.store,
91 }
92 }
93 #[doc = "Setter for [wgpu::Operations::store]. Optional, defaults to [wgpu::StoreOp::Store].\n"]
94 pub fn store(self, store: wgpu::StoreOp) -> OperationsBuilder<V, SetStore<CS>>
95 where
96 CS::Store: StoreIsEmpty,
97 {
98 OperationsBuilder {
99 load: self.load,
100 store: StoreValue(store),
101 }
102 }
103}
104pub trait Complete<V: Default>: State<V, Load: IsSetLoad<V>, Store: IsSetStore> {}
105impl<V: Default, CS: State<V, Load: IsSetLoad<V>, Store: IsSetStore>> Complete<V> for CS {}
106impl<V: Default, CS: Complete<V>> OperationsBuilder<V, CS> {
107 pub fn build(self) -> wgpu::Operations<V> {
108 wgpu::Operations {
109 load: IsSetLoad::get(self.load),
110 store: IsSetStore::get(self.store),
111 }
112 }
113}
114impl<V: Default> Nested<wgpu::Operations<V>> for wgpu::Operations<V> {
115 fn unnest(self) -> wgpu::Operations<V> {
116 self
117 }
118}
119impl<V: Default, CS: Complete<V>> Nested<wgpu::Operations<V>> for OperationsBuilder<V, CS> {
120 fn unnest(self) -> wgpu::Operations<V> {
121 self.build()
122 }
123}
124#[cfg(test)]
125mod tests {
126 #[allow(unused_imports)]
127 #[allow(unused_imports)]
128 use std::num::NonZeroU32;
129 #[test]
130 pub fn test_default() {
131 assert_eq!(
132 format!("{:#?}", super::operations::<u32>().build()),
133 format!("{:#?}", wgpu::Operations::<u32>::default()),
134 );
135 }
136}