Skip to content
This repository was archived by the owner on Mar 4, 2024. It is now read-only.

Commit 8232aba

Browse files
committed
Require a static slice instead of a Vec for the list of paramspecs
This way we can ensure they're equivalent even if the function is called multiple times.
1 parent 08b4cb7 commit 8232aba

File tree

4 files changed

+96
-81
lines changed

4 files changed

+96
-81
lines changed

examples/src/bin/listbox_model.rs

+24-19
Original file line numberDiff line numberDiff line change
@@ -329,25 +329,30 @@ mod row_data {
329329
// This maps between the GObject properties and our internal storage of the
330330
// corresponding values of the properties.
331331
impl ObjectImpl for RowData {
332-
fn properties() -> Vec<glib::ParamSpec> {
333-
vec![
334-
glib::ParamSpec::string(
335-
"name",
336-
"Name",
337-
"Name",
338-
None, // Default value
339-
glib::ParamFlags::READWRITE,
340-
),
341-
glib::ParamSpec::uint(
342-
"count",
343-
"Count",
344-
"Count",
345-
0,
346-
100,
347-
0, // Allowed range and default value
348-
glib::ParamFlags::READWRITE,
349-
),
350-
]
332+
fn properties() -> &'static [glib::ParamSpec] {
333+
use once_cell::sync::Lazy;
334+
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
335+
vec![
336+
glib::ParamSpec::string(
337+
"name",
338+
"Name",
339+
"Name",
340+
None, // Default value
341+
glib::ParamFlags::READWRITE,
342+
),
343+
glib::ParamSpec::uint(
344+
"count",
345+
"Count",
346+
"Count",
347+
0,
348+
100,
349+
0, // Allowed range and default value
350+
glib::ParamFlags::READWRITE,
351+
),
352+
]
353+
});
354+
355+
PROPERTIES.as_ref()
351356
}
352357

353358
fn set_property(

glib/src/subclass/interface.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ pub trait ObjectInterface: Sized + 'static {
9494
/// Properties installed for this interface.
9595
///
9696
/// All implementors of the interface must provide these properties.
97-
fn properties() -> Vec<ParamSpec> {
98-
vec![]
97+
fn properties() -> &'static [ParamSpec] {
98+
&[]
9999
}
100100
}
101101

glib/src/subclass/mod.rs

+31-26
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,37 @@
108108
//! // Trait that is used to override virtual methods of glib::Object.
109109
//! impl ObjectImpl for SimpleObject {
110110
//! // Called once in the very beginning to list all properties of this class.
111-
//! fn properties() -> Vec<glib::ParamSpec> {
112-
//! vec![
113-
//! glib::ParamSpec::string(
114-
//! "name",
115-
//! "Name",
116-
//! "Name of this object",
117-
//! None,
118-
//! glib::ParamFlags::READWRITE,
119-
//! ),
120-
//! glib::ParamSpec::enum_(
121-
//! "animal",
122-
//! "Animal",
123-
//! "Animal",
124-
//! Animal::static_type(),
125-
//! Animal::default() as i32,
126-
//! glib::ParamFlags::READWRITE,
127-
//! ),
128-
//! glib::ParamSpec::flags(
129-
//! "flags",
130-
//! "Flags",
131-
//! "Flags",
132-
//! MyFlags::static_type(),
133-
//! MyFlags::default().bits(),
134-
//! glib::ParamFlags::READWRITE,
135-
//! ),
136-
//! ]
111+
//! fn properties() -> &'static [glib::ParamSpec] {
112+
//! use once_cell::sync::Lazy;
113+
//! static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
114+
//! vec![
115+
//! glib::ParamSpec::string(
116+
//! "name",
117+
//! "Name",
118+
//! "Name of this object",
119+
//! None,
120+
//! glib::ParamFlags::READWRITE,
121+
//! ),
122+
//! glib::ParamSpec::enum_(
123+
//! "animal",
124+
//! "Animal",
125+
//! "Animal",
126+
//! Animal::static_type(),
127+
//! Animal::default() as i32,
128+
//! glib::ParamFlags::READWRITE,
129+
//! ),
130+
//! glib::ParamSpec::flags(
131+
//! "flags",
132+
//! "Flags",
133+
//! "Flags",
134+
//! MyFlags::static_type(),
135+
//! MyFlags::default().bits(),
136+
//! glib::ParamFlags::READWRITE,
137+
//! ),
138+
//! ]
139+
//! });
140+
//!
141+
//! PROPERTIES.as_ref()
137142
//! }
138143
//!
139144
//! // Called whenever a property is set on this instance. The id

glib/src/subclass/object.rs

+39-34
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use std::ptr;
1414
/// This allows overriding the virtual methods of `glib::Object`.
1515
pub trait ObjectImpl: ObjectSubclass + ObjectImplExt {
1616
/// Properties installed for this type.
17-
fn properties() -> Vec<ParamSpec> {
18-
vec![]
17+
fn properties() -> &'static [ParamSpec] {
18+
&[]
1919
}
2020

2121
/// Property setter.
@@ -263,7 +263,7 @@ unsafe impl<T: ObjectImpl> IsSubclassable<T> for Object {
263263

264264
pspecs_ptrs.push(ptr::null_mut());
265265

266-
for pspec in &pspecs {
266+
for pspec in pspecs {
267267
pspecs_ptrs.push(pspec.to_glib_none().0);
268268
}
269269

@@ -422,37 +422,42 @@ mod test {
422422
}
423423

424424
impl ObjectImpl for SimpleObject {
425-
fn properties() -> Vec<crate::ParamSpec> {
426-
vec![
427-
crate::ParamSpec::string(
428-
"name",
429-
"Name",
430-
"Name of this object",
431-
None,
432-
crate::ParamFlags::READWRITE,
433-
),
434-
crate::ParamSpec::string(
435-
"construct-name",
436-
"Construct Name",
437-
"Construct Name of this object",
438-
None,
439-
crate::ParamFlags::READWRITE | crate::ParamFlags::CONSTRUCT_ONLY,
440-
),
441-
crate::ParamSpec::boolean(
442-
"constructed",
443-
"Constructed",
444-
"True if the constructed() virtual method was called",
445-
false,
446-
crate::ParamFlags::READABLE,
447-
),
448-
crate::ParamSpec::object(
449-
"child",
450-
"Child",
451-
"Child object",
452-
super::ChildObject::static_type(),
453-
crate::ParamFlags::READWRITE,
454-
),
455-
]
425+
fn properties() -> &'static [ParamSpec] {
426+
use once_cell::sync::Lazy;
427+
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
428+
vec![
429+
crate::ParamSpec::string(
430+
"name",
431+
"Name",
432+
"Name of this object",
433+
None,
434+
crate::ParamFlags::READWRITE,
435+
),
436+
crate::ParamSpec::string(
437+
"construct-name",
438+
"Construct Name",
439+
"Construct Name of this object",
440+
None,
441+
crate::ParamFlags::READWRITE | crate::ParamFlags::CONSTRUCT_ONLY,
442+
),
443+
crate::ParamSpec::boolean(
444+
"constructed",
445+
"Constructed",
446+
"True if the constructed() virtual method was called",
447+
false,
448+
crate::ParamFlags::READABLE,
449+
),
450+
crate::ParamSpec::object(
451+
"child",
452+
"Child",
453+
"Child object",
454+
super::ChildObject::static_type(),
455+
crate::ParamFlags::READWRITE,
456+
),
457+
]
458+
});
459+
460+
PROPERTIES.as_ref()
456461
}
457462

458463
fn set_property(

0 commit comments

Comments
 (0)