dear_imgui_wgpu/texture/result.rs
1use super::*;
2
3/// Result of a texture update operation
4///
5/// This enum represents the outcome of a texture update operation and
6/// contains any state changes that need to be applied to the texture data.
7/// This follows Rust's principle of explicit state management.
8#[derive(Debug, Clone)]
9pub enum TextureUpdateResult {
10 /// Texture was successfully created
11 Created { texture_id: TextureId },
12 /// Texture was successfully updated
13 Updated,
14 /// Texture was destroyed
15 Destroyed,
16 /// Texture update failed
17 Failed,
18 /// No action was needed
19 NoAction,
20}
21
22impl TextureUpdateResult {
23 /// Apply the result to a texture data object
24 ///
25 /// This method updates the texture data's status and ID based on the operation result.
26 /// This is the Rust-idiomatic way to handle state updates.
27 pub fn apply_to(self, texture_data: &mut TextureData) {
28 match self {
29 TextureUpdateResult::Created { texture_id } => {
30 texture_data.set_tex_id(texture_id);
31 texture_data.set_status(TextureStatus::OK);
32 }
33 TextureUpdateResult::Updated => {
34 texture_data.set_status(TextureStatus::OK);
35 }
36 TextureUpdateResult::Destroyed => mark_texture_destroyed(texture_data),
37 TextureUpdateResult::Failed => {
38 texture_data.set_status(TextureStatus::Destroyed);
39 }
40 TextureUpdateResult::NoAction => {
41 // No changes needed
42 }
43 }
44 }
45}
46
47pub(super) fn mark_texture_destroyed(texture_data: &mut TextureData) {
48 unsafe {
49 // ImGui's SetStatus(Destroyed) has special semantics: if WantDestroyNextFrame is false,
50 // Destroyed may translate back to WantCreate. When honoring a requested destroy, set it
51 // before writing the final status.
52 (*texture_data.as_raw_mut()).WantDestroyNextFrame = true;
53 }
54 texture_data.set_status(TextureStatus::Destroyed);
55}