dear-imgui-rs 0.16.0

High-level Rust bindings to Dear ImGui v1.92.9b with docking, WGPU/GL backends, and extensions (ImPlot/ImPlot3D, ImNodes, ImGuizmo, file browser, reflection-based UI)
Documentation
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
use super::validation::{
    TextureLayout, non_negative_texture_count_from_i32, require_exact_payload,
    validate_native_texture_layout,
};
use super::{
    TextureDataError, TextureFormat, TextureId, TextureRect, TextureRegion, TextureStatus,
    TextureSubresource,
};
use crate::sys;
use std::cell::UnsafeCell;
use std::ffi::c_void;

/// Texture data managed by Dear ImGui
///
/// This is a wrapper around ImTextureData that provides safe access to
/// texture information and pixel data. Application code configures an owned value before
/// registration and mutates registered textures only through their owning Context.
///
/// Lifecycle & Backend Flow (ImGui 1.92+)
/// - Create an instance with `OwnedTextureData::from_pixels()`.
/// - Mutate pixels with `replace_pixels()` or `update_subresource()`.
/// - Transfer user-created owned textures via `Context::register_texture(tex)`.
/// - A renderer owns one synchronous or detached consumer and processes the pointer-free requests
///   exposed by `PendingFrame::texture_requests` or `FrameSnapshot::texture_requests`.
/// - The renderer returns request-bound `TextureFeedback`; the owning Context validates and
///   reconciles it before mutating native texture status or identifiers.
///
/// Context owns every registered user allocation through retirement. Application pixel mutation
/// normally uses `Context::try_with_texture_mut`, so safe code cannot drop the allocation while
/// native draw data or a renderer still refers to it. `Context::with_texture_mut` is the lower-level
/// result-composition API; callers must handle any inner pixel-mutation result themselves.
///
/// Construct owned values explicitly through [`super::OwnedTextureData::from_pixels`].
/// `TextureData` is a borrowed view and therefore has no constructor:
///
/// ```compile_fail
/// use dear_imgui_rs::texture::TextureData;
/// let _ = TextureData::new();
/// ```
///
/// The former metadata and storage mutators are not safe public operations:
///
/// ```compile_fail
/// use dear_imgui_rs::texture::TextureData;
/// fn resize(texture: &mut TextureData) { texture.set_width(2); }
/// ```
///
/// ```compile_fail
/// use dear_imgui_rs::texture::TextureData;
/// fn resize(texture: &mut TextureData) { texture.set_height(2); }
/// ```
///
/// ```compile_fail
/// use dear_imgui_rs::texture::{TextureData, TextureFormat};
/// fn reformat(texture: &mut TextureData) { texture.set_format(TextureFormat::Alpha8); }
/// ```
///
/// ```compile_fail
/// use dear_imgui_rs::texture::TextureData;
/// fn destroy_storage(texture: &mut TextureData) { texture.destroy_pixels(); }
/// ```
#[repr(transparent)]
pub struct TextureData {
    raw: UnsafeCell<sys::ImTextureData>,
}

// Ensure the wrapper stays layout-compatible with the sys bindings.
const _: [(); std::mem::size_of::<sys::ImTextureData>()] = [(); std::mem::size_of::<TextureData>()];
const _: [(); std::mem::align_of::<sys::ImTextureData>()] =
    [(); std::mem::align_of::<TextureData>()];

impl TextureData {
    #[inline]
    pub(super) fn inner(&self) -> &sys::ImTextureData {
        // Safety: `TextureData` is a view into an ImGui-owned `ImTextureData`. Dear ImGui and
        // renderer backends can mutate fields (e.g. Status/TexID/BackendUserData) while Rust holds
        // `&TextureData`, so we store it behind `UnsafeCell` to make that interior mutability
        // explicit.
        unsafe { &*self.raw.get() }
    }

    #[inline]
    pub(super) fn inner_mut(&mut self) -> &mut sys::ImTextureData {
        // Safety: caller has `&mut TextureData`, so this is a unique Rust borrow for this wrapper.
        unsafe { &mut *self.raw.get() }
    }

