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

Commit ca797cc

Browse files
authored
Merge pull request #777 from sdroege/impl-trait-without-object-reference
Change *Impl trait methods to only take &self and not Self::Type in a…
2 parents 6bba580 + 2fba46b commit ca797cc

File tree

22 files changed

+712
-905
lines changed

22 files changed

+712
-905
lines changed

examples/basic_subclass/simple_application/imp.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ impl ApplicationImpl for SimpleApplication {
2727
/// `gio::Application::activate` is what gets called when the
2828
/// application is launched by the desktop environment and
2929
/// aksed to present itself.
30-
fn activate(&self, app: &Self::Type) {
31-
let app = app.downcast_ref::<super::SimpleApplication>().unwrap();
32-
let imp = app.imp();
33-
let window = imp
30+
fn activate(&self) {
31+
let window = self
3432
.window
3533
.get()
3634
.expect("Should always be initiliazed in gio_application_startup");
@@ -45,13 +43,11 @@ impl ApplicationImpl for SimpleApplication {
4543
///
4644
/// Due to this, we create and initialize the `SimpleWindow` widget
4745
/// here. Widgets can't be created before `startup` has been called.
48-
fn startup(&self, app: &Self::Type) {
49-
self.parent_startup(app);
46+
fn startup(&self) {
47+
self.parent_startup();
5048

51-
let app = app.downcast_ref::<super::SimpleApplication>().unwrap();
52-
let imp = app.imp();
53-
let window = SimpleWindow::new(app);
54-
imp.window
49+
let window = SimpleWindow::new(&self.instance());
50+
self.window
5551
.set(window)
5652
.expect("Failed to initialize application window");
5753
}

examples/basic_subclass/simple_window/imp.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ impl ObjectImpl for SimpleWindow {
2727
// Here we are overriding the glib::Objcet::contructed
2828
// method. Its what gets called when we create our Object
2929
// and where we can initialize things.
30-
fn constructed(&self, obj: &Self::Type) {
31-
self.parent_constructed(obj);
30+
fn constructed(&self) {
31+
self.parent_constructed();
32+
33+
let instance = self.instance();
3234

3335
let headerbar = gtk::HeaderBar::new();
3436
let increment = gtk::Button::with_label("Increment!");
@@ -40,14 +42,13 @@ impl ObjectImpl for SimpleWindow {
4042

4143
// Connect our method `on_increment_clicked` to be called
4244
// when the increment button is clicked.
43-
increment.connect_clicked(clone!(@weak obj => move |_| {
44-
let imp = obj.imp();
45+
increment.connect_clicked(clone!(@weak self as imp => move |_| {
4546
imp.on_increment_clicked();
4647
}));
4748

48-
obj.add(&label);
49-
obj.set_titlebar(Some(&headerbar));
50-
obj.set_default_size(640, 480);
49+
instance.add(&label);
50+
instance.set_titlebar(Some(&headerbar));
51+
instance.set_default_size(640, 480);
5152

5253
self.widgets
5354
.set(WindowWidgets { label })

examples/composite_template/example_application_window/imp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ impl ObjectSubclass for ExampleApplicationWindow {
3838
}
3939

4040
impl ObjectImpl for ExampleApplicationWindow {
41-
fn constructed(&self, obj: &Self::Type) {
42-
obj.init_label();
43-
self.parent_constructed(obj);
41+
fn constructed(&self) {
42+
self.instance().init_label();
43+
self.parent_constructed();
4444
}
4545
}
4646

examples/list_box_model/model/imp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ impl ObjectSubclass for Model {
2222
impl ObjectImpl for Model {}
2323

2424
impl ListModelImpl for Model {
25-
fn item_type(&self, _list_model: &Self::Type) -> glib::Type {
25+
fn item_type(&self) -> glib::Type {
2626
RowData::static_type()
2727
}
28-
fn n_items(&self, _list_model: &Self::Type) -> u32 {
28+
fn n_items(&self) -> u32 {
2929
self.0.borrow().len() as u32
3030
}
31-
fn item(&self, _list_model: &Self::Type, position: u32) -> Option<glib::Object> {
31+
fn item(&self, position: u32) -> Option<glib::Object> {
3232
self.0
3333
.borrow()
3434
.get(position as usize)

examples/list_box_model/row_data/imp.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,7 @@ impl ObjectImpl for RowData {
5151
PROPERTIES.as_ref()
5252
}
5353

54-
fn set_property(
55-
&self,
56-
_obj: &Self::Type,
57-
_id: usize,
58-
value: &glib::Value,
59-
pspec: &glib::ParamSpec,
60-
) {
54+
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
6155
match pspec.name() {
6256
"name" => {
6357
let name = value
@@ -75,7 +69,7 @@ impl ObjectImpl for RowData {
7569
}
7670
}
7771

78-
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
72+
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
7973
match pspec.name() {
8074
"name" => self.name.borrow().to_value(),
8175
"count" => self.count.borrow().to_value(),

gtk/src/subclass/application.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ use crate::Window;
1212
pub trait GtkApplicationImpl:
1313
GtkApplicationImplExt + gio::subclass::prelude::ApplicationImpl
1414
{
15-
fn window_added(&self, application: &Self::Type, window: &Window) {
16-
self.parent_window_added(application, window)
15+
fn window_added(&self, window: &Window) {
16+
self.parent_window_added(window)
1717
}
1818

19-
fn window_removed(&self, application: &Self::Type, window: &Window) {
20-
self.parent_window_removed(application, window)
19+
fn window_removed(&self, window: &Window) {
20+
self.parent_window_removed(window)
2121
}
2222
}
2323

2424
pub trait GtkApplicationImplExt: ObjectSubclass {
25-
fn parent_window_added(&self, application: &Self::Type, window: &Window);
26-
fn parent_window_removed(&self, application: &Self::Type, window: &Window);
25+
fn parent_window_added(&self, window: &Window);
26+
fn parent_window_removed(&self, window: &Window);
2727
}
2828

2929
impl<T: GtkApplicationImpl> GtkApplicationImplExt for T {
30-
fn parent_window_added(&self, application: &Self::Type, window: &Window) {
30+
fn parent_window_added(&self, window: &Window) {
3131
unsafe {
3232
let data = T::type_data();
3333
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkApplicationClass;
3434
if let Some(f) = (*parent_class).window_added {
3535
f(
36-
application
36+
self.instance()
3737
.unsafe_cast_ref::<Application>()
3838
.to_glib_none()
3939
.0,
@@ -43,13 +43,13 @@ impl<T: GtkApplicationImpl> GtkApplicationImplExt for T {
4343
}
4444
}
4545

46-
fn parent_window_removed(&self, application: &Self::Type, window: &Window) {
46+
fn parent_window_removed(&self, window: &Window) {
4747
unsafe {
4848
let data = T::type_data();
4949
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkApplicationClass;
5050
if let Some(f) = (*parent_class).window_removed {
5151
f(
52-
application
52+
self.instance()
5353
.unsafe_cast_ref::<Application>()
5454
.to_glib_none()
5555
.0,
@@ -113,17 +113,15 @@ unsafe extern "C" fn application_window_added<T: GtkApplicationImpl>(
113113
) {
114114
let instance = &*(ptr as *mut T::Instance);
115115
let imp = instance.imp();
116-
let wrap: Borrowed<Application> = from_glib_borrow(ptr);
117116

118-
imp.window_added(wrap.unsafe_cast_ref(), &from_glib_borrow(wptr))
117+
imp.window_added(&from_glib_borrow(wptr))
119118
}
120119
unsafe extern "C" fn application_window_removed<T: GtkApplicationImpl>(
121120
ptr: *mut ffi::GtkApplication,
122121
wptr: *mut ffi::GtkWindow,
123122
) {
124123
let instance = &*(ptr as *mut T::Instance);
125124
let imp = instance.imp();
126-
let wrap: Borrowed<Application> = from_glib_borrow(ptr);
127125

128-
imp.window_removed(wrap.unsafe_cast_ref(), &from_glib_borrow(wptr))
126+
imp.window_removed(&from_glib_borrow(wptr))
129127
}

gtk/src/subclass/button.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@ use super::bin::BinImpl;
88
use crate::Button;
99

1010
pub trait ButtonImpl: ButtonImplExt + BinImpl {
11-
fn activate(&self, button: &Self::Type) {
12-
self.parent_activate(button)
11+
fn activate(&self) {
12+
self.parent_activate()
1313
}
1414

15-
fn clicked(&self, button: &Self::Type) {
16-
self.parent_clicked(button)
15+
fn clicked(&self) {
16+
self.parent_clicked()
1717
}
1818
}
1919

2020
pub trait ButtonImplExt: ObjectSubclass {
21-
fn parent_activate(&self, button: &Self::Type);
22-
fn parent_clicked(&self, button: &Self::Type);
21+
fn parent_activate(&self);
22+
fn parent_clicked(&self);
2323
}
2424

2525
impl<T: ButtonImpl> ButtonImplExt for T {
26-
fn parent_activate(&self, button: &Self::Type) {
26+
fn parent_activate(&self) {
2727
unsafe {
2828
let data = T::type_data();
2929
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkButtonClass;
3030
if let Some(f) = (*parent_class).activate {
31-
f(button.unsafe_cast_ref::<Button>().to_glib_none().0)
31+
f(self.instance().unsafe_cast_ref::<Button>().to_glib_none().0)
3232
}
3333
}
3434
}
3535

36-
fn parent_clicked(&self, button: &Self::Type) {
36+
fn parent_clicked(&self) {
3737
unsafe {
3838
let data = T::type_data();
3939
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkButtonClass;
4040
if let Some(f) = (*parent_class).clicked {
41-
f(button.unsafe_cast_ref::<Button>().to_glib_none().0)
41+
f(self.instance().unsafe_cast_ref::<Button>().to_glib_none().0)
4242
}
4343
}
4444
}
@@ -61,15 +61,13 @@ unsafe impl<T: ButtonImpl> IsSubclassable<T> for Button {
6161
unsafe extern "C" fn button_activate<T: ButtonImpl>(ptr: *mut ffi::GtkButton) {
6262
let instance = &*(ptr as *mut T::Instance);
6363
let imp = instance.imp();
64-
let wrap: Borrowed<Button> = from_glib_borrow(ptr);
6564

66-
imp.activate(wrap.unsafe_cast_ref())
65+
imp.activate()
6766
}
6867

6968
unsafe extern "C" fn button_clicked<T: ButtonImpl>(ptr: *mut ffi::GtkButton) {
7069
let instance = &*(ptr as *mut T::Instance);
7170
let imp = instance.imp();
72-
let wrap: Borrowed<Button> = from_glib_borrow(ptr);
7371

74-
imp.clicked(wrap.unsafe_cast_ref())
72+
imp.clicked()
7573
}

0 commit comments

Comments
 (0)