Skip to content

Commit 94ff272

Browse files
committed
Rename StrVItem to GStrPtr and make it clonable and transparent
It is useful to have also in cases different from StrV, for instance for some API we want to return a Slice of GStrPtr.
1 parent 22a91c1 commit 94ff272

File tree

4 files changed

+286
-265
lines changed

4 files changed

+286
-265
lines changed

glib/src/collections/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ pub mod slist;
1313
pub use slist::SList;
1414

1515
pub mod strv;
16-
pub use strv::{StrV, StrVItem};
16+
pub use strv::StrV;

glib/src/collections/strv.rs

Lines changed: 19 additions & 263 deletions
Original file line numberDiff line numberDiff line change
@@ -2,248 +2,7 @@
22

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

5-
use crate::{translate::*, GStr, GString};
6-
7-
// rustdoc-stripper-ignore-next
8-
/// `NULL`-terminated UTF-8 string as stored in [`StrV`].
9-
///
10-
/// Unlike [`&GStr`] this does not have its length stored.
11-
#[repr(transparent)]
12-
pub struct StrVItem(ptr::NonNull<c_char>);
13-
14-
impl fmt::Debug for StrVItem {
15-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16-
f.write_str(self.as_str())
17-
}
18-
}
19-
20-
impl std::ops::Deref for StrVItem {
21-
type Target = GStr;
22-
23-
#[inline]
24-
fn deref(&self) -> &Self::Target {
25-
self.as_ref()
26-
}
27-
}
28-
29-
impl AsRef<GStr> for StrVItem {
30-
#[inline]
31-
fn as_ref(&self) -> &GStr {
32-
unsafe { GStr::from_ptr(self.0.as_ptr()) }
33-
}
34-
}
35-
36-
impl AsRef<str> for StrVItem {
37-
#[inline]
38-
fn as_ref(&self) -> &str {
39-
self.as_str()
40-
}
41-
}
42-
43-
impl fmt::Display for StrVItem {
44-
#[inline]
45-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46-
f.write_str(self.as_str())
47-
}
48-
}
49-
50-
impl Eq for StrVItem {}
51-
52-
impl PartialEq for StrVItem {
53-
#[inline]
54-
fn eq(&self, other: &StrVItem) -> bool {
55-
self.as_str() == other.as_str()
56-
}
57-
}
58-
59-
impl PartialEq<StrVItem> for String {
60-
#[inline]
61-
fn eq(&self, other: &StrVItem) -> bool {
62-
self.as_str() == other.as_str()
63-
}
64-
}
65-
66-
impl PartialEq<StrVItem> for GString {
67-
#[inline]
68-
fn eq(&self, other: &StrVItem) -> bool {
69-
self.as_str() == other.as_str()
70-
}
71-
}
72-
73-
impl PartialEq<str> for StrVItem {
74-
#[inline]
75-
fn eq(&self, other: &str) -> bool {
76-
self.as_str() == other
77-
}
78-
}
79-
80-
impl PartialEq<&str> for StrVItem {
81-
#[inline]
82-
fn eq(&self, other: &&str) -> bool {
83-
self.as_str() == *other
84-
}
85-
}
86-
87-
impl PartialEq<GStr> for StrVItem {
88-
#[inline]
89-
fn eq(&self, other: &GStr) -> bool {
90-
self.as_str() == other
91-
}
92-
}
93-
94-
impl PartialEq<&GStr> for StrVItem {
95-
#[inline]
96-
fn eq(&self, other: &&GStr) -> bool {
97-
self.as_str() == *other
98-
}
99-
}
100-
101-
impl PartialEq<StrVItem> for &str {
102-
#[inline]
103-
fn eq(&self, other: &StrVItem) -> bool {
104-
*self == other.as_str()
105-
}
106-
}
107-
108-
impl PartialEq<StrVItem> for &GStr {
109-
#[inline]
110-
fn eq(&self, other: &StrVItem) -> bool {
111-
*self == other.as_str()
112-
}
113-
}
114-
115-
impl PartialEq<String> for StrVItem {
116-
#[inline]
117-
fn eq(&self, other: &String) -> bool {
118-
self.as_str() == other.as_str()
119-
}
120-
}
121-
122-
impl PartialEq<GString> for StrVItem {
123-
#[inline]
124-
fn eq(&self, other: &GString) -> bool {
125-
self.as_str() == other.as_str()
126-
}
127-
}
128-
129-
impl PartialEq<StrVItem> for str {
130-
#[inline]
131-
fn eq(&self, other: &StrVItem) -> bool {
132-
self == other.as_str()
133-
}
134-
}
135-
136-
impl PartialEq<StrVItem> for GStr {
137-
#[inline]
138-
fn eq(&self, other: &StrVItem) -> bool {
139-
self == other.as_str()
140-
}
141-
}
142-
143-
impl PartialOrd<StrVItem> for StrVItem {
144-
#[inline]
145-
fn partial_cmp(&self, other: &StrVItem) -> Option<std::cmp::Ordering> {
146-
Some(self.as_str().cmp(other.as_str()))
147-
}
148-
}
149-
150-
impl Ord for StrVItem {
151-
#[inline]
152-
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
153-
self.as_str().cmp(other.as_str())
154-
}
155-
}
156-
157-
impl PartialOrd<StrVItem> for String {
158-
#[inline]
159-
fn partial_cmp(&self, other: &StrVItem) -> Option<std::cmp::Ordering> {
160-
Some(self.as_str().cmp(other.as_str()))
161-
}
162-
}
163-
164-
impl PartialOrd<StrVItem> for GString {
165-
#[inline]
166-
fn partial_cmp(&self, other: &StrVItem) -> Option<std::cmp::Ordering> {
167-
Some(self.as_str().cmp(other.as_str()))
168-
}
169-
}
170-
171-
impl PartialOrd<String> for StrVItem {
172-
#[inline]
173-
fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
174-
Some(self.as_str().cmp(other.as_str()))
175-
}
176-
}
177-
178-
impl PartialOrd<GString> for StrVItem {
179-
#[inline]
180-
fn partial_cmp(&self, other: &GString) -> Option<std::cmp::Ordering> {
181-
Some(self.as_str().cmp(other.as_str()))
182-
}
183-
}
184-
185-
impl PartialOrd<StrVItem> for str {
186-
#[inline]
187-
fn partial_cmp(&self, other: &StrVItem) -> Option<std::cmp::Ordering> {
188-
Some(self.cmp(other.as_str()))
189-
}
190-
}
191-
192-
impl PartialOrd<StrVItem> for GStr {
193-
#[inline]
194-
fn partial_cmp(&self, other: &StrVItem) -> Option<std::cmp::Ordering> {
195-
Some(self.as_str().cmp(other.as_str()))
196-
}
197-
}
198-
199-
impl PartialOrd<str> for StrVItem {
200-
#[inline]
201-
fn partial_cmp(&self, other: &str) -> Option<std::cmp::Ordering> {
202-
Some(self.as_str().cmp(other))
203-
}
204-
}
205-
206-
impl PartialOrd<GStr> for StrVItem {
207-
#[inline]
208-
fn partial_cmp(&self, other: &GStr) -> Option<std::cmp::Ordering> {
209-
Some(self.as_str().cmp(other))
210-
}
211-
}
212-
213-
impl AsRef<StrVItem> for StrVItem {
214-
#[inline]
215-
fn as_ref(&self) -> &StrVItem {
216-
self
217-
}
218-
}
219-
220-
impl AsRef<std::ffi::OsStr> for StrVItem {
221-
#[inline]
222-
fn as_ref(&self) -> &std::ffi::OsStr {
223-
self.as_str().as_ref()
224-
}
225-
}
226-
227-
impl AsRef<std::path::Path> for StrVItem {
228-
#[inline]
229-
fn as_ref(&self) -> &std::path::Path {
230-
self.as_str().as_ref()
231-
}
232-
}
233-
234-
impl AsRef<[u8]> for StrVItem {
235-
#[inline]
236-
fn as_ref(&self) -> &[u8] {
237-
self.as_bytes()
238-
}
239-
}
240-
241-
impl std::hash::Hash for StrVItem {
242-
#[inline]
243-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
244-
self.as_str().hash(state);
245-
}
246-
}
5+
use crate::{translate::*, GStr, GStrPtr, GString};
2476

