Advertisement
Guest User

Untitled

a guest
Oct 8th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. const std = @import("std");
  2.  
  3. const warn = std.debug.warn;
  4. const allocator = std.debug.global_allocator;
  5.  
  6. fn foo(n: u32) void {
  7.     warn("{} * 2 = {}\n", n, n * 2);
  8. }
  9.  
  10. pub fn main() !void {
  11.     var is_finished: bool = false;
  12.  
  13.     var handle = try async<allocator> slow_action(&is_finished);
  14.    
  15.     var mapped = try async<allocator> map(u32, void, handle, foo, &is_finished);
  16.    
  17.     while(!is_finished) {
  18.         resume mapped;
  19.     }
  20.  
  21.     cancel mapped;
  22. }
  23.  
  24. async fn map(comptime T: type, comptime U: type, p: promise-> T, f: fn(T) U, is_finished: *bool) void {
  25.     while(!is_finished.*) {
  26.         resume p;
  27.     }
  28.    
  29.     var result = await p;
  30.    
  31.     f(result);
  32.  
  33. }
  34.  
  35. async fn slow_action(is_finished: *bool) u32 {
  36.     suspend;
  37.     var n: usize = 0;
  38.  
  39.     while(n < 10): (n += 1) {
  40.         suspend;
  41.     }
  42.    
  43.     is_finished.* = true;
  44.     return 10;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement