wxdragon 0.9.15

Safe Rust bindings for wxWidgets via the wxDragon C wrapper
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
//! Variant wrapper for typed wxVariant C API.

use crate::utils::ArrayString;
use crate::{Bitmap, DateTime};
use std::ffi::CStr;
use std::marker::PhantomData;
use std::rc::Rc;
use wxdragon_sys as ffi;

/// Represents the type of data stored in a variant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VariantType {
    Bool,
    Int32,
    Int64,
    Double,
    String,
    DateTime,
    Bitmap,
    ArrayString,
    Progress,
    IconText,
}

impl VariantType {
    pub fn as_str(&self) -> &'static str {
        match self {
            VariantType::Bool => "bool",
            VariantType::Int32 => "long",
            VariantType::Int64 => "longlong",
            VariantType::Double => "double",
            VariantType::String => "string",
            VariantType::DateTime => "datetime",
            VariantType::Bitmap => "wxBitmap",
            VariantType::ArrayString => "wxArrayString",
            VariantType::Progress => "long",
            VariantType::IconText => "wxDataViewIconText",
        }
    }
}

/// Safe Rust wrapper over a wxVariant pointer (wxd_Variant_t).
///
/// Owns the underlying wxVariant by default and destroys it in Drop.
pub struct Variant {
    ptr: *mut ffi::wxd_Variant_t,
    /// Indicates whether this Rust wrapper owns the underlying wxVariant and is responsible for destroying it.
    /// Ownership is determined by how the pointer was obtained (e.g., from `From<*mut>` vs `From<*const>`), not by the pointer's type.
    owned: bool,
    // Prevent Send/Sync: wxWidgets objects are not thread-safe and must stay on UI thread.
    _nosend_nosync: PhantomData<Rc<()>>,
}

impl Variant {
    /// Create an empty variant.
    pub fn new() -> Self {
        let ptr = unsafe { ffi::wxd_Variant_CreateEmpty() };
        assert!(!ptr.is_null(), "Failed to create wxVariant");
        Self {
            ptr,
            owned: true,
            _nosend_nosync: PhantomData,
        }
    }

    pub fn is_owned(&self) -> bool {
        self.owned
    }

    /// Create a new variant and set it to a bool value.
    pub fn from_bool(v: bool) -> Self {
        let var = Self::new();
        unsafe { ffi::wxd_Variant_SetBool(var.ptr, v) };
        var
    }

    pub fn from_i32(v: i32) -> Self {
        let var = Self::new();
        unsafe { ffi::wxd_Variant_SetInt32(var.ptr, v) };
        var
    }

    pub fn from_i64(v: i64) -> Self {
        let var = Self::new();
        unsafe { ffi::wxd_Variant_SetInt64(var.ptr, v) };
        var
    }

    pub fn from_f64(v: f64) -> Self {
        let var = Self::new();
        unsafe { ffi::wxd_Variant_SetDouble(var.ptr, v) };
        var
    }

    pub fn from_string<S: AsRef<str>>(s: S) -> Self {
        let var = Self::new();
        let b = s.as_ref().as_bytes();
        unsafe { ffi::wxd_Variant_SetString_Utf8(var.ptr, b.as_ptr() as _, b.len() as i32) };
        var
    }

    pub fn from_datetime(dt: &DateTime) -> Self {
        let mut var = Self::new();
        unsafe { ffi::wxd_Variant_SetDateTime(var.as_mut_ptr(), dt.as_const_ptr()) };
        var
    }

    pub fn from_bitmap(bmp: &Bitmap) -> Self {
        let mut var = Self::new();
        unsafe { ffi::wxd_Variant_SetBitmap(var.as_mut_ptr(), bmp.as_const_ptr()) };
        var
    }

    /// Create a new variant from a slice of Rust strings by storing a wxArrayString by value inside wxVariant.
    pub fn from_array_string(strings: &ArrayString) -> Self {
        let mut var = Self::new();
        unsafe { ffi::wxd_Variant_SetArrayString(var.as_mut_ptr(), strings.as_const_ptr()) };
        var
    }