    /// Create a new texture data from raw pointer (crate-internal)
    ///
    /// Safety: caller must ensure the pointer is valid for the returned lifetime.
    pub(crate) unsafe fn from_raw<'a>(raw: *mut sys::ImTextureData) -> &'a mut Self {
        unsafe { &mut *(raw as *mut Self) }
    }

    /// Create a shared texture data view from a raw pointer (crate-internal).
    ///
    /// Safety: caller must ensure the pointer is valid for the returned lifetime.
    pub(crate) unsafe fn from_raw_ref<'a>(raw: *const sys::ImTextureData) -> &'a Self {
        unsafe { &*(raw as *const Self) }
    }

    /// Get the raw pointer to the underlying ImTextureData
    pub fn as_raw(&self) -> *const sys::ImTextureData {
        self.raw.get() as *const _
    }

    /// Get the raw mutable pointer to the underlying ImTextureData
    pub fn as_raw_mut(&mut self) -> *mut sys::ImTextureData {
        self.raw.get()
    }

    /// Get Dear ImGui's debug-only native texture number.
    ///
    /// This value is not a safe identity: user textures default to zero and atlas values are only
    /// unique within one atlas.
    pub(crate) fn native_unique_id(&self) -> i32 {
        self.inner().UniqueID
    }

    /// Get the current status of this texture
    pub fn status(&self) -> TextureStatus {
        TextureStatus::from(self.inner().Status)
    }

    /// Marks this allocation as retained by the Context-owned renderer queue.
    ///
    /// The marker is opaque and is never dereferenced. Keeping it on the native allocation tells
    /// Dear ImGui not to auto-complete a destroy while Rust still owns queued work.
    pub(crate) fn claim_managed_queue(&mut self) {
        let raw = self.as_raw_mut();
        let marker = raw.cast::<c_void>();
        unsafe {
            assert!(
                (*raw).QueueUserData.is_null() || (*raw).QueueUserData == marker,
                "managed texture is already retained by a different native queue"
            );
            (*raw).QueueUserData = marker;
        }
    }

    /// Set the renderer-owned lifecycle status of this texture.
    ///
    /// Managed renderers should return request-bound `TextureFeedback` instead of calling this
    /// method. This escape hatch exists for renderer-owned textures passed directly to native
    /// Dear ImGui backends.
    ///
    /// # Safety
    ///
    /// The caller must own the renderer transition represented by `status`, uphold Dear ImGui's
    /// texture state machine, and synchronize every GPU use affected by the transition. A texture
    /// registered with a `Context` may only be changed by that Context's validated reconciliation
    /// or renderer-teardown path; external callers must use request-bound `TextureFeedback`
    /// instead.
    pub unsafe fn set_status(&mut self, status: TextureStatus) {
        unsafe {
            // When marking a texture as destroyed, Dear ImGui expects the backend to clear any
            // backend bindings (TexID/BackendUserData). Otherwise ImGui will assert when
            // processing the texture list.
            if status == TextureStatus::Destroyed {
                sys::ImTextureData_SetTexID(self.as_raw_mut(), 0 as sys::ImTextureID);
                (*self.as_raw_mut()).BackendUserData = std::ptr::null_mut();
                (*self.as_raw_mut()).QueueUserData = std::ptr::null_mut();
            }
            sys::ImTextureData_SetStatus(self.as_raw_mut(), status.into());
        }
    }

    /// Get the backend user data pointer without dereferencing it.
    pub fn backend_user_data(&self) -> *mut c_void {
        self.inner().BackendUserData
    }

    /// Set the renderer-owned backend data pointer.
    ///
    /// # Safety
    ///
    /// `data` must be null or point to state with the layout expected by the active renderer
    /// backend. That state must remain valid until the backend has stopped using it and the pointer
    /// is cleared. A texture registered with a `Context` may only be changed by that Context's
    /// validated reconciliation path, whose managed protocol does not expose backend-owned
    /// pointers.
    pub unsafe fn set_backend_user_data(&mut self, data: *mut c_void) {
        self.inner_mut().BackendUserData = data;
    }

