Source file or_canceled.ml

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
open Base

type%template ('a : k) t =
  | Canceled
  | Completed of 'a
[@@kind
  k
  = ( value_or_null
    , void
    , value_or_null & void
    , value_or_null & value_or_null
    , (value_or_null & value_or_null) & value_or_null
    , word
    , word & value_or_null )]
[@@deriving
  compare ~localize, equal ~localize, globalize, sexp ~stackify, sexp_grammar, hash]

let%template[@inline] completed_exn : (_ t[@kind k]) -> _ = function
  | Completed a -> a
  | Canceled ->
    (match failwith "[Await.Or_canceled.completed_exn]: Canceled" with
     | (_ : Nothing.t) -> .)
[@@kind
  k
  = ( value_or_null
    , void
    , value_or_null & void
    , value_or_null & value_or_null
    , (value_or_null & value_or_null) & value_or_null
    , word
    , word & value_or_null )]
;;

include Monad.Make [@mode local] [@modality portable] (struct
    type nonrec 'a t = 'a t

    let[@inline] return a = Completed a
    let map = `Define_using_bind

    let[@inline] bind t ~f =
      match t with
      | Canceled -> Canceled
      | Completed a -> f a
    ;;
  end)

let never_completed = function
  | Canceled -> ()
  | Completed (_ : Nothing.t) -> .
;;

module Exn = struct
  exception Canceled

  let%template[@inline] catch (f : (_ -> _ @ l p u) @ local once) : (_ t[@kind k]) @ l p u
    =
    match[@exclave_if_local l ~reasons:[ Will_return_unboxed ]] f #() with
    | value -> Completed value
    | exception Canceled -> Canceled
  [@@kind
    k
    = ( value_or_null
      , void
      , value_or_null & void
      , value_or_null & value_or_null
      , (value_or_null & value_or_null) & value_or_null
      , word
      , word & value_or_null )]
  [@@mode u = (aliased, unique), l = (global, local), p = (nonportable, portable)]
  ;;
end