    /// Returns a const raw pointer to the underlying wxd_Variant_t.
    ///
    /// Ownership notes:
    /// - This does not transfer ownership; the pointer is only valid while this `Variant` is alive (unless moved via `into_raw_*`).
    /// - Do not destroy this pointer yourself. Destruction is handled by the owning wrapper or by `into_raw_mut` transfer.
    pub fn as_const_ptr(&self) -> *const ffi::wxd_Variant_t {
        self.ptr as *const _
    }

    /// Returns a mutable raw pointer to the underlying wxd_Variant_t.
    ///
    /// Use with care; prefer safe methods when possible.
    ///
    /// Ownership notes:
    /// - This does not transfer ownership. If you mutate through this pointer, you must uphold exclusive access and invariants.
    /// - Do not destroy the returned pointer here; use `into_raw_mut` to transfer ownership if you need to manage lifetime manually.
    pub fn as_mut_ptr(&mut self) -> *mut ffi::wxd_Variant_t {
        self.ptr
    }

    /// Consumes self and returns a raw mutable pointer, transferring ownership to the caller.
    ///
    /// After calling this, you must NOT use the original `Variant` again.
    ///
    /// Caller responsibilities:
    /// - You now own the pointer and must destroy it exactly once with `wxd_Variant_Destroy`.
    /// - Ensure no further use-after-free occurs.
    pub fn into_raw_mut(self) -> *mut ffi::wxd_Variant_t {
        assert!(self.owned, "into_raw_mut can only be called on owning Variant instances");
        let ptr = self.ptr;
        std::mem::forget(self);
        ptr
    }

    /// Consumes a borrowed (non-owning) wrapper and returns a raw const pointer without taking ownership.
    ///
    /// Panics if called on an owning wrapper to avoid leaking the owned resource.
    ///
    /// Caller responsibilities:
    /// - This does NOT transfer ownership; do not destroy the returned pointer.
    /// - The pointer remains valid only while the original owner keeps it alive.
    pub fn into_raw_const(self) -> *const ffi::wxd_Variant_t {
        assert!(
            !self.owned,
            "into_raw_const must only be used on non-owning (borrowed) wrappers"
        );
        let ptr = self.ptr as *const _;
        std::mem::forget(self);
        ptr
    }

    /// Returns the wxVariant type name (e.g., "string", "bool").
    pub fn type_name(&self) -> String {
        // Query required length first by calling with out_len=0
        let needed = unsafe { ffi::wxd_Variant_GetTypeName_Utf8(self.as_const_ptr(), std::ptr::null_mut(), 0) };
        if needed <= 0 {
            return String::new();
        }
        let mut b = vec![
            0;
            match needed.checked_add(1) {
                Some(len) => len as usize,
                None => return String::new(),
            }
        ];
        let w = unsafe { ffi::wxd_Variant_GetTypeName_Utf8(self.as_const_ptr(), b.as_mut_ptr(), b.len()) };
        if w <= 0 {
            return String::new();
        }
        unsafe { CStr::from_ptr(b.as_ptr()).to_string_lossy().into_owned() }
    }

    pub fn get_bool(&self) -> Option<bool> {
        let mut out = false;
        let ok = unsafe { ffi::wxd_Variant_GetBool(self.as_const_ptr(), &mut out) };
        if ok { Some(out) } else { None }
    }

    pub fn get_i32(&self) -> Option<i32> {
        let mut out = 0_i32;
        let ok = unsafe { ffi::wxd_Variant_GetInt32(self.as_const_ptr(), &mut out) };
        if ok { Some(out) } else { None }
    }

    pub fn get_i64(&self) -> Option<i64> {
        let mut out = 0_i64;
        let ok = unsafe { ffi::wxd_Variant_GetInt64(self.as_const_ptr(), &mut out) };
        if ok { Some(out) } else { None }
    }

    pub fn get_f64(&self) -> Option<f64> {
        let mut out = 0_f64;
        let ok = unsafe { ffi::wxd_Variant_GetDouble(self.as_const_ptr(), &mut out) };
        if ok { Some(out) } else { None }
    }

    pub fn get_string(&self) -> Option<String> {
        let needed = unsafe { ffi::wxd_Variant_GetString_Utf8(self.as_const_ptr(), std::ptr::null_mut(), 0) };
        if needed < 0 {
            return None;
        }
        let mut buf = vec![
            0;
            match needed.checked_add(1) {
                Some(len) => len as usize,
                None => return Some(String::new()),
            }
        ];
        let w = unsafe { ffi::wxd_Variant_GetString_Utf8(self.as_const_ptr(), buf.as_mut_ptr(), buf.len()) };
        if w < 0 {
            return None;
        }
        unsafe { Some(CStr::from_ptr(buf.as_ptr()).to_string_lossy().into_owned()) }
    }