    /// Get the texture ID
    pub fn tex_id(&self) -> TextureId {
        TextureId::from(self.inner().TexID)
    }

    /// Set the renderer-owned texture identifier.
    ///
    /// Managed renderers should return request-bound `TextureFeedback` instead of calling this
    /// method.
    ///
    /// # Safety
    ///
    /// `tex_id` must be valid for the active renderer backend for every draw command that can
    /// observe it, and changing or clearing it must be synchronized with all GPU use. A texture
    /// registered with a `Context` may only be changed by that Context's validated reconciliation
    /// path; external callers must use request-bound `TextureFeedback` instead.
    pub unsafe fn set_tex_id(&mut self, tex_id: TextureId) {
        unsafe {
            sys::ImTextureData_SetTexID(self.as_raw_mut(), sys::ImTextureID::from(tex_id));
        }
    }

    /// Get the texture format
    pub fn format(&self) -> TextureFormat {
        TextureFormat::from(self.inner().Format)
    }

    /// Get the texture width
    pub fn width(&self) -> u32 {
        u32::try_from(self.raw_width_i32()).unwrap_or(0)
    }

    /// Get the texture height
    pub fn height(&self) -> u32 {
        u32::try_from(self.raw_height_i32()).unwrap_or(0)
    }

    /// Get the bytes per pixel
    pub fn bytes_per_pixel(&self) -> usize {
        usize::try_from(self.raw_bytes_per_pixel_i32()).unwrap_or(0)
    }

    /// Get the number of unused frames
    pub fn unused_frames(&self) -> usize {
        non_negative_texture_count_from_i32(
            "TextureData::unused_frames()",
            self.inner().UnusedFrames,
        )
    }

    /// Get the reference count
    pub fn ref_count(&self) -> u16 {
        self.inner().RefCount
    }

    /// Check if the texture uses colors (rather than just white + alpha)
    pub fn use_colors(&self) -> bool {
        self.inner().UseColors
    }

    /// Check if the texture is queued for destruction next frame
    pub fn want_destroy_next_frame(&self) -> bool {
        self.inner().WantDestroyNextFrame
    }

    /// Get the pixel data
    ///
    /// Returns None if no pixel data is available.
    pub fn pixels(&self) -> Option<&[u8]> {
        let raw = self.inner();
        if raw.Pixels.is_null() {
            None
        } else {
            let width = raw.Width;
            let height = raw.Height;
            let bytes_per_pixel = raw.BytesPerPixel;
            if width <= 0 || height <= 0 || bytes_per_pixel <= 0 {
                return None;
            }

            let size = (width as usize)
                .checked_mul(height as usize)?
                .checked_mul(bytes_per_pixel as usize)?;
            unsafe { Some(std::slice::from_raw_parts(raw.Pixels as *const u8, size)) }
        }
    }

    /// Get the bounding box of all used pixels in the texture
    pub fn used_rect(&self) -> TextureRect {
        TextureRect::from(self.inner().UsedRect)
    }

    /// Get the bounding box of all queued updates
    pub fn update_rect(&self) -> TextureRect {
        TextureRect::from(self.inner().UpdateRect)
    }

