1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Take a look at the license at the top of the repository in the LICENSE file.
use std::{boxed::Box as Box_, pin::Pin};
use glib::{prelude::*, translate::*};
use crate::{Asset, ffi};
impl Asset {
// rustdoc-stripper-ignore-next
/// Request an asset for a specific extractable type using generics.
///
/// # Example
/// ```ignore
/// let asset = ges::Asset::request::<ges::TestClip>(None)?;
/// ```
#[doc(alias = "ges_asset_request")]
pub fn request<T>(id: Option<&str>) -> Result<Asset, glib::Error>
where
T: StaticType + IsA<crate::Extractable>,
{
assert_initialized_main_thread!();
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::ges_asset_request(
T::static_type().into_glib(),
id.to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
// rustdoc-stripper-ignore-next
/// Request an asset asynchronously for a specific extractable type using generics.
///
/// # Example
/// ```ignore
/// ges::Asset::request_async::<ges::UriClip, _>(
/// Some("file:///path/to/file.mp4"),
/// None,
/// |result| {
/// match result {
/// Ok(asset) => println!("Asset loaded: {:?}", asset),
/// Err(err) => eprintln!("Failed to load asset: {}", err),
/// }
/// },
/// );
/// ```
#[doc(alias = "ges_asset_request_async")]
pub fn request_async<T, P>(
id: Option<&str>,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) where
T: StaticType + IsA<crate::Extractable>,
P: FnOnce(Result<Asset, glib::Error>) + 'static,
{
assert_initialized_main_thread!();
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn request_async_trampoline<
P: FnOnce(Result<Asset, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
unsafe {
let mut error = std::ptr::null_mut();
let ret = ffi::ges_asset_request_finish(res, &mut error);
let result = if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
}
let callback = request_async_trampoline::<P>;
unsafe {
ffi::ges_asset_request_async(
T::static_type().into_glib(),
id.to_glib_none().0,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
Some(callback),
Box_::into_raw(user_data) as *mut _,
);
}
}
// rustdoc-stripper-ignore-next
/// Request an asset as a future for a specific extractable type.
///
/// # Example
/// ```ignore
/// let asset = ges::Asset::request_future::<ges::UriClip>(Some("file:///path/to/file.mp4")).await?;
/// ```
pub fn request_future<T>(
id: Option<&str>,
) -> Pin<Box_<dyn std::future::Future<Output = Result<Asset, glib::Error>> + 'static>>
where
T: StaticType + IsA<crate::Extractable> + 'static,
{
skip_assert_initialized!();
let id = id.map(ToOwned::to_owned);
Box_::pin(gio::GioFuture::new(&(), move |_obj, cancellable, send| {
Self::request_async::<T, _>(
id.as_ref().map(::std::borrow::Borrow::borrow),
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
// rustdoc-stripper-ignore-next
/// Check if an asset needs to be reloaded for a specific extractable type.
///
/// # Example
/// ```ignore
/// if ges::Asset::needs_reload::<ges::UriClip>(Some("file:///path/to/file.mp4")) {
/// println!("Asset needs reload");
/// }
/// ```
#[doc(alias = "ges_asset_needs_reload")]
pub fn needs_reload<T>(id: Option<&str>) -> bool
where
T: StaticType + IsA<crate::Extractable>,
{
assert_initialized_main_thread!();
unsafe {
from_glib(ffi::ges_asset_needs_reload(
T::static_type().into_glib(),
id.to_glib_none().0,
))
}
}
}