diff --git a/gstreamer-rtsp-server/src/rtsp_session_pool.rs b/gstreamer-rtsp-server/src/rtsp_session_pool.rs index edf5d937b..1ff1b935c 100644 --- a/gstreamer-rtsp-server/src/rtsp_session_pool.rs +++ b/gstreamer-rtsp-server/src/rtsp_session_pool.rs @@ -9,25 +9,26 @@ use std::cell::RefCell; use std::mem::transmute; use RTSPSessionPool; -unsafe extern "C" fn trampoline_watch( +unsafe extern "C" fn trampoline_watch Continue + Send + 'static>( pool: *mut ffi::GstRTSPSessionPool, func: gpointer, ) -> gboolean { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &RefCell Continue + Send + 'static>> = transmute(func); + let func: &RefCell = transmute(func); (&mut *func.borrow_mut())(&from_glib_borrow(pool)).to_glib() } -unsafe extern "C" fn destroy_closure_watch(ptr: gpointer) { - Box:: Continue + Send + 'static>>>::from_raw( - ptr as *mut _, - ); +unsafe extern "C" fn destroy_closure_watch< + F: FnMut(&RTSPSessionPool) -> Continue + Send + 'static, +>( + ptr: gpointer, +) { + Box::>::from_raw(ptr as *mut _); } fn into_raw_watch Continue + Send + 'static>(func: F) -> gpointer { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] - let func: Box Continue + Send + 'static>>> = - Box::new(RefCell::new(Box::new(func))); + let func: Box> = Box::new(RefCell::new(func)); Box::into_raw(func) as gpointer } @@ -55,12 +56,11 @@ impl> RTSPSessionPoolExtManual for O { skip_assert_initialized!(); unsafe { let source = ffi::gst_rtsp_session_pool_create_watch(self.as_ref().to_glib_none().0); - let trampoline = trampoline_watch as gpointer; glib_ffi::g_source_set_callback( source, - Some(transmute(trampoline)), + Some(transmute(trampoline_watch:: as usize)), into_raw_watch(func), - Some(destroy_closure_watch), + Some(destroy_closure_watch::), ); glib_ffi::g_source_set_priority(source, priority.to_glib()); diff --git a/gstreamer-video/src/functions.rs b/gstreamer-video/src/functions.rs index ead7dc9ca..3865d2a09 100644 --- a/gstreamer-video/src/functions.rs +++ b/gstreamer-video/src/functions.rs @@ -55,7 +55,7 @@ pub fn convert_sample_async( F: FnOnce(Result) + Send + 'static, { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let callback: &mut Option> = mem::transmute(user_data); + let callback: &mut Option = mem::transmute(user_data); let callback = callback.take().unwrap(); if error.is_null() { @@ -68,11 +68,11 @@ pub fn convert_sample_async( where F: FnOnce(Result) + Send + 'static, { - let _: Box>> = Box::from_raw(user_data as *mut _); + let _: Box> = Box::from_raw(user_data as *mut _); } unsafe { - let user_data: Box>> = Box::new(Some(Box::new(func))); + let user_data: Box> = Box::new(Some(func)); ffi::gst_video_convert_sample_async( sample.to_glib_none().0, diff --git a/gstreamer/src/bus.rs b/gstreamer/src/bus.rs index cc12b8993..6fd533ac6 100644 --- a/gstreamer/src/bus.rs +++ b/gstreamer/src/bus.rs @@ -20,36 +20,39 @@ use Bus; use BusSyncReply; use Message; -unsafe extern "C" fn trampoline_watch( +unsafe extern "C" fn trampoline_watch Continue + Send + 'static>( bus: *mut ffi::GstBus, msg: *mut ffi::GstMessage, func: gpointer, ) -> gboolean { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &RefCell Continue + Send + 'static>> = transmute(func); + let func: &RefCell = transmute(func); (&mut *func.borrow_mut())(&from_glib_borrow(bus), &Message::from_glib_borrow(msg)).to_glib() } -unsafe extern "C" fn destroy_closure_watch(ptr: gpointer) { - Box:: Continue + Send + 'static>>>::from_raw( - ptr as *mut _, - ); +unsafe extern "C" fn destroy_closure_watch< + F: FnMut(&Bus, &Message) -> Continue + Send + 'static, +>( + ptr: gpointer, +) { + Box::>::from_raw(ptr as *mut _); } fn into_raw_watch Continue + Send + 'static>(func: F) -> gpointer { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] - let func: Box Continue + Send + 'static>>> = - Box::new(RefCell::new(Box::new(func))); + let func: Box> = Box::new(RefCell::new(func)); Box::into_raw(func) as gpointer } -unsafe extern "C" fn trampoline_sync( +unsafe extern "C" fn trampoline_sync< + F: Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static, +>( bus: *mut ffi::GstBus, msg: *mut ffi::GstMessage, func: gpointer, ) -> ffi::GstBusSyncReply { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let f: &&(Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static) = transmute(func); + let f: &F = transmute(func); let res = f(&from_glib_borrow(bus), &Message::from_glib_borrow(msg)).to_glib(); if res == ffi::GST_BUS_DROP { @@ -59,15 +62,18 @@ unsafe extern "C" fn trampoline_sync( res } -unsafe extern "C" fn destroy_closure_sync(ptr: gpointer) { - Box:: BusSyncReply + Send + Sync + 'static>>::from_raw(ptr as *mut _); +unsafe extern "C" fn destroy_closure_sync< + F: Fn(&Bus, &Message) -> BusSyncReply + Send + Sync + 'static, +>( + ptr: gpointer, +) { + Box::::from_raw(ptr as *mut _); } fn into_raw_sync BusSyncReply + Send + Sync + 'static>( func: F, ) -> gpointer { - let func: Box BusSyncReply + Send + Sync + 'static>> = - Box::new(Box::new(func)); + let func: Box = Box::new(func); Box::into_raw(func) as gpointer } @@ -90,12 +96,11 @@ impl Bus { skip_assert_initialized!(); unsafe { let source = ffi::gst_bus_create_watch(self.to_glib_none().0); - let trampoline = trampoline_watch as gpointer; glib_ffi::g_source_set_callback( source, - Some(transmute(trampoline)), + Some(transmute(trampoline_watch:: as usize)), into_raw_watch(func), - Some(destroy_closure_watch), + Some(destroy_closure_watch::), ); glib_ffi::g_source_set_priority(source, priority.to_glib()); @@ -116,9 +121,9 @@ impl Bus { from_glib(ffi::gst_bus_add_watch_full( self.to_glib_none().0, glib_ffi::G_PRIORITY_DEFAULT, - Some(trampoline_watch), + Some(trampoline_watch::), into_raw_watch(func), - Some(destroy_closure_watch), + Some(destroy_closure_watch::), )) } } @@ -130,9 +135,9 @@ impl Bus { unsafe { ffi::gst_bus_set_sync_handler( self.to_glib_none().0, - Some(trampoline_sync), + Some(trampoline_sync::), into_raw_sync(func), - Some(destroy_closure_sync), + Some(destroy_closure_sync::), ) } } diff --git a/gstreamer/src/clock.rs b/gstreamer/src/clock.rs index 9eef70b17..4e6795950 100644 --- a/gstreamer/src/clock.rs +++ b/gstreamer/src/clock.rs @@ -33,14 +33,14 @@ glib_wrapper! { } } -unsafe extern "C" fn trampoline_wait_async( +unsafe extern "C" fn trampoline_wait_async( clock: *mut ffi::GstClock, time: ffi::GstClockTime, id: gpointer, func: gpointer, ) -> gboolean { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let f: &&(Fn(&Clock, ClockTime, &ClockId) + Send + 'static) = transmute(func); + let f: &F = transmute(func); f( &from_glib_borrow(clock), from_glib(time), @@ -49,13 +49,17 @@ unsafe extern "C" fn trampoline_wait_async( glib_ffi::GTRUE } -unsafe extern "C" fn destroy_closure_wait_async(ptr: gpointer) { - Box::>::from_raw(ptr as *mut _); +unsafe extern "C" fn destroy_closure_wait_async< + F: Fn(&Clock, ClockTime, &ClockId) + Send + 'static, +>( + ptr: gpointer, +) { + Box::::from_raw(ptr as *mut _); } fn into_raw_wait_async(func: F) -> gpointer { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] - let func: Box> = Box::new(Box::new(func)); + let func: Box = Box::new(func); Box::into_raw(func) as gpointer } @@ -84,9 +88,9 @@ impl ClockId { let ret: ClockReturn = unsafe { from_glib(ffi::gst_clock_id_wait_async( self.to_glib_none().0, - Some(trampoline_wait_async), + Some(trampoline_wait_async::), into_raw_wait_async(func), - Some(destroy_closure_wait_async), + Some(destroy_closure_wait_async::), )) }; ret.into_result() diff --git a/gstreamer/src/element.rs b/gstreamer/src/element.rs index 3de91cbc3..4ea7f79ec 100644 --- a/gstreamer/src/element.rs +++ b/gstreamer/src/element.rs @@ -652,13 +652,13 @@ impl> ElementExtManual for O { where F: FnOnce(&Self) + Send + 'static, { - let user_data: Box>> = Box::new(Some(Box::new(func))); + let user_data: Box> = Box::new(Some(func)); unsafe extern "C" fn trampoline, F: FnOnce(&O) + Send + 'static>( element: *mut ffi::GstElement, user_data: glib_ffi::gpointer, ) { - let user_data: &mut Option> = &mut *(user_data as *mut _); + let user_data: &mut Option = &mut *(user_data as *mut _); let callback = user_data.take().unwrap(); callback(&Element::from_glib_borrow(element).unsafe_cast()); @@ -667,17 +667,15 @@ impl> ElementExtManual for O { unsafe extern "C" fn free_user_data, F: FnOnce(&O) + Send + 'static>( user_data: glib_ffi::gpointer, ) { - let _: Box>> = Box::from_raw(user_data as *mut _); + let _: Box> = Box::from_raw(user_data as *mut _); } - let trampoline = trampoline::; - let free_user_data = free_user_data::; unsafe { ffi::gst_element_call_async( self.as_ref().to_glib_none().0, - Some(trampoline), + Some(trampoline::), Box::into_raw(user_data) as *mut _, - Some(free_user_data), + Some(free_user_data::), ); } } diff --git a/gstreamer/src/iterator.rs b/gstreamer/src/iterator.rs index 1d70ad322..65c9c73c9 100644 --- a/gstreamer/src/iterator.rs +++ b/gstreamer/src/iterator.rs @@ -90,12 +90,7 @@ where mem::forget(self); let func_box: Box bool + Send + Sync + 'static> = Box::new(func); - // FIXME: Use Value::from_type once we depend on new enough GLib - let mut closure_value = glib::Value::uninitialized(); - gobject_ffi::g_value_init( - closure_value.to_glib_none_mut().0, - filter_boxed_get_type::(), - ); + let mut closure_value = glib::Value::from_type(from_glib(filter_boxed_get_type::())); gobject_ffi::g_value_set_boxed( closure_value.to_glib_none_mut().0, Arc::into_raw(Arc::new(func_box)) as gpointer, @@ -117,12 +112,11 @@ where let mut elem = glib::Value::uninitialized(); let mut func = func; - let func_obj: &mut (FnMut(T) -> bool) = &mut func; - let func_ptr = &func_obj as *const &mut (FnMut(T) -> bool) as gpointer; + let func_ptr = &mut func as *mut F as gpointer; let res = from_glib(ffi::gst_iterator_find_custom( self.to_glib_none_mut().0, - Some(find_trampoline::), + Some(find_trampoline::), elem.to_glib_none_mut().0, func_ptr, )); @@ -140,12 +134,11 @@ where { unsafe { let mut func = func; - let func_obj: &mut (FnMut(T)) = &mut func; - let func_ptr = &func_obj as *const &mut (FnMut(T)) as gpointer; + let func_ptr = &mut func as *mut F as gpointer; let res = ffi::gst_iterator_foreach( self.to_glib_none_mut().0, - Some(foreach_trampoline::), + Some(foreach_trampoline::), func_ptr, ); @@ -163,13 +156,10 @@ where { unsafe { let mut func = func; - let func_obj: &mut (FnMut(U, T) -> Result) = &mut func; - let func_ptr = &func_obj as *const &mut (FnMut(U, T) -> Result) as gpointer; + let func_ptr = &mut func as *mut F as gpointer; let mut accum = Some(init); - // FIXME: Use Value::from_type once we depend on new enough GLib - let mut ret = glib::Value::uninitialized(); - gobject_ffi::g_value_init(ret.to_glib_none_mut().0, gobject_ffi::G_TYPE_POINTER); + let mut ret = glib::Value::from_type(glib::Type::Pointer); gobject_ffi::g_value_set_pointer( ret.to_glib_none_mut().0, &mut accum as *mut _ as gpointer, @@ -177,7 +167,7 @@ where let res = ffi::gst_iterator_fold( self.to_glib_none_mut().0, - Some(fold_trampoline::), + Some(fold_trampoline::), ret.to_glib_none_mut().0, func_ptr, ); @@ -405,13 +395,16 @@ unsafe extern "C" fn filter_boxed_get_type() -> glib_ff TYPE } -unsafe extern "C" fn find_trampoline(value: gconstpointer, func: gconstpointer) -> i32 +unsafe extern "C" fn find_trampoline bool>( + value: gconstpointer, + func: gconstpointer, +) -> i32 where for<'a> T: FromValueOptional<'a> + 'static, { let value = value as *const gobject_ffi::GValue; - let func = func as *mut &mut (FnMut(T) -> bool); + let func = func as *mut F; let value = &*(value as *const glib::Value); let value = value.get::().unwrap(); @@ -422,18 +415,20 @@ where } } -unsafe extern "C" fn foreach_trampoline(value: *const gobject_ffi::GValue, func: gpointer) -where +unsafe extern "C" fn foreach_trampoline( + value: *const gobject_ffi::GValue, + func: gpointer, +) where for<'a> T: FromValueOptional<'a> + 'static, { - let func = func as *mut &mut (FnMut(T)); + let func = func as *mut F; let value = &*(value as *const glib::Value); let value = value.get::().unwrap(); (*func)(value); } -unsafe extern "C" fn fold_trampoline( +unsafe extern "C" fn fold_trampoline Result>( value: *const gobject_ffi::GValue, ret: *mut gobject_ffi::GValue, func: gpointer, @@ -441,7 +436,7 @@ unsafe extern "C" fn fold_trampoline( where for<'a> T: FromValueOptional<'a> + 'static, { - let func = func as *mut &mut (FnMut(U, T) -> Result); + let func = func as *mut F; let value = &*(value as *const glib::Value); let value = value.get::().unwrap(); diff --git a/gstreamer/src/pad.rs b/gstreamer/src/pad.rs index e27b0529a..d56d9d9ea 100644 --- a/gstreamer/src/pad.rs +++ b/gstreamer/src/pad.rs @@ -35,10 +35,10 @@ use std::mem::transmute; use std::ptr; use glib; -use glib::object::IsA; +use glib::object::{Cast, IsA}; use glib::translate::{ - from_glib, from_glib_borrow, from_glib_full, from_glib_none, mut_override, FromGlib, ToGlib, - ToGlibPtr, + from_glib, from_glib_borrow, from_glib_full, from_glib_none, mut_override, FromGlib, + FromGlibPtrBorrow, ToGlib, ToGlibPtr, }; use glib::StaticType; use glib_ffi; @@ -116,7 +116,7 @@ impl Drop for StreamLock { pub trait PadExtManual: 'static { fn add_probe(&self, mask: PadProbeType, func: F) -> Option where - F: Fn(&Pad, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static; + F: Fn(&Self, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static; fn remove_probe(&self, id: PadProbeId); fn chain(&self, buffer: Buffer) -> Result; @@ -165,65 +165,65 @@ pub trait PadExtManual: 'static { fn set_activate_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static; + F: Fn(&Self, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static; fn set_activatemode_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError> + F: Fn(&Self, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError> + Send + Sync + 'static; fn set_chain_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::Buffer) -> Result + F: Fn(&Self, &Option<::Object>, ::Buffer) -> Result + Send + Sync + 'static; fn set_chain_list_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::BufferList) -> Result + F: Fn(&Self, &Option<::Object>, ::BufferList) -> Result + Send + Sync + 'static; fn set_event_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::Event) -> bool + Send + Sync + 'static; + F: Fn(&Self, &Option<::Object>, ::Event) -> bool + Send + Sync + 'static; fn set_event_full_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::Event) -> Result + F: Fn(&Self, &Option<::Object>, ::Event) -> Result + Send + Sync + 'static; fn set_getrange_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowError> + F: Fn(&Self, &Option<::Object>, u64, u32) -> Result<::Buffer, ::FlowError> + Send + Sync + 'static; fn set_iterate_internal_links_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>) -> ::Iterator + Send + Sync + 'static; + F: Fn(&Self, &Option<::Object>) -> ::Iterator + Send + Sync + 'static; fn set_link_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, &Pad) -> Result<::PadLinkSuccess, ::PadLinkError> + F: Fn(&Self, &Option<::Object>, &Pad) -> Result<::PadLinkSuccess, ::PadLinkError> + Send + Sync + 'static; fn set_query_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, &mut ::QueryRef) -> bool + Send + Sync + 'static; + F: Fn(&Self, &Option<::Object>, &mut ::QueryRef) -> bool + Send + Sync + 'static; fn set_unlink_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>) + Send + Sync + 'static; + F: Fn(&Self, &Option<::Object>) + Send + Sync + 'static; fn start_task(&self, func: F) -> Result<(), glib::BoolError>; @@ -272,18 +272,16 @@ pub trait PadExtManual: 'static { impl> PadExtManual for O { fn add_probe(&self, mask: PadProbeType, func: F) -> Option where - F: Fn(&Pad, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static, + F: Fn(&Self, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static, { unsafe { - let func_box: Box< - Fn(&Pad, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); let id = ffi::gst_pad_add_probe( self.as_ref().to_glib_none().0, mask.to_glib(), - Some(trampoline_pad_probe), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_pad_probe::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); if id == 0 { @@ -517,224 +515,185 @@ impl> PadExtManual for O { fn set_activate_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static, + F: Fn(&Self, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static, { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_activate_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_activate_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_activate_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_activatemode_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError> + F: Fn(&Self, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError> + Send + Sync + 'static, { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError> - + Send - + Sync - + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_activatemode_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_activatemode_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_activatemode_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_chain_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::Buffer) -> Result + F: Fn(&Self, &Option<::Object>, ::Buffer) -> Result + Send + Sync + 'static, { unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>, ::Buffer) -> Result - + Send - + Sync - + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_chain_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_chain_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_chain_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_chain_list_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::BufferList) -> Result + F: Fn(&Self, &Option<::Object>, ::BufferList) -> Result + Send + Sync + 'static, { unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>, ::BufferList) -> Result - + Send - + Sync - + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_chain_list_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_chain_list_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_chain_list_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_event_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::Event) -> bool + Send + Sync + 'static, + F: Fn(&Self, &Option<::Object>, ::Event) -> bool + Send + Sync + 'static, { unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>, ::Event) -> bool + Send + Sync + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_event_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_event_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_event_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_event_full_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, ::Event) -> Result + F: Fn(&Self, &Option<::Object>, ::Event) -> Result + Send + Sync + 'static, { unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>, ::Event) -> Result - + Send - + Sync - + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_event_full_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_event_full_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_event_full_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_getrange_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, FlowError> + F: Fn(&Self, &Option<::Object>, u64, u32) -> Result<::Buffer, FlowError> + Send + Sync + 'static, { unsafe { - #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] - let func_box: Box< - Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, FlowError> - + Send - + Sync - + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_getrange_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_getrange_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_getrange_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_iterate_internal_links_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>) -> ::Iterator + Send + Sync + 'static, + F: Fn(&Self, &Option<::Object>) -> ::Iterator + Send + Sync + 'static, { unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>) -> ::Iterator + Send + Sync + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_iterate_internal_links_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_iterate_internal_links_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_iterate_internal_links_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_link_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, &Pad) -> Result<::PadLinkSuccess, ::PadLinkError> + F: Fn(&Self, &Option<::Object>, &Pad) -> Result<::PadLinkSuccess, ::PadLinkError> + Send + Sync + 'static, { unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>, &Pad) -> Result<::PadLinkSuccess, ::PadLinkError> - + Send - + Sync - + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_link_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_link_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_link_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_query_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>, &mut ::QueryRef) -> bool + Send + Sync + 'static, + F: Fn(&Self, &Option<::Object>, &mut ::QueryRef) -> bool + Send + Sync + 'static, { unsafe { - let func_box: Box< - Fn(&Pad, &Option<::Object>, &mut ::QueryRef) -> bool + Send + Sync + 'static, - > = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_query_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_query_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_query_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } fn set_unlink_function(&self, func: F) where - F: Fn(&Pad, &Option<::Object>) + Send + Sync + 'static, + F: Fn(&Self, &Option<::Object>) + Send + Sync + 'static, { unsafe { - let func_box: Box) + Send + Sync + 'static> = Box::new(func); + let func_box: Box = Box::new(func); ffi::gst_pad_set_unlink_function_full( self.as_ref().to_glib_none().0, - Some(trampoline_unlink_function), - Box::into_raw(Box::new(func_box)) as gpointer, - Some(destroy_closure), + Some(trampoline_unlink_function::), + Box::into_raw(func_box) as gpointer, + Some(destroy_closure::), ); } } @@ -744,9 +703,9 @@ impl> PadExtManual for O { glib_result_from_gboolean!( ffi::gst_pad_start_task( self.as_ref().to_glib_none().0, - Some(trampoline_pad_task), + Some(trampoline_pad_task::), into_raw_pad_task(func), - Some(destroy_closure_pad_task), + Some(destroy_closure::), ), "Failed to start pad task", ) @@ -1039,14 +998,19 @@ impl> PadExtManual for O { } } -unsafe extern "C" fn trampoline_pad_probe( +unsafe extern "C" fn trampoline_pad_probe< + T, + F: Fn(&T, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, info: *mut ffi::GstPadProbeInfo, func: gpointer, -) -> ffi::GstPadProbeReturn { +) -> ffi::GstPadProbeReturn +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &mut PadProbeInfo) -> PadProbeReturn + Send + Sync + 'static) = - transmute(func); + let func: &F = transmute(func); let mut data_type = None; let mut probe_info = PadProbeInfo { @@ -1085,7 +1049,7 @@ unsafe extern "C" fn trampoline_pad_probe( }, }; - let ret = func(&from_glib_borrow(pad), &mut probe_info).to_glib(); + let ret = func(&Pad::from_glib_borrow(pad).unsafe_cast(), &mut probe_info).to_glib(); match probe_info.data { Some(PadProbeData::Buffer(buffer)) => { @@ -1115,68 +1079,78 @@ unsafe extern "C" fn trampoline_pad_probe( ret } -unsafe extern "C" fn trampoline_activate_function( +unsafe extern "C" fn trampoline_activate_function< + T, + F: Fn(&T, &Option<::Object>) -> Result<(), LoggableError> + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, -) -> glib_ffi::gboolean { +) -> glib_ffi::gboolean +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>) -> Result<(), LoggableError> - + Send - + Sync - + 'static) = transmute((*pad).activatedata); + let func: &F = transmute((*pad).activatedata); - let pad: Pad = from_glib_borrow(pad); - match func(&pad, &from_glib_borrow(parent)) { + match func( + &Pad::from_glib_borrow(pad).unsafe_cast(), + &from_glib_borrow(parent), + ) { Ok(()) => true, Err(err) => { - err.log_with_object(&pad); + err.log_with_object(&Pad::from_glib_borrow(pad)); false } } .to_glib() } -unsafe extern "C" fn trampoline_activatemode_function( +unsafe extern "C" fn trampoline_activatemode_function< + T, + F: Fn(&T, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError> + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, mode: ffi::GstPadMode, active: glib_ffi::gboolean, -) -> glib_ffi::gboolean { +) -> glib_ffi::gboolean +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>, ::PadMode, bool) -> Result<(), LoggableError> - + Send - + Sync - + 'static) = transmute((*pad).activatemodedata); + let func: &F = transmute((*pad).activatemodedata); - let pad: Pad = from_glib_borrow(pad); match func( - &pad, + &&Pad::from_glib_borrow(pad).unsafe_cast(), &from_glib_borrow(parent), from_glib(mode), from_glib(active), ) { Ok(()) => true, Err(err) => { - err.log_with_object(&pad); + err.log_with_object(&Pad::from_glib_borrow(pad)); false } } .to_glib() } -unsafe extern "C" fn trampoline_chain_function( +unsafe extern "C" fn trampoline_chain_function< + T, + F: Fn(&T, &Option<::Object>, ::Buffer) -> Result + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, buffer: *mut ffi::GstBuffer, -) -> ffi::GstFlowReturn { +) -> ffi::GstFlowReturn +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>, ::Buffer) -> Result - + Send - + Sync - + 'static) = transmute((*pad).chaindata); + let func: &F = transmute((*pad).chaindata); let res: FlowReturn = func( - &from_glib_borrow(pad), + &Pad::from_glib_borrow(pad).unsafe_cast(), &from_glib_borrow(parent), from_glib_full(buffer), ) @@ -1184,19 +1158,25 @@ unsafe extern "C" fn trampoline_chain_function( res.to_glib() } -unsafe extern "C" fn trampoline_chain_list_function( +unsafe extern "C" fn trampoline_chain_list_function< + T, + F: Fn(&T, &Option<::Object>, ::BufferList) -> Result + + Send + + Sync + + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, list: *mut ffi::GstBufferList, -) -> ffi::GstFlowReturn { +) -> ffi::GstFlowReturn +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>, ::BufferList) -> Result - + Send - + Sync - + 'static) = transmute((*pad).chainlistdata); + let func: &F = transmute((*pad).chainlistdata); let res: FlowReturn = func( - &from_glib_borrow(pad), + &Pad::from_glib_borrow(pad).unsafe_cast(), &from_glib_borrow(parent), from_glib_full(list), ) @@ -1204,36 +1184,44 @@ unsafe extern "C" fn trampoline_chain_list_function( res.to_glib() } -unsafe extern "C" fn trampoline_event_function( +unsafe extern "C" fn trampoline_event_function< + T, + F: Fn(&T, &Option<::Object>, ::Event) -> bool + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, event: *mut ffi::GstEvent, -) -> glib_ffi::gboolean { +) -> glib_ffi::gboolean +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>, ::Event) -> bool + Send + Sync + 'static) = - transmute((*pad).eventdata); + let func: &F = transmute((*pad).eventdata); func( - &from_glib_borrow(pad), + &Pad::from_glib_borrow(pad).unsafe_cast(), &from_glib_borrow(parent), from_glib_full(event), ) .to_glib() } -unsafe extern "C" fn trampoline_event_full_function( +unsafe extern "C" fn trampoline_event_full_function< + T, + F: Fn(&T, &Option<::Object>, ::Event) -> Result + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, event: *mut ffi::GstEvent, -) -> ffi::GstFlowReturn { +) -> ffi::GstFlowReturn +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>, ::Event) -> Result - + Send - + Sync - + 'static) = transmute((*pad).eventdata); + let func: &F = transmute((*pad).eventdata); let res: FlowReturn = func( - &from_glib_borrow(pad), + &Pad::from_glib_borrow(pad).unsafe_cast(), &from_glib_borrow(parent), from_glib_full(event), ) @@ -1241,21 +1229,24 @@ unsafe extern "C" fn trampoline_event_full_function( res.to_glib() } -unsafe extern "C" fn trampoline_getrange_function( +unsafe extern "C" fn trampoline_getrange_function< + T, + F: Fn(&T, &Option<::Object>, u64, u32) -> Result<::Buffer, FlowError> + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, offset: u64, length: u32, buffer: *mut *mut ffi::GstBuffer, -) -> ffi::GstFlowReturn { +) -> ffi::GstFlowReturn +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>, u64, u32) -> Result<::Buffer, FlowError> - + Send - + Sync - + 'static) = transmute((*pad).getrangedata); + let func: &F = transmute((*pad).getrangedata); match func( - &from_glib_borrow(pad), + &Pad::from_glib_borrow(pad).unsafe_cast(), &from_glib_borrow(parent), offset, length, @@ -1268,39 +1259,49 @@ unsafe extern "C" fn trampoline_getrange_function( } } -unsafe extern "C" fn trampoline_iterate_internal_links_function( +unsafe extern "C" fn trampoline_iterate_internal_links_function< + T, + F: Fn(&T, &Option<::Object>) -> ::Iterator + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, -) -> *mut ffi::GstIterator { +) -> *mut ffi::GstIterator +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>) -> ::Iterator + Send + Sync + 'static) = - transmute((*pad).iterintlinkdata); + let func: &F = transmute((*pad).iterintlinkdata); // Steal the iterator and return it - let ret = func(&from_glib_borrow(pad), &from_glib_borrow(parent)); + let ret = func( + &Pad::from_glib_borrow(pad).unsafe_cast(), + &from_glib_borrow(parent), + ); let ptr = ret.to_glib_none().0; mem::forget(ret); ptr as *mut _ } -unsafe extern "C" fn trampoline_link_function( +unsafe extern "C" fn trampoline_link_function< + T, + F: Fn(&T, &Option<::Object>, &::Pad) -> Result<::PadLinkSuccess, ::PadLinkError> + + Send + + Sync + + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, peer: *mut ffi::GstPad, -) -> ffi::GstPadLinkReturn { +) -> ffi::GstPadLinkReturn +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn( - &Pad, - &Option<::Object>, - &::Pad, - ) -> Result<::PadLinkSuccess, ::PadLinkError> - + Send - + Sync - + 'static) = transmute((*pad).linkdata); + let func: &F = transmute((*pad).linkdata); let res: ::PadLinkReturn = func( - &from_glib_borrow(pad), + &Pad::from_glib_borrow(pad).unsafe_cast(), &from_glib_borrow(parent), &from_glib_borrow(peer), ) @@ -1308,51 +1309,59 @@ unsafe extern "C" fn trampoline_link_function( res.to_glib() } -unsafe extern "C" fn trampoline_query_function( +unsafe extern "C" fn trampoline_query_function< + T, + F: Fn(&T, &Option<::Object>, &mut ::QueryRef) -> bool + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, query: *mut ffi::GstQuery, -) -> glib_ffi::gboolean { +) -> glib_ffi::gboolean +where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>, &mut ::QueryRef) -> bool + Send + Sync + 'static) = - transmute((*pad).querydata); + let func: &F = transmute((*pad).querydata); func( - &from_glib_borrow(pad), + &Pad::from_glib_borrow(pad).unsafe_cast(), &from_glib_borrow(parent), ::QueryRef::from_mut_ptr(query), ) .to_glib() } -unsafe extern "C" fn trampoline_unlink_function( +unsafe extern "C" fn trampoline_unlink_function< + T, + F: Fn(&T, &Option<::Object>) + Send + Sync + 'static, +>( pad: *mut ffi::GstPad, parent: *mut ffi::GstObject, -) { +) where + T: IsA, +{ #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&Pad, &Option<::Object>) + Send + Sync + 'static) = - transmute((*pad).unlinkdata); + let func: &F = transmute((*pad).unlinkdata); - func(&from_glib_borrow(pad), &from_glib_borrow(parent)) + func( + &Pad::from_glib_borrow(pad).unsafe_cast(), + &from_glib_borrow(parent), + ) } -unsafe extern "C" fn destroy_closure(ptr: gpointer) { - Box::>::from_raw(ptr as *mut _); +unsafe extern "C" fn destroy_closure(ptr: gpointer) { + Box::::from_raw(ptr as *mut _); } -unsafe extern "C" fn trampoline_pad_task(func: gpointer) { +unsafe extern "C" fn trampoline_pad_task(func: gpointer) { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &RefCell> = transmute(func); + let func: &RefCell = transmute(func); (&mut *func.borrow_mut())() } -unsafe extern "C" fn destroy_closure_pad_task(ptr: gpointer) { - Box::>>::from_raw(ptr as *mut _); -} - fn into_raw_pad_task(func: F) -> gpointer { #[cfg_attr(feature = "cargo-clippy", allow(type_complexity))] - let func: Box>> = Box::new(RefCell::new(Box::new(func))); + let func: Box> = Box::new(RefCell::new(func)); Box::into_raw(func) as gpointer } diff --git a/gstreamer/src/promise.rs b/gstreamer/src/promise.rs index b2500d9ce..8f19d23f2 100644 --- a/gstreamer/src/promise.rs +++ b/gstreamer/src/promise.rs @@ -36,13 +36,13 @@ impl Promise { where F: FnOnce(&Promise) + Send + 'static, { - let user_data: Box>> = Box::new(Some(Box::new(func))); + let user_data: Box> = Box::new(Some(func)); unsafe extern "C" fn trampoline( promise: *mut ffi::GstPromise, user_data: glib_ffi::gpointer, ) { - let user_data: &mut Option> = &mut *(user_data as *mut _); + let user_data: &mut Option = &mut *(user_data as *mut _); let callback = user_data.take().unwrap(); callback(&from_glib_borrow(promise)); @@ -51,16 +51,14 @@ impl Promise { unsafe extern "C" fn free_user_data( user_data: glib_ffi::gpointer, ) { - let _: Box>> = Box::from_raw(user_data as *mut _); + let _: Box> = Box::from_raw(user_data as *mut _); } - let trampoline = trampoline::; - let free_user_data = free_user_data::; unsafe { from_glib_full(ffi::gst_promise_new_with_change_func( - Some(trampoline), + Some(trampoline::), Box::into_raw(user_data) as *mut _, - Some(free_user_data), + Some(free_user_data::), )) } } diff --git a/gstreamer/src/typefind.rs b/gstreamer/src/typefind.rs index e0414f794..98133852d 100644 --- a/gstreamer/src/typefind.rs +++ b/gstreamer/src/typefind.rs @@ -56,19 +56,18 @@ impl<'a> TypeFind<'a> { let extensions = extensions.into(); let possible_caps = possible_caps.into(); unsafe { - let func: Box> = - Box::new(Box::new(func)); + let func: Box = Box::new(func); let func = Box::into_raw(func); let res = ffi::gst_type_find_register( plugin.to_glib_none().0, name.to_glib_none().0, rank, - Some(type_find_trampoline), + Some(type_find_trampoline::), extensions.to_glib_none().0, possible_caps.to_glib_none().0, func as *mut _, - Some(type_find_closure_drop), + Some(type_find_closure_drop::), ); glib_result_from_gboolean!(res, "Failed to register typefind factory") @@ -116,17 +115,19 @@ impl TypeFindFactory { } } -unsafe extern "C" fn type_find_trampoline( +unsafe extern "C" fn type_find_trampoline( find: *mut ffi::GstTypeFind, user_data: glib_ffi::gpointer, ) { #[cfg_attr(feature = "cargo-clippy", allow(transmute_ptr_to_ref))] - let func: &&(Fn(&mut TypeFind) + Send + Sync + 'static) = mem::transmute(user_data); + let func: &F = mem::transmute(user_data); func(&mut *(find as *mut TypeFind)); } -unsafe extern "C" fn type_find_closure_drop(data: glib_ffi::gpointer) { - Box::>::from_raw(data as *mut _); +unsafe extern "C" fn type_find_closure_drop( + data: glib_ffi::gpointer, +) { + Box::::from_raw(data as *mut _); } unsafe extern "C" fn type_find_peek(data: glib_ffi::gpointer, offset: i64, size: u32) -> *const u8 {