    /// Iterate over queued update rectangles (copying to safe TextureRect)
    pub fn updates(&self) -> impl Iterator<Item = TextureRect> + '_ {
        let vec = &self.inner().Updates;
        let count = if vec.Data.is_null() {
            0
        } else {
            usize::try_from(vec.Size).unwrap_or(0)
        };
        let data = vec.Data as *const sys::ImTextureRect;
        (0..count).map(move |i| unsafe { TextureRect::from(*data.add(i)) })
    }

    /// Get the pixel data at a specific position
    ///
    /// Returns None if no pixel data is available or coordinates are out of bounds.
    pub fn pixels_at(&self, x: u32, y: u32) -> Option<&[u8]> {
        let raw = self.inner();
        let width = u32::try_from(raw.Width).ok()?;
        let height = u32::try_from(raw.Height).ok()?;
        let bytes_per_pixel = usize::try_from(raw.BytesPerPixel).ok()?;
        if raw.Pixels.is_null() || width == 0 || height == 0 || bytes_per_pixel == 0 {
            return None;
        }
        if x >= width || y >= height {
            None
        } else {
            let width_usize = usize::try_from(width).ok()?;
            let height_usize = usize::try_from(height).ok()?;
            let x_usize = usize::try_from(x).ok()?;
            let y_usize = usize::try_from(y).ok()?;

            let total_size = width_usize
                .checked_mul(height_usize)?
                .checked_mul(bytes_per_pixel)?;

            let offset_px = y_usize.checked_mul(width_usize)?.checked_add(x_usize)?;
            let offset_bytes = offset_px.checked_mul(bytes_per_pixel)?;
            let remaining_size = total_size.checked_sub(offset_bytes)?;

            unsafe {
                let ptr = (raw.Pixels as *const u8).add(offset_bytes);
                Some(std::slice::from_raw_parts(ptr, remaining_size))
            }
        }
    }

    /// Get the pitch (bytes per row)
    pub fn pitch(&self) -> usize {
        let width = self.width();
        let bytes_per_pixel = self.bytes_per_pixel();
        if width == 0 || bytes_per_pixel == 0 {
            return 0;
        }
        usize::try_from(width)
            .expect("TextureData::pitch() width must fit usize")
            .checked_mul(bytes_per_pixel)
            .expect("TextureData::pitch() byte pitch overflowed usize")
    }

    /// Replace every pixel using an exact, tightly packed payload.
    ///
    /// Validation is transactional: an error leaves the pixel allocation, contents, status, and
    /// queued update rectangles unchanged. Live textures queue a full update through Dear ImGui's
    /// native update list; textures awaiting initial creation keep `WantCreate`.
    ///
    /// # Errors
    ///
    /// Returns [`TextureDataError`] when the texture is not mutable, its native layout is invalid,
    /// the payload length is not exact, or a live full update cannot be represented safely.
    pub fn replace_pixels(&mut self, pixels: &[u8]) -> Result<(), TextureDataError> {
        let raw = self.inner();
        let layout = validate_native_texture_layout(raw.Width, raw.Height, raw.BytesPerPixel)?;
        require_exact_payload(layout.byte_len, pixels.len())?;
        let status = validate_mutable_texture(raw)?;
        let queued_rect = if status == TextureStatus::WantCreate {
            None
        } else {
            let region =
                TextureRegion::from_validated_dimensions(0, 0, layout.width, layout.height);
            Some(
                validate_live_update_region(raw, layout, region).map_err(|error| match error {
                    TextureDataError::UpdateRegionNotRepresentable(_) => {
                        TextureDataError::FullUpdateRectOutOfRange {
                            width: layout.width,
                            height: layout.height,
                        }
                    }
                    other => other,
                })?,
            )
        };

        unsafe {
            std::ptr::copy_nonoverlapping(
                pixels.as_ptr(),
                self.inner().Pixels.cast::<u8>(),
                layout.byte_len,
            );
        }
        if let Some(rect) = queued_rect {
            queue_texture_upload(self.as_raw_mut(), rect);
        }
        Ok(())
    }

    /// Copy a strided source payload into one texture region.
    ///
    /// The exact payload length is `(height - 1) * row_pitch + tight_row_bytes`. This permits
    /// padding between rows without accepting unused bytes after the final row. All validation and
    /// offset checks complete before the first destination byte is written. A texture in
    /// `WantCreate` changes its initial pixels without queuing an update rectangle or changing
    /// status. A texture in `OK` or `WantUpdates` queues the region and ends in `WantUpdates`.
    ///
    /// # Errors
    ///
    /// Returns [`TextureDataError`] when the texture is not mutable, the region is out of bounds,
    /// the row pitch or payload is invalid, or the queued native rectangle is not representable.
    pub fn update_subresource(
        &mut self,
        update: TextureSubresource<'_>,
    ) -> Result<(), TextureDataError> {
        let raw = self.inner();
        let layout = validate_native_texture_layout(raw.Width, raw.Height, raw.BytesPerPixel)?;
        let status = validate_mutable_texture(raw)?;
        let validated = validate_subresource(raw, layout, update, status)?;

        let destination = unsafe {
            std::slice::from_raw_parts_mut(self.inner().Pixels.cast::<u8>(), layout.byte_len)
        };
        let region = update.region();
        let row_count = usize::try_from(region.height()).expect("validated height must fit usize");
        let y = usize::try_from(region.y()).expect("validated y must fit usize");
        for row in 0..row_count {
            let source_start = row * update.row_pitch();
            let destination_start = (y + row) * layout.row_pitch + validated.x_bytes;
            destination[destination_start..destination_start + validated.tight_row_bytes]
                .copy_from_slice(
                    &update.pixels()[source_start..source_start + validated.tight_row_bytes],
                );
        }
        if let Some(rect) = validated.queued_rect {
            queue_texture_upload(self.as_raw_mut(), rect);
        }
        Ok(())
    }

    pub(crate) fn raw_width_i32(&self) -> i32 {
        self.inner().Width
    }

    pub(crate) fn raw_height_i32(&self) -> i32 {
        self.inner().Height
    }

    pub(crate) fn raw_bytes_per_pixel_i32(&self) -> i32 {
        self.inner().BytesPerPixel
    }
}

