Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. pub struct Client {
  2. native: *mut GoaClient,
  3. }
  4.  
  5. impl Client {
  6. fn new() -> impl Future<Self> {
  7. AsyncClient::new()
  8. }
  9. }
  10.  
  11. /*
  12. * Future client implementation:
  13. */
  14.  
  15. type MaybeClient = Box<Mutex<Option<Client>>>;
  16.  
  17. struct AsyncClient {
  18. client: MaybeClient,
  19. }
  20.  
  21. // When the client is ready, sets the `client` field of `AsyncClient`:
  22. #[no_mangle]
  23. extern "C" fn on_client_ready(source: *mut GObject, result: *mut GAsyncResult, maybe_client: *mut MaybeClient) {
  24. let client = Client { native = /* pointer from GAsyncResult */ };
  25. let maybe_client = Box::from_raw(maybe_client);
  26.  
  27. if let Ok(maybe_client) = maybe_client.lock() {
  28. *maybe_client = Some(client);
  29. }
  30.  
  31. // Don't drop the box:
  32. std::mem::forget(maybe_client);
  33. }
  34.  
  35. impl AsyncClient {
  36. fn new() -> Self {
  37. let maybe_client = MaybeClient::default();
  38.  
  39. // Sets the callback, and give `maybe_client` as a shared data:
  40. goa_client_new(std::ptr::null(), on_client_ready, Box::into_raw(maybe_client));
  41. }
  42. }
  43.  
  44. impl Future for AsyncClient {
  45. type Output = Client;
  46.  
  47. fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
  48. if let Some(client) = self.maybe_client.lock().unwrap().take() {
  49. Poll::Ready(client)
  50. } else {
  51. Poll::Pending
  52. }
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement