From 68b3d02ba273d1970f23ccbb5d2a8f7ff37dcf05 Mon Sep 17 00:00:00 2001 From: Mika Attila Date: Sat, 7 Mar 2015 14:21:17 +0100 Subject: [PATCH] Make sure Notification cannot outlive Context --- examples/show.rs | 6 ++---- src/lib.rs | 12 +++++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/show.rs b/examples/show.rs index a38f10a2..c950e352 100644 --- a/examples/show.rs +++ b/examples/show.rs @@ -1,9 +1,7 @@ extern crate libnotify; fn main() { - let n = { - let notify = libnotify::Context::new("hello").unwrap(); - notify.new_notification("Hello, ", "World!").unwrap() - }; + let notify = libnotify::Context::new("hello").unwrap(); + let n = notify.new_notification("Hello, ", "World!").unwrap(); n.show().unwrap(); } diff --git a/src/lib.rs b/src/lib.rs index b35a484b..3e877469 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ extern crate "libnotify-sys" as sys; extern crate "glib-2_0-sys" as glib; use std::ffi::CString; +use std::marker::PhantomData; use glib::types::{ TRUE, @@ -61,7 +62,7 @@ impl Context { 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 { - handle: *mut sys::NotifyNotification +pub struct Notification<'a> { + handle: *mut sys::NotifyNotification, + _phantom: PhantomData<&'a Context> } -impl Notification { - pub fn show(&self) -> Result<(), ()> { +impl<'a> Notification<'a> { + pub fn show(&'a self) -> Result<(), ()> { unsafe { let mut err: *mut glib::GError = std::ptr::null_mut(); sys::notify_notification_show(self.handle, &mut err);