    pub fn get_datetime(&self) -> Option<DateTime> {
        let ptr = unsafe { ffi::wxd_Variant_GetDateTime(self.as_const_ptr()) };
        if ptr.is_null() { None } else { Some(DateTime::from(ptr)) }
    }

    pub fn get_bitmap(&self) -> Option<Bitmap> {
        let ptr = unsafe { ffi::wxd_Variant_GetBitmapClone(self.as_const_ptr()) };
        if ptr.is_null() { None } else { Some(Bitmap::from(ptr)) }
    }

    /// If this variant stores a wxArrayString, return it as an ArrayString.
    pub fn get_array_string(&self) -> Option<ArrayString> {
        let ptr = unsafe { ffi::wxd_Variant_GetArrayStringClone(self.as_const_ptr()) };
        if ptr.is_null() { None } else { Some(ArrayString::from(ptr)) }
    }
}

impl Clone for Variant {
    fn clone(&self) -> Self {
        let cloned = unsafe { ffi::wxd_Variant_Clone(self.as_const_ptr()) };
        assert!(!cloned.is_null(), "Failed to clone wxVariant");
        Self::from(cloned)
    }
}

impl Drop for Variant {
    fn drop(&mut self) {
        if self.is_owned() && !self.ptr.is_null() {
            unsafe { ffi::wxd_Variant_Destroy(self.as_mut_ptr()) };
        }
    }
}

impl Default for Variant {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for Variant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Variant(type={})", self.type_name())
    }
}

impl From<*const ffi::wxd_Variant_t> for Variant {
    /// Does not take ownership of the raw pointer.
    fn from(ptr: *const ffi::wxd_Variant_t) -> Self {
        assert!(!ptr.is_null(), "invalid null pointer passed to Variant::from");
        Variant {
            ptr: ptr as *mut _,
            owned: false,
            _nosend_nosync: PhantomData,
        }
    }
}

impl From<*mut ffi::wxd_Variant_t> for Variant {
    /// Takes ownership of the raw pointer.
    fn from(ptr: *mut ffi::wxd_Variant_t) -> Self {
        assert!(!ptr.is_null(), "invalid null pointer passed to Variant::from");
        Variant {
            ptr,
            owned: true,
            _nosend_nosync: PhantomData,
        }
    }
}

impl TryFrom<Variant> for *const ffi::wxd_Variant_t {
    type Error = std::io::Error;
    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        if value.ptr.is_null() {
            return Err(Error::new(InvalidInput, "Variant pointer is null"));
        }
        if value.is_owned() {
            return Err(Error::new(
                InvalidData,
                "Variant owns the pointer, please use mutable version",
            ));
        }
        Ok(value.ptr)
    }
}

impl TryFrom<Variant> for *mut ffi::wxd_Variant_t {
    type Error = std::io::Error;
    fn try_from(mut value: Variant) -> Result<Self, Self::Error> {
        if value.ptr.is_null() {
            return Err(Error::new(InvalidInput, "Variant pointer is null"));
        }
        if !value.is_owned() {
            return Err(Error::new(
                InvalidData,
                "Variant does not own the pointer, please use const version",
            ));
        }
        value.owned = false;
        Ok(value.ptr as *mut _)
    }
}

impl From<bool> for Variant {
    fn from(value: bool) -> Self {
        Self::from_bool(value)
    }
}

impl From<i32> for Variant {
    fn from(value: i32) -> Self {
        Self::from_i32(value)
    }
}

impl From<i64> for Variant {
    fn from(value: i64) -> Self {
        Self::from_i64(value)
    }
}

impl From<f64> for Variant {
    fn from(value: f64) -> Self {
        Self::from_f64(value)
    }
}

impl From<&str> for Variant {
    fn from(value: &str) -> Self {
        Self::from_string(value)
    }
}

impl From<String> for Variant {
    fn from(value: String) -> Self {
        Self::from_string(value)
    }
}

impl From<DateTime> for Variant {
    fn from(value: DateTime) -> Self {
        Self::from(&value)
    }
}

