2
2
3
3
use std:: { ffi:: c_char, fmt, marker:: PhantomData , mem, ptr} ;
4
4
5
- use crate :: { translate:: * , GStr , GStrPtr , GString } ;
5
+ use crate :: { translate:: * , GStr , GString , GStringPtr } ;
6
6
7
7
// rustdoc-stripper-ignore-next
8
8
/// Minimum size of the `StrV` allocation.
@@ -100,25 +100,25 @@ impl Default for StrV {
100
100
}
101
101
}
102
102
103
- impl AsRef < [ GStrPtr ] > for StrV {
103
+ impl AsRef < [ GStringPtr ] > for StrV {
104
104
#[ inline]
105
- fn as_ref ( & self ) -> & [ GStrPtr ] {
105
+ fn as_ref ( & self ) -> & [ GStringPtr ] {
106
106
self . as_slice ( )
107
107
}
108
108
}
109
109
110
- impl std:: borrow:: Borrow < [ GStrPtr ] > for StrV {
110
+ impl std:: borrow:: Borrow < [ GStringPtr ] > for StrV {
111
111
#[ inline]
112
- fn borrow ( & self ) -> & [ GStrPtr ] {
112
+ fn borrow ( & self ) -> & [ GStringPtr ] {
113
113
self . as_slice ( )
114
114
}
115
115
}
116
116
117
117
impl std:: ops:: Deref for StrV {
118
- type Target = [ GStrPtr ] ;
118
+ type Target = [ GStringPtr ] ;
119
119
120
120
#[ inline]
121
- fn deref ( & self ) -> & [ GStrPtr ] {
121
+ fn deref ( & self ) -> & [ GStringPtr ] {
122
122
self . as_slice ( )
123
123
}
124
124
}
@@ -160,8 +160,8 @@ impl std::iter::FromIterator<GString> for StrV {
160
160
}
161
161
162
162
impl < ' a > std:: iter:: IntoIterator for & ' a StrV {
163
- type Item = & ' a GStrPtr ;
164
- type IntoIter = std:: slice:: Iter < ' a , GStrPtr > ;
163
+ type Item = & ' a GStringPtr ;
164
+ type IntoIter = std:: slice:: Iter < ' a , GStringPtr > ;
165
165
166
166
#[ inline]
167
167
fn into_iter ( self ) -> Self :: IntoIter {
@@ -201,12 +201,12 @@ impl IntoIter {
201
201
// rustdoc-stripper-ignore-next
202
202
/// Returns the remaining items as slice.
203
203
#[ inline]
204
- pub fn as_slice ( & self ) -> & [ GStrPtr ] {
204
+ pub const fn as_slice ( & self ) -> & [ GStringPtr ] {
205
205
unsafe {
206
206
if self . len == 0 {
207
207
& [ ]
208
208
} else {
209
- std:: slice:: from_raw_parts ( self . idx . as_ptr ( ) as * const GStrPtr , self . len )
209
+ std:: slice:: from_raw_parts ( self . idx . as_ptr ( ) as * const GStringPtr , self . len )
210
210
}
211
211
}
212
212
}
@@ -394,7 +394,7 @@ impl Clone for StrV {
394
394
unsafe {
395
395
let mut s = Self :: with_capacity ( self . len ( ) ) ;
396
396
for ( i, item) in self . iter ( ) . enumerate ( ) {
397
- * s. ptr . as_ptr ( ) . add ( i) = GString :: from ( item. as_str ( ) ) . into_glib_ptr ( ) ;
397
+ * s. ptr . as_ptr ( ) . add ( i) = GString :: from ( item. to_str ( ) ) . into_glib_ptr ( ) ;
398
398
}
399
399
s. len = self . len ( ) ;
400
400
* s. ptr . as_ptr ( ) . add ( s. len ) = ptr:: null_mut ( ) ;
@@ -407,7 +407,7 @@ impl StrV {
407
407
// rustdoc-stripper-ignore-next
408
408
/// Borrows a C array.
409
409
#[ inline]
410
- pub unsafe fn from_glib_borrow < ' a > ( ptr : * const * const c_char ) -> & ' a [ GStrPtr ] {
410
+ pub unsafe fn from_glib_borrow < ' a > ( ptr : * const * const c_char ) -> & ' a [ GStringPtr ] {
411
411
let mut len = 0 ;
412
412
if !ptr. is_null ( ) {
413
413
while !( * ptr. add ( len) ) . is_null ( ) {
@@ -420,13 +420,16 @@ impl StrV {
420
420
// rustdoc-stripper-ignore-next
421
421
/// Borrows a C array.
422
422
#[ inline]
423
- pub unsafe fn from_glib_borrow_num < ' a > ( ptr : * const * const c_char , len : usize ) -> & ' a [ GStrPtr ] {
423
+ pub unsafe fn from_glib_borrow_num < ' a > (
424
+ ptr : * const * const c_char ,
425
+ len : usize ,
426
+ ) -> & ' a [ GStringPtr ] {
424
427
debug_assert ! ( !ptr. is_null( ) || len == 0 ) ;
425
428
426
429
if len == 0 {
427
430
& [ ]
428
431
} else {
429
- std:: slice:: from_raw_parts ( ptr as * const GStrPtr , len)
432
+ std:: slice:: from_raw_parts ( ptr as * const GStringPtr , len)
430
433
}
431
434
}
432
435
@@ -443,11 +446,23 @@ impl StrV {
443
446
if len == 0 {
444
447
StrV :: default ( )
445
448
} else {
446
- // Need to fully copy the array here.
447
- let slice = Self :: from_glib_borrow_num ( ptr, len) ;
448
- let mut s = Self :: new ( ) ;
449
- s. extend_from_slice ( slice) ;
450
- s
449
+ let new_ptr =
450
+ ffi:: g_malloc ( mem:: size_of :: < * mut c_char > ( ) * len + 1 ) as * mut * mut c_char ;
451
+
452
+ // Need to clone every item because we don't own it here
453
+ for i in 0 ..len {
454
+ let p = ptr. add ( i) as * mut * const c_char ;
455
+ let q = new_ptr. add ( i) as * mut * const c_char ;
456
+ * q = ffi:: g_strdup ( * p) ;
457
+ }
458
+
459
+ * new_ptr. add ( len) = ptr:: null_mut ( ) ;
460
+
461
+ StrV {
462
+ ptr : ptr:: NonNull :: new_unchecked ( new_ptr) ,
463
+ len,
464
+ capacity : len + 1 ,
465
+ }
451
466
}
452
467
}
453
468
@@ -675,14 +690,14 @@ impl StrV {
675
690
}
676
691
677
692
// rustdoc-stripper-ignore-next
678
- /// Borrows this slice as a `&[GStrPtr ]`.
693
+ /// Borrows this slice as a `&[GStringPtr ]`.
679
694
#[ inline]
680
- pub fn as_slice ( & self ) -> & [ GStrPtr ] {
695
+ pub const fn as_slice ( & self ) -> & [ GStringPtr ] {
681
696
unsafe {
682
697
if self . len == 0 {
683
698
& [ ]
684
699
} else {
685
- std:: slice:: from_raw_parts ( self . ptr . as_ptr ( ) as * const GStrPtr , self . len )
700
+ std:: slice:: from_raw_parts ( self . ptr . as_ptr ( ) as * const GStringPtr , self . len )
686
701
}
687
702
}
688
703
}
@@ -948,7 +963,7 @@ impl crate::StaticType for StrV {
948
963
}
949
964
}
950
965
951
- impl < ' a > crate :: StaticType for & ' a [ GStrPtr ] {
966
+ impl < ' a > crate :: StaticType for & ' a [ GStringPtr ] {
952
967
#[ inline]
953
968
fn static_type ( ) -> crate :: Type {
954
969
<Vec < String > >:: static_type ( )
@@ -968,7 +983,7 @@ unsafe impl<'a> crate::value::FromValue<'a> for StrV {
968
983
}
969
984
}
970
985
971
- unsafe impl < ' a > crate :: value:: FromValue < ' a > for & ' a [ GStrPtr ] {
986
+ unsafe impl < ' a > crate :: value:: FromValue < ' a > for & ' a [ GStringPtr ] {
972
987
type Checker = crate :: value:: GenericValueTypeChecker < Self > ;
973
988
974
989
unsafe fn from_value ( value : & ' a crate :: value:: Value ) -> Self {
0 commit comments