Make sure Notification cannot outlive Context
This commit is contained in:
parent
2d06fb7cc3
commit
68b3d02ba2
@ -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();
|
let n = notify.new_notification("Hello, ", "World!").unwrap();
|
||||||
notify.new_notification("Hello, ", "World!").unwrap()
|
|
||||||
};
|
|
||||||
n.show().unwrap();
|
n.show().unwrap();
|
||||||
}
|
}
|
||||||
|
12
src/lib.rs
12
src/lib.rs
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user