impl<'a> From<&'a DateTime> for Variant {
    fn from(value: &'a DateTime) -> Self {
        Self::from_datetime(value)
    }
}

impl From<Bitmap> for Variant {
    fn from(value: Bitmap) -> Self {
        Self::from_bitmap(&value)
    }
}

impl<'a> From<&'a Bitmap> for Variant {
    fn from(value: &'a Bitmap) -> Self {
        Self::from_bitmap(value)
    }
}

impl<T: AsRef<str>> From<Vec<T>> for Variant {
    fn from(value: Vec<T>) -> Self {
        let arr = ArrayString::from(value);
        Variant::from(&arr)
    }
}

impl<T: AsRef<str>> From<&[T]> for Variant {
    fn from(value: &[T]) -> Self {
        let arr = ArrayString::from(value);
        Variant::from(&arr)
    }
}

impl<T: AsRef<str>> From<&Vec<T>> for Variant {
    fn from(value: &Vec<T>) -> Self {
        let arr = ArrayString::from(value.as_slice());
        Variant::from(&arr)
    }
}

impl From<ArrayString> for Variant {
    fn from(value: ArrayString) -> Self {
        Variant::from_array_string(&value)
    }
}

impl From<&ArrayString> for Variant {
    fn from(value: &ArrayString) -> Self {
        Variant::from_array_string(value)
    }
}

use std::io::{Error, ErrorKind::InvalidData, ErrorKind::InvalidInput};

impl TryFrom<Variant> for bool {
    type Error = std::io::Error;

    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        let type_name = value.type_name();
        value
            .get_bool()
            .ok_or(Error::new(InvalidData, format!("Not a bool, it's a {type_name}")))
    }
}

impl TryFrom<Variant> for i32 {
    type Error = std::io::Error;

    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        let type_name = value.type_name();
        value
            .get_i32()
            .ok_or(Error::new(InvalidData, format!("Not an i32, it's a {type_name}")))
    }
}

impl TryFrom<Variant> for i64 {
    type Error = std::io::Error;

    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        let type_name = value.type_name();
        value
            .get_i64()
            .ok_or(Error::new(InvalidData, format!("Not an i64, it's a {type_name}")))
    }
}

impl TryFrom<Variant> for f64 {
    type Error = std::io::Error;

    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        let type_name = value.type_name();
        value
            .get_f64()
            .ok_or(Error::new(InvalidData, format!("Not an f64, it's a {type_name}")))
    }
}

impl TryFrom<Variant> for String {
    type Error = std::io::Error;

    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        let type_name = value.type_name();
        value
            .get_string()
            .ok_or(Error::new(InvalidData, format!("Not a String, it's a {type_name}")))
    }
}

impl TryFrom<Variant> for DateTime {
    type Error = std::io::Error;

    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        let type_name = value.type_name();
        value
            .get_datetime()
            .ok_or(Error::new(InvalidData, format!("Not a DateTime, it's a {type_name}")))
    }
}

impl TryFrom<Variant> for Bitmap {
    type Error = std::io::Error;

    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        let type_name = value.type_name();
        value
            .get_bitmap()
            .ok_or(Error::new(InvalidData, format!("Not a Bitmap, it's a {type_name}")))
    }
}

impl TryFrom<Variant> for ArrayString {
    type Error = std::io::Error;

    fn try_from(value: Variant) -> Result<Self, Self::Error> {
        let type_name = value.type_name();
        value
            .get_array_string()
            .ok_or(Error::new(InvalidData, format!("Not a wxArrayString, it's a {type_name}")))
    }
}

#[cfg(test)]
mod tests {
    use super::Variant;
    use crate::ArrayString;

    #[test]
    fn variant_array_string_roundtrip() {
        let src = vec!["alpha", "beta", "gamma"];
        let v = Variant::from(&src);
        // Type name differs by backend; ensure it's some array string form
        let tn = v.type_name();
        println!("Variant type name: {}", tn);
        assert!(tn.contains("string"), "unexpected type name: {}", tn);
        let got = v.get_array_string().expect("expected array string");
        assert_eq!(src, got.get_strings());

        // TryFrom
        let got2: ArrayString = v.clone().try_into().expect("convert to ArrayString");
        assert_eq!(src, got2.get_strings());
    }
}