Skip to main content

glib/
match_info.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{GStr, Regex, ffi, prelude::*, translate::*};
4use std::{marker::PhantomData, mem, ptr};
5
6/// A GMatchInfo is an opaque struct used to return information about
7/// matches.
8// rustdoc-stripper-ignore-next-stop
9/// A GMatchInfo is an opaque struct used to return information about
10/// matches.
11#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[repr(transparent)]
13pub struct MatchInfo<'input> {
14    inner: std::ptr::NonNull<ffi::GMatchInfo>,
15    _phantom: PhantomData<&'input GStr>,
16}
17
18impl Clone for MatchInfo<'_> {
19    fn clone(&self) -> Self {
20        unsafe {
21            ffi::g_match_info_ref(self.inner.as_ptr());
22        }
23        Self {
24            inner: self.inner,
25            _phantom: PhantomData,
26        }
27    }
28}
29
30impl Drop for MatchInfo<'_> {
31    fn drop(&mut self) {
32        unsafe {
33            ffi::g_match_info_unref(self.inner.as_ptr());
34        }
35    }
36}
37
38impl MatchInfo<'_> {
39    #[doc = "Return the inner pointer to the underlying C value."]
40    #[inline]
41    pub fn as_ptr(&self) -> *mut ffi::GMatchInfo {
42        self.inner.as_ptr()
43    }
44    #[doc = "Borrows the underlying C value."]
45    #[inline]
46    pub unsafe fn from_glib_ptr_borrow(ptr: &*mut ffi::GMatchInfo) -> &Self {
47        unsafe {
48            debug_assert_eq!(
49                std::mem::size_of::<Self>(),
50                std::mem::size_of::<crate::ffi::gpointer>()
51            );
52            debug_assert!(!ptr.is_null());
53            &*(ptr as *const *mut ffi::GMatchInfo as *const Self)
54        }
55    }
56}
57
58#[doc(hidden)]
59impl GlibPtrDefault for MatchInfo<'_> {
60    type GlibType = *mut ffi::GMatchInfo;
61}
62#[doc(hidden)]
63unsafe impl TransparentPtrType for MatchInfo<'_> {}
64
65#[doc(hidden)]
66impl<'a, 'input> ToGlibPtr<'a, *mut ffi::GMatchInfo> for MatchInfo<'input>
67where
68    'input: 'a,
69{
70    type Storage = PhantomData<&'a Self>;
71    #[inline]
72    fn to_glib_none(&'a self) -> Stash<'a, *mut ffi::GMatchInfo, Self> {
73        Stash(self.inner.as_ptr(), PhantomData)
74    }
75    #[inline]
76    fn to_glib_full(&self) -> *mut ffi::GMatchInfo {
77        let ptr = self.inner.as_ptr();
78        unsafe {
79            ffi::g_match_info_ref(ptr);
80        }
81        ptr
82    }
83}
84#[doc(hidden)]
85impl<'a, 'input> ToGlibPtr<'a, *const ffi::GMatchInfo> for MatchInfo<'input>
86where
87    'input: 'a,
88{
89    type Storage = PhantomData<&'a Self>;
90    #[inline]
91    fn to_glib_none(&'a self) -> Stash<'a, *const ffi::GMatchInfo, Self> {
92        Stash(self.inner.as_ptr(), PhantomData)
93    }
94    #[inline]
95    fn to_glib_full(&self) -> *const ffi::GMatchInfo {
96        let ptr = self.inner.as_ptr();
97        unsafe {
98            ffi::g_match_info_ref(ptr);
99        }
100        ptr
101    }
102}
103
104#[doc(hidden)]
105impl FromGlibPtrNone<*mut ffi::GMatchInfo> for MatchInfo<'_> {
106    #[inline]
107    unsafe fn from_glib_none(ptr: *mut ffi::GMatchInfo) -> Self {
108        debug_assert!(!ptr.is_null());
109        unsafe {
110            ffi::g_match_info_ref(ptr);
111            Self {
112                inner: ptr::NonNull::new_unchecked(ptr),
113                _phantom: PhantomData,
114            }
115        }
116    }
117}
118#[doc(hidden)]
119impl FromGlibPtrNone<*const ffi::GMatchInfo> for MatchInfo<'_> {
120    #[inline]
121    unsafe fn from_glib_none(ptr: *const ffi::GMatchInfo) -> Self {
122        unsafe { Self::from_glib_none(ptr.cast_mut()) }
123    }
124}
125#[doc(hidden)]
126impl FromGlibPtrFull<*mut ffi::GMatchInfo> for MatchInfo<'_> {
127    #[inline]
128    unsafe fn from_glib_full(ptr: *mut ffi::GMatchInfo) -> Self {
129        debug_assert!(!ptr.is_null());
130        unsafe {
131            Self {
132                inner: ptr::NonNull::new_unchecked(ptr),
133                _phantom: PhantomData,
134            }
135        }
136    }
137}
138#[doc(hidden)]
139impl FromGlibPtrBorrow<*mut ffi::GMatchInfo> for MatchInfo<'_> {
140    #[inline]
141    unsafe fn from_glib_borrow(ptr: *mut ffi::GMatchInfo) -> Borrowed<Self> {
142        debug_assert!(!ptr.is_null());
143        unsafe {
144            Borrowed::new(Self {
145                inner: ptr::NonNull::new_unchecked(ptr),
146                _phantom: PhantomData,
147            })
148        }
149    }
150}
151#[doc(hidden)]
152impl FromGlibPtrBorrow<*const ffi::GMatchInfo> for MatchInfo<'_> {
153    #[inline]
154    unsafe fn from_glib_borrow(ptr: *const ffi::GMatchInfo) -> Borrowed<Self> {
155        unsafe { from_glib_borrow::<_, Self>(ptr.cast_mut()) }
156    }
157}
158
159#[doc(hidden)]
160impl IntoGlibPtr<*mut ffi::GMatchInfo> for MatchInfo<'_> {
161    #[inline]
162    fn into_glib_ptr(self) -> *mut ffi::GMatchInfo {
163        let s = std::mem::ManuallyDrop::new(self);
164        ToGlibPtr::<*const ffi::GMatchInfo>::to_glib_none(&*s).0 as *mut _
165    }
166}
167#[doc(hidden)]
168impl IntoGlibPtr<*const ffi::GMatchInfo> for MatchInfo<'_> {
169    #[inline]
170    fn into_glib_ptr(self) -> *const ffi::GMatchInfo {
171        let s = std::mem::ManuallyDrop::new(self);
172        ToGlibPtr::<*const ffi::GMatchInfo>::to_glib_none(&*s).0 as *const _
173    }
174}
175impl StaticType for MatchInfo<'_> {
176    #[inline]
177    fn static_type() -> crate::types::Type {
178        unsafe { from_glib(ffi::g_match_info_get_type()) }
179    }
180}
181
182#[doc(hidden)]
183impl ValueType for MatchInfo<'static> {
184    type Type = Self;
185}
186
187#[doc(hidden)]
188impl crate::value::ValueTypeOptional for MatchInfo<'static> {}
189
190unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for MatchInfo<'input> {
191    type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;
192
193    unsafe fn from_value(value: &'a crate::Value) -> Self {
194        unsafe {
195            let ptr = crate::gobject_ffi::g_value_dup_boxed(
196                crate::translate::ToGlibPtr::to_glib_none(value).0,
197            );
198            debug_assert!(!ptr.is_null());
199            <Self as crate::translate::FromGlibPtrFull<*mut ffi::GMatchInfo>>::from_glib_full(
200                ptr as *mut ffi::GMatchInfo,
201            )
202        }
203    }
204}
205#[doc(hidden)]
206unsafe impl<'a, 'input: 'a> crate::value::FromValue<'a> for &'a MatchInfo<'input> {
207    type Checker = crate::value::GenericValueTypeOrNoneChecker<Self>;
208
209    #[inline]
210    unsafe fn from_value(value: &'a crate::Value) -> Self {
211        unsafe {
212            let value = &*(value as *const crate::Value as *const crate::gobject_ffi::GValue);
213            <MatchInfo<'input>>::from_glib_ptr_borrow(
214                &*(&value.data[0].v_pointer as *const crate::ffi::gpointer
215                    as *const *mut ffi::GMatchInfo),
216            )
217        }
218    }
219}
220impl ToValue for MatchInfo<'static> {
221    #[inline]
222    fn to_value(&self) -> crate::Value {
223        unsafe {
224            let mut value = crate::Value::from_type_unchecked(<Self as StaticType>::static_type());
225            crate::gobject_ffi::g_value_take_boxed(
226                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
227                crate::translate::ToGlibPtr::<*mut ffi::GMatchInfo>::to_glib_full(self) as *mut _,
228            );
229            value
230        }
231    }
232
233    #[inline]
234    fn value_type(&self) -> crate::Type {
235        <Self as StaticType>::static_type()
236    }
237}
238
239impl From<MatchInfo<'static>> for crate::Value {
240    #[inline]
241    fn from(s: MatchInfo<'static>) -> Self {
242        unsafe {
243            let mut value = crate::Value::from_type_unchecked(
244                <MatchInfo<'static> as StaticType>::static_type(),
245            );
246            crate::gobject_ffi::g_value_take_boxed(
247                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
248                crate::translate::IntoGlibPtr::<*mut ffi::GMatchInfo>::into_glib_ptr(s) as *mut _,
249            );
250            value
251        }
252    }
253}
254
255#[doc(hidden)]
256impl crate::value::ToValueOptional for MatchInfo<'static> {
257    #[inline]
258    fn to_value_optional(s: Option<&Self>) -> crate::Value {
259        let mut value = crate::Value::for_value_type::<Self>();
260        unsafe {
261            crate::gobject_ffi::g_value_take_boxed(
262                crate::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
263                crate::translate::ToGlibPtr::<*mut ffi::GMatchInfo>::to_glib_full(&s) as *mut _,
264            );
265        }
266
267        value
268    }
269}
270
271impl HasParamSpec for MatchInfo<'static> {
272    type ParamSpec = crate::ParamSpecBoxed;
273    type SetValue = Self;
274    type BuilderFn = fn(&str) -> crate::ParamSpecBoxedBuilder<Self>;
275
276    fn param_spec_builder() -> Self::BuilderFn {
277        Self::ParamSpec::builder
278    }
279}
280
281impl<'input> MatchInfo<'input> {
282    #[doc(alias = "g_match_info_fetch")]
283    pub fn fetch(&self, match_num: i32) -> Option<crate::GString> {
284        unsafe { from_glib_full(ffi::g_match_info_fetch(self.to_glib_none().0, match_num)) }
285    }
286
287    #[doc(alias = "g_match_info_fetch_all")]
288    pub fn fetch_all(&self) -> Vec<crate::GString> {
289        unsafe {
290            FromGlibPtrContainer::from_glib_full(ffi::g_match_info_fetch_all(self.to_glib_none().0))
291        }
292    }
293
294    #[doc(alias = "g_match_info_fetch_pos")]
295    pub fn fetch_pos(&self, match_num: i32) -> Option<(i32, i32)> {
296        unsafe {
297            let mut start_pos = std::mem::MaybeUninit::uninit();
298            let mut end_pos = std::mem::MaybeUninit::uninit();
299            let ret = from_glib(ffi::g_match_info_fetch_pos(
300                self.to_glib_none().0,
301                match_num,
302                start_pos.as_mut_ptr(),
303                end_pos.as_mut_ptr(),
304            ));
305            if ret {
306                Some((start_pos.assume_init(), end_pos.assume_init()))
307            } else {
308                None
309            }
310        }
311    }
312
313    #[doc(alias = "g_match_info_get_match_count")]
314    #[doc(alias = "get_match_count")]
315    pub fn match_count(&self) -> i32 {
316        unsafe { ffi::g_match_info_get_match_count(self.to_glib_none().0) }
317    }
318
319    #[doc(alias = "g_match_info_get_regex")]
320    #[doc(alias = "get_regex")]
321    pub fn regex(&self) -> Regex {
322        unsafe { from_glib_none(ffi::g_match_info_get_regex(self.to_glib_none().0)) }
323    }
324
325    #[doc(alias = "g_match_info_get_string")]
326    #[doc(alias = "get_string")]
327    pub fn string(&self) -> &'input crate::GStr {
328        unsafe { from_glib_none(ffi::g_match_info_get_string(self.to_glib_none().0)) }
329    }
330
331    #[doc(alias = "g_match_info_is_partial_match")]
332    pub fn is_partial_match(&self) -> bool {
333        unsafe { from_glib(ffi::g_match_info_is_partial_match(self.to_glib_none().0)) }
334    }
335
336    #[doc(alias = "g_match_info_matches")]
337    pub fn matches(&self) -> bool {
338        unsafe { from_glib(ffi::g_match_info_matches(self.to_glib_none().0)) }
339    }
340
341    #[doc(alias = "g_match_info_next")]
342    pub fn next(&self) -> Result<bool, crate::Error> {
343        unsafe {
344            let mut error = std::ptr::null_mut();
345            let is_ok = ffi::g_match_info_next(self.to_glib_none().0, &mut error);
346            if !error.is_null() {
347                Err(from_glib_full(error))
348            } else {
349                Ok(from_glib(is_ok))
350            }
351        }
352    }
353
354    #[doc(alias = "g_match_info_expand_references")]
355    pub fn expand_references(
356        &self,
357        string_to_expand: impl IntoGStr,
358    ) -> Result<Option<crate::GString>, crate::Error> {
359        string_to_expand.run_with_gstr(|string_to_expand| unsafe {
360            let mut error = ptr::null_mut();
361            let ret = ffi::g_match_info_expand_references(
362                self.to_glib_none().0,
363                string_to_expand.to_glib_none().0,
364                &mut error,
365            );
366            if error.is_null() {
367                Ok(from_glib_full(ret))
368            } else {
369                Err(from_glib_full(error))
370            }
371        })
372    }
373
374    #[doc(alias = "g_match_info_fetch_named")]
375    pub fn fetch_named(&self, name: impl IntoGStr) -> Option<crate::GString> {
376        name.run_with_gstr(|name| unsafe {
377            from_glib_full(ffi::g_match_info_fetch_named(
378                self.to_glib_none().0,
379                name.to_glib_none().0,
380            ))
381        })
382    }
383
384    #[doc(alias = "g_match_info_fetch_named_pos")]
385    pub fn fetch_named_pos(&self, name: impl IntoGStr) -> Option<(i32, i32)> {
386        name.run_with_gstr(|name| unsafe {
387            let mut start_pos = mem::MaybeUninit::uninit();
388            let mut end_pos = mem::MaybeUninit::uninit();
389            let ret = from_glib(ffi::g_match_info_fetch_named_pos(
390                self.to_glib_none().0,
391                name.to_glib_none().0,
392                start_pos.as_mut_ptr(),
393                end_pos.as_mut_ptr(),
394            ));
395            if ret {
396                Some((start_pos.assume_init(), end_pos.assume_init()))
397            } else {
398                None
399            }
400        })
401    }
402}