Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub struct Client {
- native: *mut GoaClient,
- }
- impl Client {
- fn new() -> impl Future<Self> {
- AsyncClient::new()
- }
- }
- /*
- * Future client implementation:
- */
- type MaybeClient = Box<Mutex<Option<Client>>>;
- struct AsyncClient {
- client: MaybeClient,
- }
- // When the client is ready, sets the `client` field of `AsyncClient`:
- #[no_mangle]
- extern "C" fn on_client_ready(source: *mut GObject, result: *mut GAsyncResult, maybe_client: *mut MaybeClient) {
- let client = Client { native = /* pointer from GAsyncResult */ };
- let maybe_client = Box::from_raw(maybe_client);
- if let Ok(maybe_client) = maybe_client.lock() {
- *maybe_client = Some(client);
- }
- // Don't drop the box:
- std::mem::forget(maybe_client);
- }
- impl AsyncClient {
- fn new() -> Self {
- let maybe_client = MaybeClient::default();
- // Sets the callback, and give `maybe_client` as a shared data:
- goa_client_new(std::ptr::null(), on_client_ready, Box::into_raw(maybe_client));
- }
- }
- impl Future for AsyncClient {
- type Output = Client;
- fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
- if let Some(client) = self.maybe_client.lock().unwrap().take() {
- Poll::Ready(client)
- } else {
- Poll::Pending
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement