Skip to content

Commit fbfffb5

Browse files
authored
Merge pull request #950 from pbor/stringptr
Rename GStrPtr to GStringPtr
2 parents e842054 + 3aaff7e commit fbfffb5

File tree

3 files changed

+133
-146
lines changed

3 files changed

+133
-146
lines changed

glib/src/collections/strv.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::{ffi::c_char, fmt, marker::PhantomData, mem, ptr};
44

5-
use crate::{translate::*, GStr, GStrPtr, GString};
5+
use crate::{translate::*, GStr, GString, GStringPtr};
66

77
// rustdoc-stripper-ignore-next
88
/// Minimum size of the `StrV` allocation.
@@ -100,25 +100,25 @@ impl Default for StrV {
100100
}
101101
}
102102

103-
impl AsRef<[GStrPtr]> for StrV {
103+
impl AsRef<[GStringPtr]> for StrV {
104104
#[inline]
105-
fn as_ref(&self) -> &[GStrPtr] {
105+
fn as_ref(&self) -> &[GStringPtr] {
106106
self.as_slice()
107107
}
108108
}
109109

110-
impl std::borrow::Borrow<[GStrPtr]> for StrV {
110+
impl std::borrow::Borrow<[GStringPtr]> for StrV {
111111
#[inline]
112-
fn borrow(&self) -> &[GStrPtr] {
112+
fn borrow(&self) -> &[GStringPtr] {
113113
self.as_slice()
114114
}
115115
}
116116

117117
impl std::ops::Deref for StrV {
118-
type Target = [GStrPtr];
118+
type Target = [GStringPtr];
119119

120120
#[inline]
121-
fn deref(&self) -> &[GStrPtr] {
121+
fn deref(&self) -> &[GStringPtr] {
122122
self.as_slice()
123123
}
124124
}
@@ -160,8 +160,8 @@ impl std::iter::FromIterator<GString> for StrV {
160160
}
161161

162162
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>;
165165

166166
#[inline]
167167
fn into_iter(self) -> Self::IntoIter {
@@ -201,12 +201,12 @@ impl IntoIter {
201201
// rustdoc-stripper-ignore-next
202202
/// Returns the remaining items as slice.
203203
#[inline]
204-
pub fn as_slice(&self) -> &[GStrPtr] {
204+
pub const fn as_slice(&self) -> &[GStringPtr] {
205205
unsafe {
206206
if self.len == 0 {
207207
&[]
208208
} 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)
210210
}
211211
}
212212
}
@@ -394,7 +394,7 @@ impl Clone for StrV {
394394
unsafe {
395395
let mut s = Self::with_capacity(self.len());
396396
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();
398398
}
399399
s.len = self.len();
400400
*s.ptr.as_ptr().add(s.len) = ptr::null_mut();
@@ -407,7 +407,7 @@ impl StrV {
407407
// rustdoc-stripper-ignore-next
408408
/// Borrows a C array.
409409
#[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] {
411411
let mut len = 0;
412412
if !ptr.is_null() {
413413
while !(*ptr.add(len)).is_null() {
@@ -420,13 +420,16 @@ impl StrV {
420420
// rustdoc-stripper-ignore-next
421421
/// Borrows a C array.
422422
#[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] {
424427
debug_assert!(!ptr.is_null() || len == 0);
425428

426429
if len == 0 {
427430
&[]
428431
} else {
429-
std::slice::from_raw_parts(ptr as *const GStrPtr, len)
432+
std::slice::from_raw_parts(ptr as *const GStringPtr, len)
430433
}
431434
}
432435

@@ -443,11 +446,23 @@ impl StrV {
443446
if len == 0 {
444447
StrV::default()
445448
} 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+
}
451466
}
452467
}
453468

@@ -675,14 +690,14 @@ impl StrV {
675690
}
676691

677692
// rustdoc-stripper-ignore-next
678-
/// Borrows this slice as a `&[GStrPtr]`.
693+
/// Borrows this slice as a `&[GStringPtr]`.
679694
#[inline]
680-
pub fn as_slice(&self) -> &[GStrPtr] {
695+
pub const fn as_slice(&self) -> &[GStringPtr] {
681696
unsafe {
682697
if self.len == 0 {
683698
&[]
684699
} 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)
686701
}
687702
}
688703
}
@@ -948,7 +963,7 @@ impl crate::StaticType for StrV {
948963
}
949964
}
950965

951-
impl<'a> crate::StaticType for &'a [GStrPtr] {
966+
impl<'a> crate::StaticType for &'a [GStringPtr] {
952967
#[inline]
953968
fn static_type() -> crate::Type {
954969
<Vec<String>>::static_type()
@@ -968,7 +983,7 @@ unsafe impl<'a> crate::value::FromValue<'a> for StrV {
968983
}
969984
}
970985

971-
unsafe impl<'a> crate::value::FromValue<'a> for &'a [GStrPtr] {
986+
unsafe impl<'a> crate::value::FromValue<'a> for &'a [GStringPtr] {
972987
type Checker = crate::value::GenericValueTypeChecker<Self>;
973988

974989
unsafe fn from_value(value: &'a crate::value::Value) -> Self {

0 commit comments

Comments
 (0)