package core_kernel

  1. Overview
  2. Docs
Industrial strength alternative to OCaml's standard library

Install

Dune Dependency

Authors

Maintainers

Sources

v0.17.0.tar.gz
sha256=fd2b8c6715794df7a810a62b226f53720f211cd344b4afc9fab0498796d6b466

doc/src/core_kernel.uopt/uopt_core.ml.html

Source file uopt_core.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
module Stable = struct
  open! Core.Core_stable

  module V1 = struct
    type 'a t = 'a Uopt.t [@@deriving sexp]

    let stable_witness value_witness =
      Stable_witness.of_serializable
        (stable_witness_option value_witness)
        Uopt.of_option
        Uopt.to_option
    ;;

    (* N.B. This [bin_io] implementation is hand-rolled rather than using e.g.
       [Binable.Of_binable1.V2 (Option.V1)] in order to avoid allocating the option. *)
    module Minimal_bin_io = struct
      type nonrec 'a t = 'a t

      let bin_shape_t bin_shape_a =
        Bin_prot.Shape.basetype (Bin_prot.Shape.Uuid.of_string "option") [ bin_shape_a ]
      ;;

      let bin_size_t bin_size_a t =
        match%optional.Uopt t with
        | None -> bin_size_bool false
        | Some a -> bin_size_bool true + bin_size_a a
      ;;

      let bin_write_t bin_write_a buf ~pos t =
        match%optional.Uopt t with
        | None -> bin_write_bool buf ~pos false
        | Some a ->
          let pos = bin_write_bool buf ~pos true in
          bin_write_a buf ~pos a
      ;;

      let bin_read_t bin_read_a buf ~pos_ref =
        match bin_read_bool buf ~pos_ref with
        | false -> Uopt.none
        | true -> Uopt.some (bin_read_a buf ~pos_ref)
      ;;

      let __bin_read_t__
        (_ : _ Bin_prot.Read.reader)
        (_ : Bigstring.V1.t)
        ~pos_ref
        (_ : int)
        =
        Bin_prot.Common.raise_variant_wrong_type "Uopt" !pos_ref
      ;;
    end

    include Bin_prot.Utils.Of_minimal1 (Minimal_bin_io)
  end
end

open! Core
include Stable.V1
include Uopt
OCaml

Innovation. Community. Security.