#[derive(Clone, Copy, Debug)]
struct ValidatedSubresource {
    x_bytes: usize,
    tight_row_bytes: usize,
    queued_rect: Option<sys::ImTextureRect>,
}

fn validate_mutable_texture(raw: &sys::ImTextureData) -> Result<TextureStatus, TextureDataError> {
    let status = TextureStatus::from(raw.Status);
    if matches!(
        status,
        TextureStatus::Destroyed | TextureStatus::WantDestroy
    ) {
        return Err(TextureDataError::InvalidStatus(status));
    }
    if raw.Pixels.is_null() {
        return Err(TextureDataError::MissingPixelStorage(status));
    }
    Ok(status)
}

fn validate_subresource(
    raw: &sys::ImTextureData,
    layout: TextureLayout,
    update: TextureSubresource<'_>,
    status: TextureStatus,
) -> Result<ValidatedSubresource, TextureDataError> {
    let region = update.region();
    validate_region_bounds(layout, region)?;

    let region_width = usize::try_from(region.width()).expect("validated width must fit usize");
    let region_height = usize::try_from(region.height()).expect("validated height must fit usize");
    let tight_row_bytes = region_width.checked_mul(layout.bytes_per_pixel).ok_or(
        TextureDataError::PayloadSizeOutOfRange {
            row_pitch: update.row_pitch(),
            height: region.height(),
        },
    )?;
    if update.row_pitch() < tight_row_bytes {
        return Err(TextureDataError::RowPitchTooSmall {
            minimum: tight_row_bytes,
            actual: update.row_pitch(),
        });
    }
    let expected = update
        .row_pitch()
        .checked_mul(region_height - 1)
        .and_then(|bytes| bytes.checked_add(tight_row_bytes))
        .ok_or(TextureDataError::PayloadSizeOutOfRange {
            row_pitch: update.row_pitch(),
            height: region.height(),
        })?;
    require_exact_payload(expected, update.pixels().len())?;

    let x = usize::try_from(region.x()).expect("validated x must fit usize");
    let y = usize::try_from(region.y()).expect("validated y must fit usize");
    let x_bytes =
        x.checked_mul(layout.bytes_per_pixel)
            .ok_or(TextureDataError::PayloadSizeOutOfRange {
                row_pitch: update.row_pitch(),
                height: region.height(),
            })?;
    let last_row = y + region_height - 1;
    let destination_end = last_row
        .checked_mul(layout.row_pitch)
        .and_then(|offset| offset.checked_add(x_bytes))
        .and_then(|offset| offset.checked_add(tight_row_bytes))
        .ok_or(TextureDataError::PayloadSizeOutOfRange {
            row_pitch: update.row_pitch(),
            height: region.height(),
        })?;
    debug_assert!(destination_end <= layout.byte_len);

    let queued_rect = if status == TextureStatus::WantCreate {
        None
    } else {
        Some(validate_live_update_region(raw, layout, region)?)
    };
    Ok(ValidatedSubresource {
        x_bytes,
        tight_row_bytes,
        queued_rect,
    })
}

