json_glib/auto/array.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir
3// from gtk-girs (https://github.com/gtk-rs/gir-files)
4// DO NOT EDIT
5
6use crate::{ffi,Node,Object};
7use glib::{translate::*};
8
9glib::wrapper! {
10 /// `JsonArray` is the representation of the array type inside JSON.
11 ///
12 /// A `JsonArray` contains [`Node`][crate::Node] elements, which may contain
13 /// fundamental types, other arrays or objects.
14 ///
15 /// Since arrays can be arbitrarily big, copying them can be expensive; for
16 /// this reason, they are reference counted. You can control the lifetime of
17 /// a `JsonArray` using `Json::Array::ref()` and `Json::Array::unref()`.
18 ///
19 /// To append an element, use [`add_element()`][Self::add_element()].
20 ///
21 /// To extract an element at a given index, use [`element()`][Self::element()].
22 ///
23 /// To retrieve the entire array in list form, use [`elements()`][Self::elements()].
24 ///
25 /// To retrieve the length of the array, use [`length()`][Self::length()].
26 #[derive(Debug, PartialOrd, Ord)]
27 pub struct Array(Shared<ffi::JsonArray>);
28
29 match fn {
30 ref => |ptr| ffi::json_array_ref(ptr),
31 unref => |ptr| ffi::json_array_unref(ptr),
32 type_ => || ffi::json_array_get_type(),
33 }
34}
35
36impl Array {
37 /// Creates a new array.
38 ///
39 /// # Returns
40 ///
41 /// the newly created array
42 #[doc(alias = "json_array_new")]
43 pub fn new() -> Array {
44 assert_initialized_main_thread!();
45 unsafe {
46 from_glib_full(ffi::json_array_new())
47 }
48 }
49
50 /// Creates a new array with `n_elements` slots already allocated.
51 /// ## `n_elements`
52 /// number of slots to pre-allocate
53 ///
54 /// # Returns
55 ///
56 /// the newly created array
57 #[doc(alias = "json_array_sized_new")]
58 pub fn sized_new(n_elements: u32) -> Array {
59 assert_initialized_main_thread!();
60 unsafe {
61 from_glib_full(ffi::json_array_sized_new(n_elements))
62 }
63 }
64
65 /// Conveniently adds an array element into an array.
66 ///
67 /// If `value` is `NULL`, a `null` element will be added instead.
68 ///
69 /// See also: [`add_element()`][Self::add_element()], [`Node::take_array()`][crate::Node::take_array()]
70 /// ## `value`
71 /// the array to add
72 #[doc(alias = "json_array_add_array_element")]
73 pub fn add_array_element(&self, value: Option<Array>) {
74 unsafe {
75 ffi::json_array_add_array_element(self.to_glib_none().0, value.into_glib_ptr());
76 }
77 }
78
79 /// Conveniently adds the given boolean value into an array.
80 ///
81 /// See also: [`add_element()`][Self::add_element()], [`Node::set_boolean()`][crate::Node::set_boolean()]
82 /// ## `value`
83 /// the boolean value to add
84 #[doc(alias = "json_array_add_boolean_element")]
85 pub fn add_boolean_element(&self, value: bool) {
86 unsafe {
87 ffi::json_array_add_boolean_element(self.to_glib_none().0, value.into_glib());
88 }
89 }
90
91 /// Conveniently adds the given floating point value into an array.
92 ///
93 /// See also: [`add_element()`][Self::add_element()], [`Node::set_double()`][crate::Node::set_double()]
94 /// ## `value`
95 /// the floating point value to add
96 #[doc(alias = "json_array_add_double_element")]
97 pub fn add_double_element(&self, value: f64) {
98 unsafe {
99 ffi::json_array_add_double_element(self.to_glib_none().0, value);
100 }
101 }
102
103 /// Appends the given `node` inside an array.
104 /// ## `node`
105 /// the element to add
106 #[doc(alias = "json_array_add_element")]
107 pub fn add_element(&self, node: Node) {
108 unsafe {
109 ffi::json_array_add_element(self.to_glib_none().0, node.into_glib_ptr());
110 }
111 }
112
113 /// Conveniently adds the given integer value into an array.
114 ///
115 /// See also: [`add_element()`][Self::add_element()], [`Node::set_int()`][crate::Node::set_int()]
116 /// ## `value`
117 /// the integer value to add
118 #[doc(alias = "json_array_add_int_element")]
119 pub fn add_int_element(&self, value: i64) {
120 unsafe {
121 ffi::json_array_add_int_element(self.to_glib_none().0, value);
122 }
123 }
124
125 /// Conveniently adds a `null` element into an array
126 ///
127 /// See also: [`add_element()`][Self::add_element()], `JSON_NODE_NULL`
128 #[doc(alias = "json_array_add_null_element")]
129 pub fn add_null_element(&self) {
130 unsafe {
131 ffi::json_array_add_null_element(self.to_glib_none().0);
132 }
133 }
134
135 /// Conveniently adds an object into an array.
136 ///
137 /// If `value` is `NULL`, a `null` element will be added instead.
138 ///
139 /// See also: [`add_element()`][Self::add_element()], [`Node::take_object()`][crate::Node::take_object()]
140 /// ## `value`
141 /// the object to add
142 #[doc(alias = "json_array_add_object_element")]
143 pub fn add_object_element(&self, value: Option<Object>) {
144 unsafe {
145 ffi::json_array_add_object_element(self.to_glib_none().0, value.into_glib_ptr());
146 }
147 }
148
149 /// Conveniently adds the given string value into an array.
150 ///
151 /// See also: [`add_element()`][Self::add_element()], [`Node::set_string()`][crate::Node::set_string()]
152 /// ## `value`
153 /// the string value to add
154 #[doc(alias = "json_array_add_string_element")]
155 pub fn add_string_element(&self, value: &str) {
156 unsafe {
157 ffi::json_array_add_string_element(self.to_glib_none().0, value.to_glib_none().0);
158 }
159 }
160
161 /// Retrieves a copy of the element at the given position in the array.
162 /// ## `index_`
163 /// the index of the element to retrieve
164 ///
165 /// # Returns
166 ///
167 /// a copy of the element at the given position
168 #[doc(alias = "json_array_dup_element")]
169 pub fn dup_element(&self, index_: u32) -> Node {
170 unsafe {
171 from_glib_full(ffi::json_array_dup_element(self.to_glib_none().0, index_))
172 }
173 }
174
175 #[cfg(feature = "v1_2")]
176 #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
177 #[doc(alias = "json_array_equal")]
178 fn equal(&self, b: &Array) -> bool {
179 unsafe {
180 from_glib(ffi::json_array_equal(ToGlibPtr::<*mut ffi::JsonArray>::to_glib_none(self).0 as glib::ffi::gconstpointer, ToGlibPtr::<*mut ffi::JsonArray>::to_glib_none(b).0 as glib::ffi::gconstpointer))
181 }
182 }
183
184 /// Iterates over all elements of an array, and calls a function on
185 /// each one of them.
186 ///
187 /// It is safe to change the value of an element of the array while
188 /// iterating over it, but it is not safe to add or remove elements
189 /// from the array.
190 /// ## `func`
191 /// the function to be called on each element
192 #[doc(alias = "json_array_foreach_element")]
193 pub fn foreach_element<P: FnMut(&Array, u32, &Node)>(&self, func: P) {
194 let mut func_data: P = func;
195 unsafe extern "C" fn func_func<P: FnMut(&Array, u32, &Node)>(array: *mut ffi::JsonArray, index_: std::ffi::c_uint, element_node: *mut ffi::JsonNode, user_data: glib::ffi::gpointer) {
196 let array = from_glib_borrow(array);
197 let element_node = from_glib_borrow(element_node);
198 let callback = user_data as *mut P;
199 (*callback)(&array, index_, &element_node)
200 }
201 let func = Some(func_func::<P> as _);
202 let super_callback0: &mut P = &mut func_data;
203 unsafe {
204 ffi::json_array_foreach_element(self.to_glib_none().0, func, super_callback0 as *mut _ as *mut _);
205 }
206 }
207
208 /// Conveniently retrieves the array at the given position inside an array.
209 ///
210 /// See also: [`element()`][Self::element()], [`Node::array()`][crate::Node::array()]
211 /// ## `index_`
212 /// the index of the element to retrieve
213 ///
214 /// # Returns
215 ///
216 /// the array
217 #[doc(alias = "json_array_get_array_element")]
218 #[doc(alias = "get_array_element")]
219#[must_use]
220 pub fn array_element(&self, index_: u32) -> Array {
221 unsafe {
222 from_glib_none(ffi::json_array_get_array_element(self.to_glib_none().0, index_))
223 }
224 }
225
226 /// Conveniently retrieves the boolean value of the element at the given
227 /// position inside an array.
228 ///
229 /// See also: [`element()`][Self::element()], [`Node::is_boolean()`][crate::Node::is_boolean()]
230 /// ## `index_`
231 /// the index of the element to retrieve
232 ///
233 /// # Returns
234 ///
235 /// the boolean value
236 #[doc(alias = "json_array_get_boolean_element")]
237 #[doc(alias = "get_boolean_element")]
238 pub fn is_boolean_element(&self, index_: u32) -> bool {
239 unsafe {
240 from_glib(ffi::json_array_get_boolean_element(self.to_glib_none().0, index_))
241 }
242 }
243
244 /// Conveniently retrieves the floating point value of the element at
245 /// the given position inside an array.
246 ///
247 /// See also: [`element()`][Self::element()], [`Node::double()`][crate::Node::double()]
248 /// ## `index_`
249 /// the index of the element to retrieve
250 ///
251 /// # Returns
252 ///
253 /// the floating point value
254 #[doc(alias = "json_array_get_double_element")]
255 #[doc(alias = "get_double_element")]
256 pub fn double_element(&self, index_: u32) -> f64 {
257 unsafe {
258 ffi::json_array_get_double_element(self.to_glib_none().0, index_)
259 }
260 }
261
262 /// Retrieves the element at the given position in the array.
263 /// ## `index_`
264 /// the index of the element to retrieve
265 ///
266 /// # Returns
267 ///
268 /// the element at the given position
269 #[doc(alias = "json_array_get_element")]
270 #[doc(alias = "get_element")]
271 pub fn element(&self, index_: u32) -> Node {
272 unsafe {
273 from_glib_none(ffi::json_array_get_element(self.to_glib_none().0, index_))
274 }
275 }
276
277 /// Retrieves all the elements of an array as a list of nodes.
278 ///
279 /// # Returns
280 ///
281 /// the elements
282 /// of the array
283 #[doc(alias = "json_array_get_elements")]
284 #[doc(alias = "get_elements")]
285 pub fn elements(&self) -> Vec<Node> {
286 unsafe {
287 FromGlibPtrContainer::from_glib_container(ffi::json_array_get_elements(self.to_glib_none().0))
288 }
289 }
290
291 /// Conveniently retrieves the integer value of the element at the given
292 /// position inside an array.
293 ///
294 /// See also: [`element()`][Self::element()], [`Node::int()`][crate::Node::int()]
295 /// ## `index_`
296 /// the index of the element to retrieve
297 ///
298 /// # Returns
299 ///
300 /// the integer value
301 #[doc(alias = "json_array_get_int_element")]
302 #[doc(alias = "get_int_element")]
303 pub fn int_element(&self, index_: u32) -> i64 {
304 unsafe {
305 ffi::json_array_get_int_element(self.to_glib_none().0, index_)
306 }
307 }
308
309 /// Retrieves the length of the given array
310 ///
311 /// # Returns
312 ///
313 /// the length of the array
314 #[doc(alias = "json_array_get_length")]
315 #[doc(alias = "get_length")]
316 pub fn length(&self) -> u32 {
317 unsafe {
318 ffi::json_array_get_length(self.to_glib_none().0)
319 }
320 }
321
322 /// Conveniently checks whether the element at the given position inside the
323 /// array contains a `null` value.
324 ///
325 /// See also: [`element()`][Self::element()], [`Node::is_null()`][crate::Node::is_null()]
326 /// ## `index_`
327 /// the index of the element to retrieve
328 ///
329 /// # Returns
330 ///
331 /// `TRUE` if the element is `null`
332 #[doc(alias = "json_array_get_null_element")]
333 #[doc(alias = "get_null_element")]
334 pub fn is_null_element(&self, index_: u32) -> bool {
335 unsafe {
336 from_glib(ffi::json_array_get_null_element(self.to_glib_none().0, index_))
337 }
338 }
339
340 /// Conveniently retrieves the object at the given position inside an array.
341 ///
342 /// See also: [`element()`][Self::element()], [`Node::object()`][crate::Node::object()]
343 /// ## `index_`
344 /// the index of the element to retrieve
345 ///
346 /// # Returns
347 ///
348 /// the object
349 #[doc(alias = "json_array_get_object_element")]
350 #[doc(alias = "get_object_element")]
351 pub fn object_element(&self, index_: u32) -> Object {
352 unsafe {
353 from_glib_none(ffi::json_array_get_object_element(self.to_glib_none().0, index_))
354 }
355 }
356
357 /// Conveniently retrieves the string value of the element at the given
358 /// position inside an array.
359 ///
360 /// See also: [`element()`][Self::element()], [`Node::string()`][crate::Node::string()]
361 /// ## `index_`
362 /// the index of the element to retrieve
363 ///
364 /// # Returns
365 ///
366 /// the string value
367 #[doc(alias = "json_array_get_string_element")]
368 #[doc(alias = "get_string_element")]
369 pub fn string_element(&self, index_: u32) -> glib::GString {
370 unsafe {
371 from_glib_none(ffi::json_array_get_string_element(self.to_glib_none().0, index_))
372 }
373 }
374
375 #[cfg(feature = "v1_2")]
376 #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
377 #[doc(alias = "json_array_hash")]
378 fn hash(&self) -> u32 {
379 unsafe {
380 ffi::json_array_hash(ToGlibPtr::<*mut ffi::JsonArray>::to_glib_none(self).0 as glib::ffi::gconstpointer)
381 }
382 }
383
384 /// Check whether the given `array` has been marked as immutable by calling
385 /// [`seal()`][Self::seal()] on it.
386 ///
387 /// # Returns
388 ///
389 /// [`true`] if the array is immutable
390 #[cfg(feature = "v1_2")]
391 #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
392 #[doc(alias = "json_array_is_immutable")]
393 pub fn is_immutable(&self) -> bool {
394 unsafe {
395 from_glib(ffi::json_array_is_immutable(self.to_glib_none().0))
396 }
397 }
398
399 /// Removes the element at the given position inside an array.
400 ///
401 /// This function will release the reference held on the element.
402 /// ## `index_`
403 /// the position of the element to be removed
404 #[doc(alias = "json_array_remove_element")]
405 pub fn remove_element(&self, index_: u32) {
406 unsafe {
407 ffi::json_array_remove_element(self.to_glib_none().0, index_);
408 }
409 }
410
411 /// Seals the given array, making it immutable to further changes.
412 ///
413 /// This function will recursively seal all elements in the array too.
414 ///
415 /// If the `array` is already immutable, this is a no-op.
416 #[cfg(feature = "v1_2")]
417 #[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
418 #[doc(alias = "json_array_seal")]
419 pub fn seal(&self) {
420 unsafe {
421 ffi::json_array_seal(self.to_glib_none().0);
422 }
423 }
424}
425
426impl Default for Array {
427 fn default() -> Self {
428 Self::new()
429 }
430 }
431
432#[cfg(feature = "v1_2")]
433#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
434impl PartialEq for Array {
435 #[inline]
436 fn eq(&self, other: &Self) -> bool {
437 self.equal(other)
438 }
439}
440#[cfg(feature = "v1_2")]
441#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
442
443impl Eq for Array {}
444
445#[cfg(feature = "v1_2")]
446#[cfg_attr(docsrs, doc(cfg(feature = "v1_2")))]
447impl std::hash::Hash for Array {
448 #[inline]
449 fn hash<H>(&self, state: &mut H) where H: std::hash::Hasher {
450 std::hash::Hash::hash(&self.hash(), state)
451 }
452}