Source file optional_diff.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
module Diff = struct
  type 'a t = { diff : 'a } [@@unboxed]
end

type 'a t = 'a Diff.t option

let none = None
let[@inline] return diff = Some { Diff.diff }

let[@inline] map t ~f =
  match t with
  | Some { Diff.diff } -> Some { Diff.diff = (f [@inlined hint]) diff }
  | None -> None
;;

let[@inline] bind t ~f =
  match t with
  | Some { Diff.diff } -> (f [@inlined hint]) diff
  | None -> None
;;

let both = `both_would_allocate__use_bind_instead
let[@inline] ( >>| ) x f = map x ~f
let[@inline] ( >>= ) x f = bind x ~f

module Optional_syntax = struct
  module Optional_syntax = struct
    let[@inline] is_none t =
      match t with
      | None -> true
      | Some _ -> false
    ;;

    let[@inline] unsafe_value t =
      match t with
      | Some { Diff.diff } -> diff
      | None -> failwith "[Optional_diff.unsafe_value] called on [Optional_diff.none]"
    ;;
  end
end

include Optional_syntax.Optional_syntax

let[@inline] to_option t =
  match t with
  | None -> None
  | Some { Diff.diff } -> Some diff
;;

module Let_syntax = struct
  let return = return

  module Let_syntax = struct
    let return = return
    let map = map
    let bind = bind
    let both = both

    module Open_on_rhs = struct end
  end
end