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

Commit f2aedc0

Browse files
authored
add simple deref macro (#7)
* add simple deref macro * remove newline * remove unused import * Remove extra newline
1 parent 86e9de0 commit f2aedc0

File tree

4 files changed

+33
-31
lines changed

4 files changed

+33
-31
lines changed

gio-subclass/tests/simple_application.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// adaptation of https://gitlab.gnome.org/GNOME/glib/blob/master/gio/tests/gapplication-example-cmdline2.c
22
use std::mem;
3-
use std::ops;
43
use std::ptr;
54
use std::sync::{Once, ONCE_INIT};
65

@@ -17,6 +16,8 @@ use glib::prelude::*;
1716
use glib::translate::*;
1817

1918
extern crate gio_subclass;
19+
20+
#[macro_use]
2021
extern crate gobject_subclass;
2122

2223
use gio_subclass::application::*;
@@ -131,21 +132,8 @@ glib_wrapper! {
131132
}
132133
}
133134

134-
// TODO: This one should probably get a macro
135-
impl ops::Deref for SimpleApplication {
136-
type Target = imp::SimpleApplication;
135+
gobject_subclass_deref!(SimpleApplication, Application);
137136

138-
fn deref(&self) -> &Self::Target {
139-
unsafe {
140-
let base: Application = from_glib_borrow(self.to_glib_none().0);
141-
let imp = base.get_impl();
142-
let imp = imp.downcast_ref::<imp::SimpleApplication>().unwrap();
143-
// Cast to a raw pointer to get us an appropriate lifetime: the compiler
144-
// can't know that the lifetime of base is the same as the one of self
145-
&*(imp as *const imp::SimpleApplication)
146-
}
147-
}
148-
}
149137

150138
impl SimpleApplication {
151139
pub fn new<'a, I: Into<Option<&'a str>>>(

gobject-subclass/src/deref.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[macro_export]
2+
macro_rules! gobject_subclass_deref(
3+
($name:ident, $base:ident) => {
4+
gobject_subclass_deref!($name, imp::$name, $base);
5+
};
6+
7+
($name:ident, $target:ty, $base:ident) => {
8+
use std::ops::Deref;
9+
10+
impl Deref for $name {
11+
type Target = $target;
12+
13+
fn deref(&self) -> &Self::Target {
14+
unsafe {
15+
let base: $base = from_glib_borrow(self.to_glib_none().0);
16+
let imp = base.get_impl();
17+
let imp = imp.downcast_ref::<$target>().unwrap();
18+
// Cast to a raw pointer to get us an appropriate lifetime: the compiler
19+
// can't know that the lifetime of base is the same as the one of self
20+
&*(imp as *const $target)
21+
}
22+
}
23+
}
24+
}
25+
);

gobject-subclass/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ pub mod guard;
2323
pub mod properties;
2424
#[macro_use]
2525
pub mod object;
26+
27+
#[macro_use]
28+
mod deref;

gobject-subclass/tests/simple_object.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// except according to those terms.
88

99
use std::mem;
10-
use std::ops;
1110
use std::ptr;
1211
use std::sync::{Once, ONCE_INIT};
1312

@@ -19,6 +18,7 @@ extern crate glib;
1918
use glib::prelude::*;
2019
use glib::translate::*;
2120

21+
#[macro_use]
2222
extern crate gobject_subclass;
2323
use gobject_subclass::object::*;
2424

@@ -172,21 +172,7 @@ impl SimpleObject {
172172
}
173173
}
174174

175-
// TODO: This one should probably get a macro
176-
impl ops::Deref for SimpleObject {
177-
type Target = imp::SimpleObject;
178-
179-
fn deref(&self) -> &Self::Target {
180-
unsafe {
181-
let base: Object = from_glib_borrow(self.to_glib_none().0);
182-
let imp = base.get_impl();
183-
let imp = imp.downcast_ref::<imp::SimpleObject>().unwrap();
184-
// Cast to a raw pointer to get us an appropriate lifetime: the compiler
185-
// can't know that the lifetime of base is the same as the one of self
186-
&*(imp as *const imp::SimpleObject)
187-
}
188-
}
189-
}
175+
gobject_subclass_deref!(SimpleObject, Object);
190176

191177
#[test]
192178
fn test_create() {

0 commit comments

Comments
 (0)