Skip to content

Commit

Permalink
glib: Add new object constructor for constructing an object with defa…
Browse files Browse the repository at this point in the history
…ult property values
  • Loading branch information
sdroege committed Jan 21, 2023
1 parent 5ed62f5 commit 95a7ef9
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 28 deletions.
2 changes: 1 addition & 1 deletion examples/gio_task/file_size/mod.rs
Expand Up @@ -13,7 +13,7 @@ unsafe impl Sync for FileSize {}

impl FileSize {
pub fn new() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}

pub fn retrieved_size(&self) -> Option<i64> {
Expand Down
4 changes: 2 additions & 2 deletions gio/src/read_input_stream.rs
Expand Up @@ -146,15 +146,15 @@ glib::wrapper! {

impl ReadInputStream {
pub fn new<R: Read + Send + 'static>(read: R) -> ReadInputStream {
let obj: Self = glib::Object::new(&[]);
let obj: Self = glib::Object::new_default();

*obj.imp().read.borrow_mut() = Some(imp::Reader::Read(AnyReader::new(read)));

obj
}

pub fn new_seekable<R: Read + Seek + Send + 'static>(read: R) -> ReadInputStream {
let obj: Self = glib::Object::new(&[]);
let obj: Self = glib::Object::new_default();

*obj.imp().read.borrow_mut() = Some(imp::Reader::ReadSeek(AnyReader::new_seekable(read)));

Expand Down
2 changes: 1 addition & 1 deletion gio/src/task.rs
Expand Up @@ -500,7 +500,7 @@ mod test {

impl MySimpleObject {
pub fn new() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}

#[doc(alias = "get_size")]
Expand Down
4 changes: 2 additions & 2 deletions gio/src/write_output_stream.rs
Expand Up @@ -169,14 +169,14 @@ glib::wrapper! {

impl WriteOutputStream {
pub fn new<W: Write + Send + Any + 'static>(write: W) -> WriteOutputStream {
let obj: Self = glib::Object::new(&[]);
let obj: Self = glib::Object::new_default();

*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: Self = glib::Object::new(&[]);
let obj: Self = glib::Object::new_default();

*obj.imp().write.borrow_mut() =
Some(imp::Writer::WriteSeek(AnyWriter::new_seekable(write)));
Expand Down
2 changes: 1 addition & 1 deletion glib/src/boxed_any_object.rs
Expand Up @@ -90,7 +90,7 @@ impl BoxedAnyObject {
// rustdoc-stripper-ignore-next
/// Creates a new `BoxedAnyObject` containing `value`
pub fn new<T: 'static>(value: T) -> Self {
let obj: Self = Object::new(&[]);
let obj: Self = Object::new_default();
obj.replace(value);
obj
}
Expand Down
2 changes: 1 addition & 1 deletion glib/src/gobject/binding.rs
Expand Up @@ -279,7 +279,7 @@ mod test {

impl Default for TestObject {
fn default() -> Self {
crate::Object::new(&[])
crate::Object::new_default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion glib/src/gobject/binding_group.rs
Expand Up @@ -479,7 +479,7 @@ mod test {

impl Default for TestObject {
fn default() -> Self {
crate::Object::new(&[])
crate::Object::new_default()
}
}

Expand Down
42 changes: 32 additions & 10 deletions glib/src/object.rs
Expand Up @@ -1353,9 +1353,8 @@ impl Object {
#[allow(clippy::new_ret_no_self)]
#[track_caller]
pub fn new<T: IsA<Object> + IsClass>(properties: &[(&str, &dyn ToValue)]) -> T {
Object::with_type(T::static_type(), properties)
.downcast()
.unwrap()
let object = Object::with_type(T::static_type(), properties);
unsafe { object.unsafe_cast() }
}

// rustdoc-stripper-ignore-next
Expand Down Expand Up @@ -1387,6 +1386,29 @@ impl Object {
unsafe { Object::new_internal(type_, &mut property_values) }
}

// rustdoc-stripper-ignore-next
/// Create a new instance of an object with the default property values.
///
/// # Panics
///
/// This panics if the object is not instantiable.
#[track_caller]
pub fn new_default<T: IsA<Object> + IsClass>() -> T {
let object = Object::new_default_with_type(T::static_type());
unsafe { object.unsafe_cast() }
}

// rustdoc-stripper-ignore-next
/// Create a new instance of an object with the default property values.
///
/// # Panics
///
/// This panics if the object is not instantiable.
#[track_caller]
pub fn new_default_with_type(type_: Type) -> Object {
Object::with_mut_values(type_, &mut [])
}

// rustdoc-stripper-ignore-next
/// Create a new instance of an object of the given type with the given properties.
///
Expand Down Expand Up @@ -4448,13 +4470,13 @@ mod tests {

#[test]
fn new() {
let obj: Object = Object::new(&[]);
let obj: Object = Object::new_default();
drop(obj);
}

#[test]
fn data() {
let obj: Object = Object::new(&[]);
let obj: Object = Object::new_default();
unsafe {
obj.set_data::<String>("foo", "hello".into());
let data = obj.data::<String>("foo").unwrap();
Expand All @@ -4466,7 +4488,7 @@ mod tests {

#[test]
fn weak_ref() {
let obj: Object = Object::new(&[]);
let obj: Object = Object::new_default();

let weakref: WeakRef<Object> = WeakRef::new();
weakref.set(Some(&obj));
Expand All @@ -4484,7 +4506,7 @@ mod tests {

#[test]
fn weak_ref_notify() {
let obj: Object = Object::new(&[]);
let obj: Object = Object::new_default();

let handle = obj.add_weak_ref_notify(|| {
unreachable!();
Expand All @@ -4502,7 +4524,7 @@ mod tests {
assert!(called.load(Ordering::SeqCst));
handle.disconnect();

let obj: Object = Object::new(&[]);
let obj: Object = Object::new_default();

let called = Arc::new(AtomicBool::new(false));
let called_weak = Arc::downgrade(&called);
Expand All @@ -4513,7 +4535,7 @@ mod tests {
drop(obj);
assert!(called.load(Ordering::SeqCst));

let obj: Object = Object::new(&[]);
let obj: Object = Object::new_default();

let called = Rc::new(Cell::new(false));
let called_weak = Rc::downgrade(&called);
Expand All @@ -4527,7 +4549,7 @@ mod tests {

#[test]
fn test_value() {
let obj1: Object = Object::new(&[]);
let obj1: Object = Object::new_default();
let v = obj1.to_value();
let obj2 = v.get::<&Object>().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion glib/src/subclass/mod.rs
Expand Up @@ -176,7 +176,7 @@
//! impl SimpleObject {
//! // Create an object instance of the new type.
//! pub fn new() -> Self {
//! glib::Object::new(&[])
//! glib::Object::new_default()
//! }
//! }
//!
Expand Down
2 changes: 1 addition & 1 deletion glib/src/subclass/object.rs
Expand Up @@ -431,7 +431,7 @@ mod test {

#[test]
fn test_create_child_object() {
let obj: ChildObject = Object::new(&[]);
let obj: ChildObject = Object::new_default();

assert_eq!(&obj, obj.imp().obj().as_ref());
}
Expand Down
2 changes: 1 addition & 1 deletion glib/tests/subclass_compiletest/01-auto-send-sync.rs
Expand Up @@ -21,7 +21,7 @@ glib::wrapper! {

impl Default for TestObject {
fn default() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}
}

Expand Down
2 changes: 1 addition & 1 deletion glib/tests/subclass_compiletest/02-no-auto-send-sync.rs
Expand Up @@ -22,7 +22,7 @@ glib::wrapper! {

impl Default for TestObject {
fn default() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}
}

Expand Down
Expand Up @@ -25,7 +25,7 @@ unsafe impl<T: TestParentImpl> glib::subclass::prelude::IsSubclassable<T> for Te

impl Default for TestParent {
fn default() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}
}

Expand Down Expand Up @@ -54,7 +54,7 @@ glib::wrapper! {

impl Default for TestObject {
fn default() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}
}

Expand Down
Expand Up @@ -26,7 +26,7 @@ unsafe impl<T: TestParentImpl> glib::subclass::prelude::IsSubclassable<T> for Te

impl Default for TestParent {
fn default() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}
}

Expand Down Expand Up @@ -55,7 +55,7 @@ glib::wrapper! {

impl Default for TestObject {
fn default() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}
}

Expand Down
Expand Up @@ -39,7 +39,7 @@ glib::wrapper! {

impl Default for TestObject {
fn default() -> Self {
glib::Object::new(&[])
glib::Object::new_default()
}
}

Expand Down

0 comments on commit 95a7ef9

Please sign in to comment.