gio/auto/list_store.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, ListModel};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9 /// `GListStore` is a simple implementation of [`ListModel`][crate::ListModel] that stores
10 /// all items in memory.
11 ///
12 /// It provides insertions, deletions, and lookups in logarithmic time
13 /// with a fast path for the common case of iterating the list linearly.
14 ///
15 /// ## Properties
16 ///
17 ///
18 /// #### `item-type`
19 /// The type of items contained in this list store. Items must be
20 /// subclasses of #GObject.
21 ///
22 /// Readable | Writeable | Construct Only
23 ///
24 ///
25 /// #### `n-items`
26 /// The number of items contained in this list store.
27 ///
28 /// Readable
29 ///
30 /// # Implements
31 ///
32 /// [`trait@glib::ObjectExt`], [`ListModelExt`][trait@crate::prelude::ListModelExt], [`ListModelExtManual`][trait@crate::prelude::ListModelExtManual]
33 #[doc(alias = "GListStore")]
34 pub struct ListStore(Object<ffi::GListStore, ffi::GListStoreClass>) @implements ListModel;
35
36 match fn {
37 type_ => || ffi::g_list_store_get_type(),
38 }
39}
40
41impl ListStore {
42 // rustdoc-stripper-ignore-next
43 /// Creates a new builder-pattern struct instance to construct [`ListStore`] objects.
44 ///
45 /// This method returns an instance of [`ListStoreBuilder`](crate::builders::ListStoreBuilder) which can be used to create [`ListStore`] objects.
46 pub fn builder() -> ListStoreBuilder {
47 ListStoreBuilder::new()
48 }
49
50 /// Appends @item to @self. @item must be of type #GListStore:item-type.
51 ///
52 /// This function takes a ref on @item.
53 ///
54 /// Use g_list_store_splice() to append multiple items at the same time
55 /// efficiently.
56 /// ## `item`
57 /// the new item
58 #[doc(alias = "g_list_store_append")]
59 pub fn append(&self, item: &impl IsA<glib::Object>) {
60 unsafe {
61 ffi::g_list_store_append(self.to_glib_none().0, item.as_ref().to_glib_none().0);
62 }
63 }
64
65 /// Looks up the given @item in the list store by looping over the items until
66 /// the first occurrence of @item. If @item was not found, then @position will
67 /// not be set, and this method will return [`false`].
68 ///
69 /// If you need to compare the two items with a custom comparison function, use
70 /// g_list_store_find_with_equal_func() with a custom #GEqualFunc instead.
71 /// ## `item`
72 /// an item
73 ///
74 /// # Returns
75 ///
76 /// Whether @self contains @item. If it was found, @position will be
77 /// set to the position where @item occurred for the first time.
78 ///
79 /// ## `position`
80 /// the first position of @item, if it was found.
81 #[cfg(feature = "v2_64")]
82 #[cfg_attr(docsrs, doc(cfg(feature = "v2_64")))]
83 #[doc(alias = "g_list_store_find")]
84 pub fn find(&self, item: &impl IsA<glib::Object>) -> Option<u32> {
85 unsafe {
86 let mut position = std::mem::MaybeUninit::uninit();
87 let ret = from_glib(ffi::g_list_store_find(
88 self.to_glib_none().0,
89 item.as_ref().to_glib_none().0,
90 position.as_mut_ptr(),
91 ));
92 if ret {
93 Some(position.assume_init())
94 } else {
95 None
96 }
97 }
98 }
99
100 /// Inserts @item into @self at @position. @item must be of type
101 /// #GListStore:item-type or derived from it. @position must be smaller
102 /// than the length of the list, or equal to it to append.
103 ///
104 /// This function takes a ref on @item.
105 ///
106 /// Use g_list_store_splice() to insert multiple items at the same time
107 /// efficiently.
108 /// ## `position`
109 /// the position at which to insert the new item
110 /// ## `item`
111 /// the new item
112 #[doc(alias = "g_list_store_insert")]
113 pub fn insert(&self, position: u32, item: &impl IsA<glib::Object>) {
114 unsafe {
115 ffi::g_list_store_insert(
116 self.to_glib_none().0,
117 position,
118 item.as_ref().to_glib_none().0,
119 );
120 }
121 }
122
123 /// Removes the item from @self that is at @position. @position must be
124 /// smaller than the current length of the list.
125 ///
126 /// Use g_list_store_splice() to remove multiple items at the same time
127 /// efficiently.
128 /// ## `position`
129 /// the position of the item that is to be removed
130 #[doc(alias = "g_list_store_remove")]
131 pub fn remove(&self, position: u32) {
132 unsafe {
133 ffi::g_list_store_remove(self.to_glib_none().0, position);
134 }
135 }
136
137 /// Removes all items from @self.
138 #[doc(alias = "g_list_store_remove_all")]
139 pub fn remove_all(&self) {
140 unsafe {
141 ffi::g_list_store_remove_all(self.to_glib_none().0);
142 }
143 }
144}
145
146// rustdoc-stripper-ignore-next
147/// A [builder-pattern] type to construct [`ListStore`] objects.
148///
149/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
150#[must_use = "The builder must be built to be used"]
151pub struct ListStoreBuilder {
152 builder: glib::object::ObjectBuilder<'static, ListStore>,
153}
154
155impl ListStoreBuilder {
156 fn new() -> Self {
157 Self {
158 builder: glib::object::Object::builder(),
159 }
160 }
161
162 /// The type of items contained in this list store. Items must be
163 /// subclasses of #GObject.
164 pub fn item_type(self, item_type: glib::types::Type) -> Self {
165 Self {
166 builder: self.builder.property("item-type", item_type),
167 }
168 }
169
170 // rustdoc-stripper-ignore-next
171 /// Build the [`ListStore`].
172 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
173 pub fn build(self) -> ListStore {
174 self.builder.build()
175 }
176}