fn validate_region_bounds(
    layout: TextureLayout,
    region: TextureRegion,
) -> Result<(), TextureDataError> {
    let right = region.x().checked_add(region.width());
    let bottom = region.y().checked_add(region.height());
    if right.is_none_or(|right| right > layout.width)
        || bottom.is_none_or(|bottom| bottom > layout.height)
    {
        return Err(TextureDataError::UpdateRegionOutOfBounds {
            region,
            width: layout.width,
            height: layout.height,
        });
    }
    Ok(())
}

fn validate_live_update_region(
    raw: &sys::ImTextureData,
    layout: TextureLayout,
    region: TextureRegion,
) -> Result<sys::ImTextureRect, TextureDataError> {
    validate_region_bounds(layout, region)?;
    let right = region
        .x()
        .checked_add(region.width())
        .expect("validated region endpoint must not overflow");
    let bottom = region
        .y()
        .checked_add(region.height())
        .expect("validated region endpoint must not overflow");
    let native_endpoint_limit = u32::from(u16::MAX) + 1;
    if right > native_endpoint_limit || bottom > native_endpoint_limit {
        return Err(TextureDataError::UpdateRegionNotRepresentable(region));
    }
    let rect = sys::ImTextureRect {
        x: u16::try_from(region.x())
            .map_err(|_| TextureDataError::UpdateRegionNotRepresentable(region))?,
        y: u16::try_from(region.y())
            .map_err(|_| TextureDataError::UpdateRegionNotRepresentable(region))?,
        w: u16::try_from(region.width())
            .map_err(|_| TextureDataError::UpdateRegionNotRepresentable(region))?,
        h: u16::try_from(region.height())
            .map_err(|_| TextureDataError::UpdateRegionNotRepresentable(region))?,
    };
    if !queued_union_is_representable(raw.UpdateRect, rect, true)
        || !queued_union_is_representable(raw.UsedRect, rect, false)
    {
        return Err(TextureDataError::UpdateRegionNotRepresentable(region));
    }
    Ok(rect)
}

fn queued_union_is_representable(
    existing: sys::ImTextureRect,
    request: sys::ImTextureRect,
    empty_axis_starts_at_zero: bool,
) -> bool {
    union_axis_is_representable(
        existing.x,
        existing.w,
        request.x,
        request.w,
        empty_axis_starts_at_zero,
    ) && union_axis_is_representable(
        existing.y,
        existing.h,
        request.y,
        request.h,
        empty_axis_starts_at_zero,
    )
}

fn union_axis_is_representable(
    existing_start: u16,
    existing_len: u16,
    request_start: u16,
    request_len: u16,
    empty_axis_starts_at_zero: bool,
) -> bool {
    let existing_start = u32::from(existing_start);
    let existing_end = if empty_axis_starts_at_zero && existing_len == 0 {
        0
    } else {
        existing_start + u32::from(existing_len)
    };
    let request_start = u32::from(request_start);
    let request_end = request_start + u32::from(request_len);
    let union_start = existing_start.min(request_start);
    let union_end = existing_end.max(request_end);
    union_end - union_start <= u32::from(u16::MAX)
}

fn queue_texture_upload(texture: *mut sys::ImTextureData, rect: sys::ImTextureRect) {
    unsafe {
        // All native assertions (status, non-empty 16-bit fields, endpoints, and bounding unions)
        // were validated before any pixel mutation.
        sys::igImTextureDataQueueUpload(
            texture,
            i32::from(rect.x),
            i32::from(rect.y),
            i32::from(rect.w),
            i32::from(rect.h),
        );
    }
}