090_async7.zig 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Remember how a function with 'suspend' is async and calling an
  3. // async function without the 'async' keyword makes the CALLING
  4. // function async?
  5. //
  6. // fn fooThatMightSuspend(maybe: bool) void {
  7. // if (maybe) suspend {}
  8. // }
  9. //
  10. // fn bar() void {
  11. // fooThatMightSuspend(true); // Now bar() is async!
  12. // }
  13. //
  14. // But if you KNOW the function won't suspend, you can make a
  15. // promise to the compiler with the 'nosuspend' keyword:
  16. //
  17. // fn bar() void {
  18. // nosuspend fooThatMightSuspend(false);
  19. // }
  20. //
  21. // If the function does suspend and YOUR PROMISE TO THE COMPILER
  22. // IS BROKEN, the program will panic at runtime, which is
  23. // probably better than you deserve, you oathbreaker! >:-(
  24. //
  25. const print = @import("std").debug.print;
  26. pub fn main() void {
  27. // The main() function can not be async. But we know
  28. // getBeef() will not suspend with this particular
  29. // invocation. Please make this okay:
  30. var my_beef = getBeef(0);
  31. print("beef? {X}!\n", .{my_beef});
  32. }
  33. fn getBeef(input: u32) u32 {
  34. if (input == 0xDEAD) {
  35. suspend {}
  36. }
  37. return 0xBEEF;
  38. }
  39. //
  40. // Going Deeper Into...
  41. // ...uNdeFiNEd beHAVi0r!
  42. //
  43. // We haven't discussed it yet, but runtime "safety" features
  44. // require some extra instructions in your compiled program.
  45. // Most of the time, you're going to want to keep these in.
  46. //
  47. // But in some programs, when data integrity is less important
  48. // than raw speed (some games, for example), you can compile
  49. // without these safety features.
  50. //
  51. // Instead of a safe panic when something goes wrong, your
  52. // program will now exhibit Undefined Behavior (UB), which simply
  53. // means that the Zig language does not (cannot) define what will
  54. // happen. The best case is that it will crash, but in the worst
  55. // case, it will continue to run with the wrong results and
  56. // corrupt your data or expose you to security risks.
  57. //
  58. // This program is a great way to explore UB. Once you get it
  59. // working, try calling the getBeef() function with the value
  60. // 0xDEAD so that it will invoke the 'suspend' keyword:
  61. //
  62. // getBeef(0xDEAD)
  63. //
  64. // Now when you run the program, it will panic and give you a
  65. // nice stack trace to help debug the problem.
  66. //
  67. // zig run exercises/090_async7.zig
  68. // thread 328 panic: async function called...
  69. // ...
  70. //
  71. // But see what happens when you turn off safety checks by using
  72. // ReleaseFast mode:
  73. //
  74. // zig run -O ReleaseFast exercises/090_async7.zig
  75. // beef? 0!
  76. //
  77. // This is the wrong result. On your computer, you may get a
  78. // different answer or it might crash! What exactly will happen
  79. // is UNDEFINED. Your computer is now like a wild animal,
  80. // reacting to bits and bytes of raw memory with the base
  81. // instincts of the CPU. It is both terrifying and exhilarating.
  82. //