gstreamer_editing_services/
asset.rs1use std::{boxed::Box as Box_, pin::Pin};
4
5use glib::{prelude::*, translate::*};
6
7use crate::{ffi, Asset};
8
9impl Asset {
10 #[doc(alias = "ges_asset_request")]
18 pub fn request<T>(id: Option<&str>) -> Result<Asset, glib::Error>
19 where
20 T: StaticType + IsA<crate::Extractable>,
21 {
22 assert_initialized_main_thread!();
23 unsafe {
24 let mut error = std::ptr::null_mut();
25 let ret = ffi::ges_asset_request(
26 T::static_type().into_glib(),
27 id.to_glib_none().0,
28 &mut error,
29 );
30 if error.is_null() {
31 Ok(from_glib_full(ret))
32 } else {
33 Err(from_glib_full(error))
34 }
35 }
36 }
37
38 #[doc(alias = "ges_asset_request_async")]
55 pub fn request_async<T, P>(
56 id: Option<&str>,
57 cancellable: Option<&impl IsA<gio::Cancellable>>,
58 callback: P,
59 ) where
60 T: StaticType + IsA<crate::Extractable>,
61 P: FnOnce(Result<Asset, glib::Error>) + 'static,
62 {
63 assert_initialized_main_thread!();
64
65 let main_context = glib::MainContext::ref_thread_default();
66 let is_main_context_owner = main_context.is_owner();
67 let has_acquired_main_context = (!is_main_context_owner)
68 .then(|| main_context.acquire().ok())
69 .flatten();
70 assert!(
71 is_main_context_owner || has_acquired_main_context.is_some(),
72 "Async operations only allowed if the thread is owning the MainContext"
73 );
74
75 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
76 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
77 unsafe extern "C" fn request_async_trampoline<
78 P: FnOnce(Result<Asset, glib::Error>) + 'static,
79 >(
80 _source_object: *mut glib::gobject_ffi::GObject,
81 res: *mut gio::ffi::GAsyncResult,
82 user_data: glib::ffi::gpointer,
83 ) {
84 let mut error = std::ptr::null_mut();
85 let ret = ffi::ges_asset_request_finish(res, &mut error);
86 let result = if error.is_null() {
87 Ok(from_glib_full(ret))
88 } else {
89 Err(from_glib_full(error))
90 };
91 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
92 Box_::from_raw(user_data as *mut _);
93 let callback: P = callback.into_inner();
94 callback(result);
95 }
96 let callback = request_async_trampoline::<P>;
97 unsafe {
98 ffi::ges_asset_request_async(
99 T::static_type().into_glib(),
100 id.to_glib_none().0,
101 cancellable.map(|p| p.as_ref()).to_glib_none().0,
102 Some(callback),
103 Box_::into_raw(user_data) as *mut _,
104 );
105 }
106 }
107
108 pub fn request_future<T>(
116 id: Option<&str>,
117 ) -> Pin<Box_<dyn std::future::Future<Output = Result<Asset, glib::Error>> + 'static>>
118 where
119 T: StaticType + IsA<crate::Extractable> + 'static,
120 {
121 skip_assert_initialized!();
122 let id = id.map(ToOwned::to_owned);
123 Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
124 Self::request_async::<T, _>(
125 id.as_ref().map(::std::borrow::Borrow::borrow),
126 Some(cancellable),
127 move |res| {
128 send.resolve(res);
129 },
130 );
131 }))
132 }
133
134 #[doc(alias = "ges_asset_needs_reload")]
144 pub fn needs_reload<T>(id: Option<&str>) -> bool
145 where
146 T: StaticType + IsA<crate::Extractable>,
147 {
148 assert_initialized_main_thread!();
149 unsafe {
150 from_glib(ffi::ges_asset_needs_reload(
151 T::static_type().into_glib(),
152 id.to_glib_none().0,
153 ))
154 }
155 }
156}