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

Commit

Permalink
glib: Make Object::new() generic
Browse files Browse the repository at this point in the history
This avoids the explicit downcast in the common usage of the function.
The previous function is available as `Object::with_type()`.

Also requires a gir update and regenerating with it.
  • Loading branch information
ids1024 committed Dec 17, 2020
1 parent 8347965 commit 7e1f8b0
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 77 deletions.
18 changes: 5 additions & 13 deletions examples/src/bin/basic_subclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ glib_wrapper! {

impl SimpleWindow {
pub fn new(app: &SimpleApplication) -> Self {
glib::Object::new(Self::static_type(), &[("application", app)])
.expect("Failed to create SimpleWindow")
.downcast::<SimpleWindow>()
.expect("Created SimpleWindow is of wrong type")
glib::Object::new(&[("application", app)]).expect("Failed to create SimpleWindow")
}
}

Expand Down Expand Up @@ -196,16 +193,11 @@ glib_wrapper! {
impl SimpleApplication {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
glib::Object::new(
Self::static_type(),
&[
("application-id", &"org.gtk-rs.SimpleApplication"),
("flags", &ApplicationFlags::empty()),
],
)
glib::Object::new(&[
("application-id", &"org.gtk-rs.SimpleApplication"),
("flags", &ApplicationFlags::empty()),
])
.expect("Failed to create SimpleApp")
.downcast()
.expect("Created simpleapp is of wrong type")
}
}

Expand Down
5 changes: 1 addition & 4 deletions examples/src/bin/composite_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ glib_wrapper! {

impl ExApplicationWindow {
pub fn new<P: glib::IsA<gtk::Application>>(app: &P) -> Self {
glib::Object::new(Self::static_type(), &[("application", app)])
.expect("Failed to create ExApplicationWindow")
.downcast::<ExApplicationWindow>()
.expect("Created object is of wrong type")
glib::Object::new(&[("application", app)]).expect("Failed to create ExApplicationWindow")
}

pub fn init_label(&self) {
Expand Down
9 changes: 2 additions & 7 deletions examples/src/bin/listbox_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ mod model {
impl Model {
#[allow(clippy::new_without_default)]
pub fn new() -> Model {
glib::Object::new(Self::static_type(), &[])
.expect("Failed to create Model")
.downcast()
.expect("Created Model is of wrong type")
glib::Object::new(&[]).expect("Failed to create Model")
}

pub fn append(&self, obj: &RowData) {
Expand Down Expand Up @@ -413,10 +410,8 @@ mod row_data {
// initial values for our two properties and then returns the new instance
impl RowData {
pub fn new(name: &str, count: u32) -> RowData {
glib::Object::new(Self::static_type(), &[("name", &name), ("count", &count)])
glib::Object::new(&[("name", &name), ("count", &count)])
.expect("Failed to create row data")
.downcast()
.expect("Created row data is of wrong type")
}
}
}
10 changes: 2 additions & 8 deletions gio/src/read_input_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ glib::glib_wrapper! {

impl ReadInputStream {
pub fn new<R: Read + Send + 'static>(read: R) -> ReadInputStream {
let obj = glib::Object::new(Self::static_type(), &[])
.expect("Failed to create read input stream")
.downcast()
.expect("Created read input stream is of wrong type");
let obj = glib::Object::new(&[]).expect("Failed to create read input stream");

let imp = imp::ReadInputStream::from_instance(&obj);
*imp.read.borrow_mut() = Some(imp::Reader::Read(AnyReader::new(read)));
Expand All @@ -176,10 +173,7 @@ impl ReadInputStream {
}

pub fn new_seekable<R: Read + Seek + Send + 'static>(read: R) -> ReadInputStream {
let obj = glib::Object::new(Self::static_type(), &[])
.expect("Failed to create read input stream")
.downcast()
.expect("Created read input stream is of wrong type");
let obj = glib::Object::new(&[]).expect("Failed to create read input stream");

let imp = imp::ReadInputStream::from_instance(&obj);
*imp.read.borrow_mut() = Some(imp::Reader::ReadSeek(AnyReader::new_seekable(read)));
Expand Down
17 changes: 7 additions & 10 deletions gio/src/subclass/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,21 +549,18 @@ mod tests {
}

glib::glib_wrapper! {
pub struct SimpleApplication(ObjectSubclass<imp::SimpleApplication>);
pub struct SimpleApplication(ObjectSubclass<imp::SimpleApplication>)
@implements crate::Application;
}

#[test]
fn test_simple_application() {
let app = glib::Object::new(
SimpleApplication::static_type(),
&[
("application-id", &"org.gtk-rs.SimpleApplication"),
("flags", &crate::ApplicationFlags::empty()),
],
)
let app = glib::Object::new::<SimpleApplication>(&[
("application-id", &"org.gtk-rs.SimpleApplication"),
("flags", &crate::ApplicationFlags::empty()),
])
.unwrap()
.downcast::<crate::Application>()
.unwrap();
.upcast::<crate::Application>();

app.set_inactivity_timeout(10000);

Expand Down
5 changes: 1 addition & 4 deletions gio/src/subclass/input_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,7 @@ mod tests {

#[test]
fn test_simple_stream() {
let stream = glib::Object::new(SimpleInputStream::static_type(), &[])
.unwrap()
.downcast::<SimpleInputStream>()
.unwrap();
let stream = glib::Object::new::<SimpleInputStream>(&[]).unwrap();

let mut buf = [0; 16];
assert_eq!(stream.read(&mut buf, crate::NONE_CANCELLABLE), Ok(16));
Expand Down
5 changes: 1 addition & 4 deletions gio/src/subclass/output_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,7 @@ mod tests {

#[test]
fn test_simple_stream() {
let stream = glib::Object::new(SimpleOutputStream::static_type(), &[])
.unwrap()
.downcast::<SimpleOutputStream>()
.unwrap();
let stream = glib::Object::new::<SimpleOutputStream>(&[]).unwrap();

assert_eq!(
*imp::SimpleOutputStream::from_instance(&stream).sum.borrow(),
Expand Down
10 changes: 2 additions & 8 deletions gio/src/write_output_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,15 @@ glib::glib_wrapper! {

impl WriteOutputStream {
pub fn new<W: Write + Send + Any + 'static>(write: W) -> WriteOutputStream {
let obj = glib::Object::new(Self::static_type(), &[])
.expect("Failed to create write input stream")
.downcast()
.expect("Created write input stream is of wrong type");
let obj = glib::Object::new(&[]).expect("Failed to create write input stream");

let imp = imp::WriteOutputStream::from_instance(&obj);
*imp.write.borrow_mut() = Some(imp::Writer::Write(AnyWriter::new(write)));
obj
}

pub fn new_seekable<W: Write + Seek + Send + Any + 'static>(write: W) -> WriteOutputStream {
let obj = glib::Object::new(Self::static_type(), &[])
.expect("Failed to create write input stream")
.downcast()
.expect("Created write input stream is of wrong type");
let obj = glib::Object::new(&[]).expect("Failed to create write input stream");

let imp = imp::WriteOutputStream::from_instance(&obj);
*imp.write.borrow_mut() = Some(imp::Writer::WriteSeek(AnyWriter::new_seekable(write)));
Expand Down
12 changes: 11 additions & 1 deletion glib/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,17 @@ glib_object_wrapper!(@object
pub type ObjectClass = Class<Object>;

impl Object {
pub fn new(type_: Type, properties: &[(&str, &dyn ToValue)]) -> Result<Object, BoolError> {
#[allow(clippy::new_ret_no_self)]
pub fn new<T: IsA<Object>>(properties: &[(&str, &dyn ToValue)]) -> Result<T, BoolError> {
Ok(Object::with_type(T::static_type(), properties)?
.downcast()
.unwrap())
}

pub fn with_type(
type_: Type,
properties: &[(&str, &dyn ToValue)],
) -> Result<Object, BoolError> {
use std::ffi::CString;

let klass = ObjectClass::from_type(type_)
Expand Down
5 changes: 1 addition & 4 deletions glib/src/subclass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,7 @@
//! impl SimpleObject {
//! // Create an object instance of the new type.
//! pub fn new() -> Self {
//! glib::Object::new(Self::static_type(), &[])
//! .unwrap()
//! .downcast()
//! .unwrap()
//! glib::Object::new(&[]).unwrap()
//! }
//! }
//!
Expand Down
25 changes: 11 additions & 14 deletions glib/src/subclass/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ mod test {
#[test]
fn test_create() {
let type_ = SimpleObject::static_type();
let obj = Object::new(type_, &[]).expect("Object::new failed");
let obj = Object::with_type(type_, &[]).expect("Object::new failed");

assert!(obj.get_type().is_a(&DummyInterface::static_type()));

Expand All @@ -591,11 +591,7 @@ mod test {

#[test]
fn test_create_child_object() {
let type_ = ChildObject::static_type();
let obj = Object::new(type_, &[])
.expect("Object::new failed")
.downcast::<ChildObject>()
.unwrap();
let obj: ChildObject = Object::new(&[]).expect("Object::new failed");

// ChildObject is a zero-sized type and we map that to the same pointer as the object
// itself. No private/impl data is allocated for zero-sized types.
Expand All @@ -606,7 +602,7 @@ mod test {

#[test]
fn test_set_properties() {
let obj = Object::new(
let obj = Object::with_type(
SimpleObject::static_type(),
&[("construct-name", &"meh"), ("name", &"initial")],
)
Expand Down Expand Up @@ -674,7 +670,8 @@ mod test {
"property 'name' of type 'SimpleObject' can't be set from the given type (expected: 'gchararray', got: 'gboolean')",
);

let other_obj = Object::new(SimpleObject::static_type(), &[]).expect("Object::new failed");
let other_obj =
Object::with_type(SimpleObject::static_type(), &[]).expect("Object::new failed");
assert_eq!(
obj.set_property("child", &other_obj)
.err()
Expand All @@ -683,7 +680,7 @@ mod test {
"property 'child' of type 'SimpleObject' can't be set from the given object type (expected: 'ChildObject', got: 'SimpleObject')",
);

let child = Object::new(ChildObject::static_type(), &[]).expect("Object::new failed");
let child = Object::with_type(ChildObject::static_type(), &[]).expect("Object::new failed");
assert!(obj.set_property("child", &child).is_ok());
}

Expand All @@ -693,7 +690,7 @@ mod test {
use std::sync::Arc;

let type_ = SimpleObject::static_type();
let obj = Object::new(type_, &[("name", &"old-name")]).expect("Object::new failed");
let obj = Object::with_type(type_, &[("name", &"old-name")]).expect("Object::new failed");

let name_changed_triggered = Arc::new(AtomicBool::new(false));
let name_changed_clone = name_changed_triggered.clone();
Expand Down Expand Up @@ -735,7 +732,7 @@ mod test {

#[test]
fn test_signal_return_expected_type() {
let obj = Object::new(SimpleObject::static_type(), &[]).expect("Object::new failed");
let obj = Object::with_type(SimpleObject::static_type(), &[]).expect("Object::new failed");

obj.connect("create-string", false, move |_args| {
Some("return value".to_value())
Expand All @@ -755,7 +752,7 @@ mod test {
use std::sync::Arc;

let type_ = SimpleObject::static_type();
let obj = Object::new(type_, &[("name", &"old-name")]).expect("Object::new failed");
let obj = Object::with_type(type_, &[("name", &"old-name")]).expect("Object::new failed");

let name_changed_triggered = Arc::new(AtomicBool::new(false));
let name_changed_clone = name_changed_triggered.clone();
Expand All @@ -772,11 +769,11 @@ mod test {

#[test]
fn test_signal_return_expected_object_type() {
let obj = Object::new(SimpleObject::static_type(), &[]).expect("Object::new failed");
let obj = Object::with_type(SimpleObject::static_type(), &[]).expect("Object::new failed");

obj.connect("create-child-object", false, move |_args| {
Some(
Object::new(ChildObject::static_type(), &[])
Object::with_type(ChildObject::static_type(), &[])
.expect("Object::new failed")
.to_value(),
)
Expand Down

0 comments on commit 7e1f8b0

Please sign in to comment.