Proto/Env: export the Option module

This commit is contained in:
Milo Davis 2018-01-19 13:54:39 +01:00 committed by Grégoire
parent b8063b40fe
commit 4433395c6e
6 changed files with 35 additions and 7 deletions

View File

@ -35,10 +35,6 @@ let map_key f = function
| `Key k -> `Key (f k) | `Key k -> `Key (f k)
| `Dir k -> `Dir (f k) | `Dir k -> `Dir (f k)
let map_option f = function
| None -> None
| Some x -> Some (f x)
module Make_subcontext (C : Raw_context.T) (N : NAME) module Make_subcontext (C : Raw_context.T) (N : NAME)
: Raw_context.T with type t = C.t = struct : Raw_context.T with type t = C.t = struct
type t = C.t type t = C.t
@ -94,7 +90,7 @@ module Make_single_data_storage (C : Raw_context.T) (N : NAME) (V : VALUE)
C.init_set t N.name (V.to_bytes v) >>= fun t -> C.init_set t N.name (V.to_bytes v) >>= fun t ->
Lwt.return (C.project t) Lwt.return (C.project t)
let set_option t v = let set_option t v =
C.set_option t N.name (map_option V.to_bytes v) >>= fun t -> C.set_option t N.name (Option.map ~f:V.to_bytes v) >>= fun t ->
Lwt.return (C.project t) Lwt.return (C.project t)
let remove t = let remove t =
C.remove t N.name >>= fun t -> C.remove t N.name >>= fun t ->
@ -203,7 +199,7 @@ module Make_indexed_data_storage
C.init_set s (I.to_path i []) (V.to_bytes v) >>= fun t -> C.init_set s (I.to_path i []) (V.to_bytes v) >>= fun t ->
Lwt.return (C.project t) Lwt.return (C.project t)
let set_option s i v = let set_option s i v =
C.set_option s (I.to_path i []) (map_option V.to_bytes v) >>= fun t -> C.set_option s (I.to_path i []) (Option.map ~f:V.to_bytes v) >>= fun t ->
Lwt.return (C.project t) Lwt.return (C.project t)
let remove s i = let remove s i =
C.remove s (I.to_path i []) >>= fun t -> C.remove s (I.to_path i []) >>= fun t ->
@ -411,7 +407,7 @@ module Make_indexed_subcontext (C : Raw_context.T) (I : INDEX)
Lwt.return (C.project s) Lwt.return (C.project s)
let set_option s i v = let set_option s i v =
Raw_context.set_option (s,i) Raw_context.set_option (s,i)
N.name (map_option V.to_bytes v) >>= fun (s, _) -> N.name (Option.map ~f:V.to_bytes v) >>= fun (s, _) ->
Lwt.return (C.project s) Lwt.return (C.project s)
let remove s i = let remove s i =
Raw_context.remove (s,i) N.name >>= fun (s, _) -> Raw_context.remove (s,i) N.name >>= fun (s, _) ->

View File

@ -109,4 +109,6 @@ module Make(Param : sig val name: string end)() = struct
| Ok _ as ok -> ok | Ok _ as ok -> ok
| Error errors -> Error [Ecoproto_error errors] | Error errors -> Error [Ecoproto_error errors]
module Option = Option
end end

View File

@ -30,6 +30,7 @@
v1/error_monad.mli v1/error_monad.mli
v1/logging.mli v1/logging.mli
v1/time.mli v1/time.mli
v1/option.mli
v1/RPC_arg.mli v1/RPC_arg.mli
v1/RPC_path.mli v1/RPC_path.mli

View File

@ -0,0 +1,24 @@
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2017. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
val map: f:('a -> 'b) -> 'a option -> 'b option
val apply: f:('a -> 'b option) -> 'a option -> 'b option
val iter: f:('a -> unit) -> 'a option -> unit
val unopt: default:'a -> 'a option -> 'a
val unopt_map: f:('a -> 'b) -> default:'b -> 'a option -> 'b
val first_some: 'a option -> 'a option -> 'a option
val try_with : (unit -> 'a) -> 'a option
val some : 'a -> 'a option

View File

@ -34,3 +34,5 @@ let first_some a b = match a, b with
let try_with f = let try_with f =
try Some (f ()) with _ -> None try Some (f ()) with _ -> None
let some x = Some x

View File

@ -27,3 +27,6 @@ val first_some: 'a option -> 'a option -> 'a option
(** [Some (f ())] if [f] does not raise, [None] otherwise *) (** [Some (f ())] if [f] does not raise, [None] otherwise *)
val try_with : (unit -> 'a) -> 'a option val try_with : (unit -> 'a) -> 'a option
(** Make an option of a value *)
val some : 'a -> 'a option