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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
use std::collections::HashMap;
use std::ffi::{CString, CStr};
use std::mem;
use std::ptr;
use libc::{c_void, c_char};
use ffi;
pub trait Ptr: Copy {
fn is_null(&self) -> bool;
fn from<X>(ptr: *mut X) -> Self;
}
impl <T> Ptr for *const T {
#[inline]
fn is_null(&self) -> bool { (*self).is_null() }
#[inline]
fn from<X>(ptr: *mut X) -> *const T { ptr as *const T }
}
impl <T> Ptr for *mut T {
#[inline]
fn is_null(&self) -> bool { (*self).is_null() }
#[inline]
fn from<X>(ptr: *mut X) -> *mut T { ptr as *mut T }
}
pub struct Stash<'a, P: Copy, T: ?Sized + ToGlibPtr<'a, P>> (pub P, pub <T as ToGlibPtr<'a, P>>::Storage);
pub trait ToGlib {
type GlibType;
fn to_glib(&self) -> Self::GlibType;
}
impl ToGlib for () {
type GlibType = ();
#[inline]
fn to_glib(&self) -> () {
()
}
}
impl ToGlib for bool {
type GlibType = ffi::gboolean;
#[inline]
fn to_glib(&self) -> ffi::gboolean {
if *self { ffi::GTRUE } else { ffi::GFALSE }
}
}
pub trait ToGlibPtr<'a, P: Copy> {
type Storage;
fn to_glib_none(&self) -> Stash<'a, P, Self>;
fn to_glib_full(&self) -> P {
unimplemented!();
}
}
impl <'a, P: Ptr, T: ToGlibPtr<'a, P>> ToGlibPtr<'a, P> for Option<T> {
type Storage = Option<<T as ToGlibPtr<'a, P>>::Storage>;
#[inline]
fn to_glib_none(&self) -> Stash<'a, P, Option<T>> {
self.as_ref().map_or(Stash(Ptr::from::<()>(ptr::null_mut()), None), |s| {
let s = s.to_glib_none();
Stash(s.0, Some(s.1))
})
}
#[inline]
fn to_glib_full(&self) -> P {
self.as_ref().map_or(Ptr::from::<()>(ptr::null_mut()), |s| s.to_glib_full())
}
}
impl<'a> ToGlibPtr<'a, *const c_char> for &'a str {
type Storage = CString;
#[inline]
fn to_glib_none(&self) -> Stash<'a, *const c_char, &'a str> {
let tmp = CString::new(*self).unwrap();
Stash(tmp.as_ptr(), tmp)
}
#[inline]
fn to_glib_full(&self) -> *const c_char {
unsafe {
ffi::g_strndup(self.as_ptr() as *const c_char, self.len() as ffi::gsize)
as *const c_char
}
}
}
impl <'a> ToGlibPtr<'a, *const c_char> for String {
type Storage = CString;
#[inline]
fn to_glib_none(&self) -> Stash<'a, *const c_char, String> {
let tmp = CString::new(&self[..]).unwrap();
Stash(tmp.as_ptr(), tmp)
}
#[inline]
fn to_glib_full(&self) -> *const c_char {
unsafe {
ffi::g_strndup(self.as_ptr() as *const c_char, self.len() as ffi::gsize)
as *const c_char
}
}
}
impl<'a, S: AsRef<str>> ToGlibPtr<'a, *const *const c_char> for &'a [S] {
type Storage = PtrArray<'a, *const c_char, &'a str>;
#[inline]
fn to_glib_none(&self) -> Stash<'a, *const *const c_char, Self> {
let mut tmp_vec: Vec<_> =
self.into_iter().map(|v| AsRef::<str>::as_ref(v).to_glib_none()).collect();
let mut ptr_vec: Vec<_> =
tmp_vec.iter_mut().map(|v| v.0).collect();
unsafe {
let zero = mem::zeroed();
ptr_vec.push(zero);
}
Stash(ptr_vec.as_ptr(), PtrArray(ptr_vec, tmp_vec))
}
}
impl<'a, P: Copy, T: ToGlibPtr<'a, P>> ToGlibPtr<'a, *mut P> for &'a [T] {
type Storage = PtrArray<'a, P, T>;
#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut P, Self> {
let mut tmp_vec: Vec<_> =
self.into_iter().map(|v| v.to_glib_none()).collect();
let mut ptr_vec: Vec<_> =
tmp_vec.iter_mut().map(|v| v.0).collect();
unsafe {
let zero = mem::zeroed();
ptr_vec.push(zero);
}
Stash(ptr_vec.as_mut_ptr(), PtrArray(ptr_vec, tmp_vec))
}
}
pub struct PtrArray<'a, P: Copy, T: ?Sized + ToGlibPtr<'a, P>> (Vec<P>, Vec<Stash<'a, P, T>>);
impl<'a, P: Copy, T: ToGlibPtr<'a, P>> PtrArray<'a, P, T> {
pub fn len(&self) -> usize {
self.1.len()
}
}
impl<'a> ToGlibPtr<'a, *mut ffi::GHashTable> for HashMap<String, String> {
type Storage = (HashTable);
#[inline]
fn to_glib_none(&self) -> Stash<'a, *mut ffi::GHashTable, Self> {
let ptr = self.to_glib_full();
Stash(ptr, HashTable(ptr))
}
#[inline]
fn to_glib_full(&self) -> *mut ffi::GHashTable {
unsafe {
let ptr = ffi::g_hash_table_new_full(ffi::g_str_hash, ffi::g_str_equal, ffi::g_free,
ffi::g_free);
for (k, v) in self {
ffi::g_hash_table_insert(ptr, k.to_glib_full() as *mut _,
v.to_glib_full() as *mut _);
}
ptr
}
}
}
pub struct HashTable(*mut ffi::GHashTable);
impl Drop for HashTable {
fn drop(&mut self) {
unsafe { ffi::g_hash_table_unref(self.0) }
}
}
pub trait FromGlib<T>: Sized {
fn from_glib(val: T) -> Self;
}
#[inline]
pub fn from_glib<G, T: FromGlib<G>>(val: G) -> T {
FromGlib::from_glib(val)
}
impl FromGlib<ffi::gboolean> for bool {
#[inline]
fn from_glib(val: ffi::gboolean) -> bool {
!(val == ffi::GFALSE)
}
}
impl FromGlib<i32> for Option<u32> {
#[inline]
fn from_glib(val: i32) -> Option<u32> {
if val >= 0 {
Some(val as u32)
}
else {
None
}
}
}
impl FromGlib<i64> for Option<u64> {
#[inline]
fn from_glib(val: i64) -> Option<u64> {
if val >= 0 {
Some(val as u64)
}
else {
None
}
}
}
impl FromGlib<i32> for Option<u64> {
#[inline]
fn from_glib(val: i32) -> Option<u64> {
FromGlib::from_glib(val as i64)
}
}
pub trait FromGlibPtr<P: Ptr>: Sized {
unsafe fn from_glib_none(ptr: P) -> Self;
unsafe fn from_glib_full(ptr: P) -> Self;
}
#[inline]
pub unsafe fn from_glib_none<P: Ptr, T: FromGlibPtr<P>>(ptr: P) -> T {
FromGlibPtr::from_glib_none(ptr)
}
#[inline]
pub unsafe fn from_glib_full<P: Ptr, T: FromGlibPtr<P>>(ptr: P) -> T {
FromGlibPtr::from_glib_full(ptr)
}
impl<P: Ptr, T: FromGlibPtr<P>> FromGlibPtr<P> for Option<T> {
#[inline]
unsafe fn from_glib_none(ptr: P) -> Option<T> {
if ptr.is_null() { None }
else { Some(from_glib_none(ptr)) }
}
#[inline]
unsafe fn from_glib_full(ptr: P) -> Option<T> {
if ptr.is_null() { None }
else { Some(from_glib_full(ptr)) }
}
}
impl FromGlibPtr<*const c_char> for String {
#[inline]
unsafe fn from_glib_none(ptr: *const c_char) -> Self {
assert!(!ptr.is_null());
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).into_owned()
}
#[inline]
unsafe fn from_glib_full(ptr: *const c_char) -> Self {
let res = from_glib_none(ptr);
ffi::g_free(ptr as *mut _);
res
}
}
impl FromGlibPtr<*mut c_char> for String {
#[inline]
unsafe fn from_glib_none(ptr: *mut c_char) -> Self {
assert!(!ptr.is_null());
String::from_utf8_lossy(CStr::from_ptr(ptr).to_bytes()).into_owned()
}
#[inline]
unsafe fn from_glib_full(ptr: *mut c_char) -> Self {
let res = from_glib_none(ptr);
ffi::g_free(ptr as *mut _);
res
}
}
pub trait FromGlibPtrContainer<P: Ptr, PP: Ptr>: Sized {
unsafe fn from_glib_none(ptr: PP) -> Self;
unsafe fn from_glib_none_num(ptr: PP, num: usize) -> Self;
unsafe fn from_glib_container(ptr: PP) -> Self;
unsafe fn from_glib_container_num(ptr: PP, num: usize) -> Self;
unsafe fn from_glib_full(ptr: PP) -> Self;
unsafe fn from_glib_full_num(ptr: PP, num: usize) -> Self;
}
unsafe fn c_array_len<P: Ptr>(mut ptr: *const P) -> usize {
let mut len = 0;
if !ptr.is_null() {
while !(*ptr).is_null() {
len += 1;
ptr = ptr.offset(1);
}
}
len
}
impl <P: Ptr, T: FromGlibPtr<P>>
FromGlibPtrContainer<P, *const P>
for Vec<T> {
unsafe fn from_glib_none(ptr: *const P) -> Vec<T> {
let num = c_array_len(ptr);
Vec::from_glib_none_num(ptr, num)
}
unsafe fn from_glib_none_num(mut ptr: *const P,
num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new()
}
let mut res = Vec::with_capacity(num);
while !(*ptr).is_null() {
res.push(from_glib_none(*ptr));
ptr = ptr.offset(1);
}
res
}
unsafe fn from_glib_container(ptr: *const P) -> Vec<T> {
let num = c_array_len(ptr);
FromGlibPtrContainer::from_glib_container_num(ptr, num)
}
unsafe fn from_glib_container_num(ptr: *const P,
num: usize) -> Vec<T> {
let res = FromGlibPtrContainer::from_glib_none_num(ptr, num);
ffi::g_free(ptr as *mut _);
res
}
unsafe fn from_glib_full(ptr: *const P) -> Vec<T> {
let num = c_array_len(ptr);
FromGlibPtrContainer::from_glib_full_num(ptr, num)
}
unsafe fn from_glib_full_num(mut ptr: *const P,
num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new()
}
let mut res = Vec::with_capacity(num);
while !(*ptr).is_null() {
res.push(from_glib_full(*ptr));
ptr = ptr.offset(1);
}
ffi::g_free(ptr as *mut _);
res
}
}
unsafe fn slist_len(mut ptr: *mut ffi::GSList) -> usize {
let mut len = 0;
while !ptr.is_null() {
ptr = (*ptr).next;
len += 1;
}
len
}
impl <P: Ptr, T: FromGlibPtr<P>> FromGlibPtrContainer<P, *mut ffi::GSList> for Vec<T> {
unsafe fn from_glib_none(ptr: *mut ffi::GSList) -> Vec<T> {
let num = slist_len(ptr);
FromGlibPtrContainer::from_glib_none_num(ptr, num)
}
unsafe fn from_glib_none_num(mut ptr: *mut ffi::GSList, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new()
}
let mut res = Vec::with_capacity(num);
while !ptr.is_null() {
let item_ptr: P = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_none(item_ptr));
}
ptr = (*ptr).next;
}
res
}
unsafe fn from_glib_container(ptr: *mut ffi::GSList) -> Vec<T> {
let num = slist_len(ptr);
FromGlibPtrContainer::from_glib_container_num(ptr, num)
}
unsafe fn from_glib_container_num(ptr: *mut ffi::GSList, num: usize) -> Vec<T> {
let res = FromGlibPtrContainer::from_glib_none_num(ptr, num);
if !ptr.is_null() {
ffi::g_slist_free(ptr as *mut _);
}
res
}
unsafe fn from_glib_full(ptr: *mut ffi::GSList) -> Vec<T> {
let num = slist_len(ptr);
FromGlibPtrContainer::from_glib_container_num(ptr, num)
}
unsafe fn from_glib_full_num(mut ptr: *mut ffi::GSList, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new()
}
let orig_ptr = ptr;
let mut res = Vec::with_capacity(num);
while !ptr.is_null() {
let item_ptr: P = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_full(item_ptr));
}
ptr = (*ptr).next;
}
if !orig_ptr.is_null() {
ffi::g_slist_free(orig_ptr as *mut _);
}
res
}
}
unsafe fn list_len(mut ptr: *mut ffi::GList) -> usize {
let mut len = 0;
while !ptr.is_null() {
ptr = (*ptr).next;
len += 1;
}
len
}
impl <P: Ptr, T: FromGlibPtr<P>> FromGlibPtrContainer<P, *mut ffi::GList> for Vec<T> {
unsafe fn from_glib_none(ptr: *mut ffi::GList) -> Vec<T> {
let num = list_len(ptr);
FromGlibPtrContainer::from_glib_none_num(ptr, num)
}
unsafe fn from_glib_none_num(mut ptr: *mut ffi::GList, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new()
}
let mut res = Vec::with_capacity(num);
while !ptr.is_null() {
let item_ptr: P = Ptr::from((*ptr).data);
if !item_ptr.is_null() {
res.push(from_glib_none(item_ptr));
}
ptr = (*ptr).next;
}
res
}
unsafe fn from_glib_container(ptr: *mut ffi::GList) -> Vec<T> {
let num = list_len(ptr);
FromGlibPtrContainer::from_glib_container_num(ptr, num)
}
unsafe fn from_glib_container_num(ptr: *mut ffi::GList, num: usize) -> Vec<T> {
let res = FromGlibPtrContainer::from_glib_none_num(ptr, num);
if !ptr.is_null() {
ffi::g_list_free(ptr as *mut _);
}
res
}
unsafe fn from_glib_full(ptr: *mut ffi::GList) -> Vec<T> {
let num = list_len(ptr);
FromGlibPtrContainer::from_glib_container_num(ptr, num)
}
unsafe fn from_glib_full_num(mut ptr: *mut ffi::GList, num: usize) -> Vec<T> {
if num == 0 || ptr.is_null() {
return Vec::new()
}
let orig_ptr = ptr;
let mut res = Vec::with_capacity(num);
while !ptr.is_null() {
let mut item_ptr: P = mem::uninitialized();
let hack: *mut *mut c_void = mem::transmute(&mut item_ptr);
*hack = (*ptr).data;
if !item_ptr.is_null() {
res.push(from_glib_full(item_ptr));
}
ptr = (*ptr).next;
}
if !orig_ptr.is_null() {
ffi::g_list_free(orig_ptr as *mut _);
}
res
}
}
unsafe extern "C" fn read_string_hash_table(key: ffi::gpointer, value: ffi::gpointer,
hash_map: ffi::gpointer) {
let key: String = from_glib_none(key as *const c_char);
let value: String = from_glib_none(value as *const c_char);
let hash_map: &mut HashMap<String, String> = mem::transmute(hash_map);
hash_map.insert(key, value);
}
impl FromGlibPtrContainer<*const c_char, *mut ffi::GHashTable> for HashMap<String, String> {
unsafe fn from_glib_none(ptr: *mut ffi::GHashTable) -> Self {
let mut map = HashMap::new();
ffi::g_hash_table_foreach(ptr, read_string_hash_table, mem::transmute(&mut map));
map
}
unsafe fn from_glib_none_num(ptr: *mut ffi::GHashTable, _: usize) -> Self {
FromGlibPtrContainer::from_glib_none(ptr)
}
unsafe fn from_glib_container(ptr: *mut ffi::GHashTable) -> Self {
FromGlibPtrContainer::from_glib_full(ptr)
}
unsafe fn from_glib_container_num(ptr: *mut ffi::GHashTable, _: usize) -> Self {
FromGlibPtrContainer::from_glib_full(ptr)
}
unsafe fn from_glib_full(ptr: *mut ffi::GHashTable) -> Self {
let map = FromGlibPtrContainer::from_glib_none(ptr);
ffi::g_hash_table_unref(ptr);
map
}
unsafe fn from_glib_full_num(ptr: *mut ffi::GHashTable, _: usize) -> Self {
FromGlibPtrContainer::from_glib_full(ptr)
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use ffi;
use super::*;
#[test]
fn string_hash_map() {
let mut map = HashMap::new();
map.insert("A".into(), "1".into());
map.insert("B".into(), "2".into());
map.insert("C".into(), "3".into());
let ptr: *mut ffi::GHashTable = map.to_glib_full();
let map = unsafe { HashMap::from_glib_full(ptr) };
assert_eq!(map.get("A"), Some(&"1".into()));
assert_eq!(map.get("B"), Some(&"2".into()));
assert_eq!(map.get("C"), Some(&"3".into()));
}
}