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

add simple deref macro #7

Merged
merged 4 commits into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions gio-subclass/tests/simple_application.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// adaptation of https://gitlab.gnome.org/GNOME/glib/blob/master/gio/tests/gapplication-example-cmdline2.c
use std::mem;
use std::ops;
use std::ptr;
use std::sync::{Once, ONCE_INIT};

Expand All @@ -17,6 +16,8 @@ use glib::prelude::*;
use glib::translate::*;

extern crate gio_subclass;

#[macro_use]
extern crate gobject_subclass;

use gio_subclass::application::*;
Expand Down Expand Up @@ -127,21 +128,8 @@ glib_wrapper! {
}
}

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

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

impl SimpleApplication {
pub fn new<'a, I: Into<Option<&'a str>>>(
Expand Down
25 changes: 25 additions & 0 deletions gobject-subclass/src/deref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[macro_export]
macro_rules! gobject_subclass_deref(
($name:ident, $base:ident) => {
gobject_subclass_deref!($name, imp::$name, $base);
};

($name:ident, $target:ty, $base:ident) => {
use std::ops::Deref;

impl Deref for $name {
type Target = $target;

fn deref(&self) -> &Self::Target {
unsafe {
let base: $base = from_glib_borrow(self.to_glib_none().0);
let imp = base.get_impl();
let imp = imp.downcast_ref::<$target>().unwrap();
// Cast to a raw pointer to get us an appropriate lifetime: the compiler
// can't know that the lifetime of base is the same as the one of self
&*(imp as *const $target)
}
}
}
}
);
3 changes: 3 additions & 0 deletions gobject-subclass/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ pub mod guard;
pub mod properties;
#[macro_use]
pub mod object;

#[macro_use]
mod deref;
18 changes: 2 additions & 16 deletions gobject-subclass/tests/simple_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// except according to those terms.

use std::mem;
use std::ops;
use std::ptr;
use std::sync::{Once, ONCE_INIT};

Expand All @@ -19,6 +18,7 @@ extern crate glib;
use glib::prelude::*;
use glib::translate::*;

#[macro_use]
extern crate gobject_subclass;
use gobject_subclass::object::*;

Expand Down Expand Up @@ -172,21 +172,7 @@ impl SimpleObject {
}
}

// TODO: This one should probably get a macro
impl ops::Deref for SimpleObject {
type Target = imp::SimpleObject;

fn deref(&self) -> &Self::Target {
unsafe {
let base: Object = from_glib_borrow(self.to_glib_none().0);
let imp = base.get_impl();
let imp = imp.downcast_ref::<imp::SimpleObject>().unwrap();
// Cast to a raw pointer to get us an appropriate lifetime: the compiler
// can't know that the lifetime of base is the same as the one of self
&*(imp as *const imp::SimpleObject)
}
}
}
gobject_subclass_deref!(SimpleObject, Object);

#[test]
fn test_create() {
Expand Down