Make sure Notification cannot outlive Context

This commit is contained in:
Mika Attila 2015-03-07 14:21:17 +01:00
parent 2d06fb7cc3
commit 68b3d02ba2
2 changed files with 9 additions and 9 deletions

View File

@ -1,9 +1,7 @@
extern crate libnotify; extern crate libnotify;
fn main() { fn main() {
let n = {
let notify = libnotify::Context::new("hello").unwrap(); let notify = libnotify::Context::new("hello").unwrap();
notify.new_notification("Hello, ", "World!").unwrap() let n = notify.new_notification("Hello, ", "World!").unwrap();
};
n.show().unwrap(); n.show().unwrap();
} }

View File

@ -4,6 +4,7 @@ extern crate "libnotify-sys" as sys;
extern crate "glib-2_0-sys" as glib; extern crate "glib-2_0-sys" as glib;
use std::ffi::CString; use std::ffi::CString;
use std::marker::PhantomData;
use glib::types::{ use glib::types::{
TRUE, TRUE,
@ -61,7 +62,7 @@ impl Context {
return Err(NotificationCreationError::Unknown); return Err(NotificationCreationError::Unknown);
} }
Ok(Notification{handle: n}) Ok(Notification{handle: n, _phantom: PhantomData})
} }
} }
} }
@ -74,12 +75,13 @@ impl Drop for Context {
} }
} }
pub struct Notification { pub struct Notification<'a> {
handle: *mut sys::NotifyNotification handle: *mut sys::NotifyNotification,
_phantom: PhantomData<&'a Context>
} }
impl Notification { impl<'a> Notification<'a> {
pub fn show(&self) -> Result<(), ()> { pub fn show(&'a self) -> Result<(), ()> {
unsafe { unsafe {
let mut err: *mut glib::GError = std::ptr::null_mut(); let mut err: *mut glib::GError = std::ptr::null_mut();
sys::notify_notification_show(self.handle, &mut err); sys::notify_notification_show(self.handle, &mut err);