2487
// rustdoc-stripper-ignore-next
2498
/// Minimum size of the `StrV` allocation.
@@ -341,25 +100,25 @@ impl Default for StrV {
341100
}
342101
}
343102

344-
impl AsRef<[StrVItem]> for StrV {
103+
impl AsRef<[GStrPtr]> for StrV {
345104
#[inline]
346-
fn as_ref(&self) -> &[StrVItem] {
105+
fn as_ref(&self) -> &[GStrPtr] {
347106
self.as_slice()
348107
}
349108
}
350109

351-
impl std::borrow::Borrow<[StrVItem]> for StrV {
110+
impl std::borrow::Borrow<[GStrPtr]> for StrV {
352111
#[inline]
353-
fn borrow(&self) -> &[StrVItem] {
112+
fn borrow(&self) -> &[GStrPtr] {
354113
self.as_slice()
355114
}
356115
}
357116

358117
impl std::ops::Deref for StrV {
359-
type Target = [StrVItem];
118+
type Target = [GStrPtr];
360119

361120
#[inline]
362-
fn deref(&self) -> &[StrVItem] {
121+
fn deref(&self) -> &[GStrPtr] {
363122
self.as_slice()
364123
}
365124
}
@@ -401,8 +160,8 @@ impl std::iter::FromIterator<GString> for StrV {
401160
}
402161

403162
impl<'a> std::iter::IntoIterator for &'a StrV {
404-
type Item = &'a StrVItem;
405-
type IntoIter = std::slice::Iter<'a, StrVItem>;
163+
type Item = &'a GStrPtr;
164+
type IntoIter = std::slice::Iter<'a, GStrPtr>;
406165

407166
#[inline]
408167
fn into_iter(self) -> Self::IntoIter {
@@ -442,12 +201,12 @@ impl IntoIter {
442201
// rustdoc-stripper-ignore-next
443202
/// Returns the remaining items as slice.
444203
#[inline]
445-
pub fn as_slice(&self) -> &[StrVItem] {
204+
pub fn as_slice(&self) -> &[GStrPtr] {
446205
unsafe {
447206
if self.len == 0 {
448207
&[]
449208
} else {
450-
std::slice::from_raw_parts(self.idx.as_ptr() as *const StrVItem, self.len)
209+
std::slice::from_raw_parts(self.idx.as_ptr() as *const GStrPtr, self.len)
451210
}
452211
}
453212
}
@@ -648,7 +407,7 @@ impl StrV {
648407
// rustdoc-stripper-ignore-next
649408
/// Borrows a C array.
650409
#[inline]
651-
pub unsafe fn from_glib_borrow<'a>(ptr: *const *const c_char) -> &'a [StrVItem] {
410+
pub unsafe fn from_glib_borrow<'a>(ptr: *const *const c_char) -> &'a [GStrPtr] {
652411
let mut len = 0;
653412
if !ptr.is_null() {
654413
while !(*ptr.add(len)).is_null() {
@@ -661,16 +420,13 @@ impl StrV {
661420
// rustdoc-stripper-ignore-next
662421
/// Borrows a C array.
663422
#[inline]
664-
pub unsafe fn from_glib_borrow_num<'a>(
665-
ptr: *const *const c_char,
666-
len: usize,
667-
) -> &'a [StrVItem] {
423+
pub unsafe fn from_glib_borrow_num<'a>(ptr: *const *const c_char, len: usize) -> &'a [GStrPtr] {
668424
debug_assert!(!ptr.is_null() || len == 0);
669425

670426
if len == 0 {
671427
&[]
672428
} else {
673-
std::slice::from_raw_parts(ptr as *const StrVItem, len)
429+
std::slice::from_raw_parts(ptr as *const GStrPtr, len)
674430
}
675431
}
676432

@@ -919,14 +675,14 @@ impl StrV {
919675
}
920676

921677
// rustdoc-stripper-ignore-next
922-
/// Borrows this slice as a `&[StrVItem]`.
678+
/// Borrows this slice as a `&[GStrPtr]`.
923679
#[inline]
924-
pub fn as_slice(&self) -> &[StrVItem] {
680+
pub fn as_slice(&self) -> &[GStrPtr] {
925681
unsafe {
926682
if self.len == 0 {
927683
&[]
928684
} else {
929-
std::slice::from_raw_parts(self.ptr.as_ptr() as *const StrVItem, self.len)
685+
std::slice::from_raw_parts(self.ptr.as_ptr() as *const GStrPtr, self.len)
930686
}
931687
}
932688
}
@@ -1192,7 +948,7 @@ impl crate::StaticType for StrV {
1192948
}
1193949
}
1194950

1195-
impl<'a> crate::StaticType for &'a [StrVItem] {
951+
impl<'a> crate::StaticType for &'a [GStrPtr] {
1196952
#[inline]
1197953
fn static_type() -> crate::Type {
1198954
<Vec<String>>::static_type()
@@ -1212,7 +968,7 @@ unsafe impl<'a> crate::value::FromValue<'a> for StrV {
1212968
}
1213969
}
1214970

1215-
unsafe impl<'a> crate::value::FromValue<'a> for &'a [StrVItem] {
971+
unsafe impl<'a> crate::value::FromValue<'a> for &'a [GStrPtr] {
1216972
type Checker = crate::value::GenericValueTypeChecker<Self>;
1217973

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

0 commit comments

Comments
 (0)