From 6a1242d7d1c6f47f96725cd05edb16044f51f5d1 Mon Sep 17 00:00:00 2001 From: Hannes Mehnert Date: Mon, 29 Jul 2019 12:12:54 +0200 Subject: [PATCH] use result instead of custom polymorphic variants, fixes #113 --- lib/endpoint.ml | 8 ++++---- lib/in_memory_events.ml | 2 +- lib/location.ml | 10 +++++----- lib/location.mli | 2 +- lib/port.ml | 8 ++++++-- lib/port.mli | 2 +- lib/s.ml | 2 +- lib_test/mirage/unikernel.ml | 8 ++++---- lib_test/test.ml | 4 ++-- lwt/events_lwt_unix.ml | 5 +++-- xen/events_xen.ml | 5 +++-- 11 files changed, 31 insertions(+), 25 deletions(-) diff --git a/lib/endpoint.ml b/lib/endpoint.ml index 296eb95..367b240 100644 --- a/lib/endpoint.ml +++ b/lib/endpoint.ml @@ -213,10 +213,10 @@ let sexp_of_t (t: t) = with Ok st -> Some st | _ -> None in let left_order = match Location.of_order (get_vchan_interface_left_order t.shared_page) - with `Ok x -> Some x | _ -> None in + with Ok x -> Some x | _ -> None in let right_order = match Location.of_order (get_vchan_interface_right_order t.shared_page) - with `Ok x -> Some x | _ -> None in + with Ok x -> Some x | _ -> None in let read_producer = rd_prod t in let read_consumer = rd_cons t in let read = Cstruct.to_string t.read in @@ -434,8 +434,8 @@ let server ~domid ~port ?(read_size=1024) ?(write_size=1024) () = return vch let (>>|=) m f = match m with -| `Ok x -> f x -| `Error m -> fail (Failure m) +| Ok x -> f x +| Error (`Msg m) -> fail (Failure m) let client ~domid ~port () = C.read ~server_domid:domid ~port diff --git a/lib/in_memory_events.ml b/lib/in_memory_events.ml index c85a561..2077fe3 100644 --- a/lib/in_memory_events.ml +++ b/lib/in_memory_events.ml @@ -22,7 +22,7 @@ open Lwt type port = int [@@deriving sexp_of] -let port_of_string x = `Ok (int_of_string x) +let port_of_string x = Ok (int_of_string x) let string_of_port = string_of_int let next_port = ref 0 diff --git a/lib/location.ml b/lib/location.ml index 2c78d7b..7455428 100644 --- a/lib/location.ml +++ b/lib/location.ml @@ -41,11 +41,11 @@ let to_order = function | External n -> n + 12 let of_order = function - | 10 -> `Ok (Within_shared_page First) - | 11 -> `Ok (Within_shared_page Second) - | n when n >= 12 -> `Ok (External (n - 12)) - | x -> `Error (Printf.sprintf "Invalid ring order: %d" x) - + | 10 -> Ok (Within_shared_page First) + | 11 -> Ok (Within_shared_page Second) + | n when n >= 12 -> Ok (External (n - 12)) + | x -> Error (`Msg (Printf.sprintf "Invalid ring order: %d" x)) + (* in increasing order of bytes *) let all = [ Within_shared_page First; Within_shared_page Second; diff --git a/lib/location.mli b/lib/location.mli index f958d08..14725c6 100644 --- a/lib/location.mli +++ b/lib/location.mli @@ -36,7 +36,7 @@ val to_order: t -> int (** [to_order t] gives the 'order' which is shared via the metadata page and used to uniquely identify the location *) -val of_order: int -> [ `Ok of t | `Error of string ] +val of_order: int -> (t, [> `Msg of string ]) result (** [of_order x] parses the order *) val of_lengths: int -> int -> t * t diff --git a/lib/port.ml b/lib/port.ml index 1d687d1..7b03cd0 100644 --- a/lib/port.ml +++ b/lib/port.ml @@ -28,7 +28,11 @@ let of_string x = (n = String.length x) || (valid_char x.[n] && loop (n + 1)) in if loop 0 && (String.length x > 0) - then `Ok x - else `Error (Printf.sprintf "A Vchan port must match [a-zA-Z0-9_-]+; therefore '%s' is invalid." (String.escaped x)) + then Ok x + else + let msg = Printf.sprintf "A Vchan port must match [a-zA-Z0-9_-]+; \ + therefore %S is invalid." x + in + Error (`Msg msg) let to_string t = t diff --git a/lib/port.mli b/lib/port.mli index c3b706a..817839f 100644 --- a/lib/port.mli +++ b/lib/port.mli @@ -16,6 +16,6 @@ type t [@@deriving sexp] -val of_string: string -> [ `Ok of t | `Error of string ] +val of_string: string -> (t, [> `Msg of string ]) result val to_string: t -> string diff --git a/lib/s.ml b/lib/s.ml index 5cc43ed..829df93 100644 --- a/lib/s.ml +++ b/lib/s.ml @@ -71,7 +71,7 @@ module type EVENTS = sig finally calls to [connect] creates a channel between the two domains. Events are send and received over these channels. *) - val port_of_string: string -> [ `Ok of port | `Error of string ] + val port_of_string: string -> (port, [> `Msg of string ]) result val string_of_port: port -> string type channel [@@deriving sexp_of] diff --git a/lib_test/mirage/unikernel.ml b/lib_test/mirage/unikernel.ml index d199d1a..ad0d7b3 100644 --- a/lib_test/mirage/unikernel.ml +++ b/lib_test/mirage/unikernel.ml @@ -54,10 +54,10 @@ module Server (C: Mirage_console_lwt.S) = struct C.log c (sprintf "remote domid is %d and port is %s" remote_domid port) >>= fun () -> Vchan.Port.of_string port |> function - |`Error e -> + | Error (`Msg e) -> C.log c e >>= fun () -> fail (Failure "error making port") - |`Ok port -> + | Ok port -> C.log c "creating server" >>= fun () -> VX.server ~domid:remote_domid ~port ~read_size:4096 ~write_size:4096 () >>= read_all c @@ -80,8 +80,8 @@ module Client (C: Mirage_console_lwt.S) = struct >>= fun () -> Vchan.Port.of_string port |> function - |`Error _ -> fail (Failure "error making port") - |`Ok port -> + | Error _ -> fail (Failure "error making port") + | Ok port -> OS.Time.sleep_ns (Duration.of_sec 2) >>= fun () -> VX.client ~domid:remote_domid ~port () >>= fun t -> diff --git a/lib_test/test.ml b/lib_test/test.ml index 5de9546..4b8e172 100644 --- a/lib_test/test.ml +++ b/lib_test/test.ml @@ -23,8 +23,8 @@ let () = () let port = match Vchan.Port.of_string "test" with -| `Error _ -> failwith "Failed to parse test port" -| `Ok x -> x +| Error _ -> failwith "Failed to parse test port" +| Ok x -> x open Lwt diff --git a/lwt/events_lwt_unix.ml b/lwt/events_lwt_unix.ml index 4354429..67f065b 100644 --- a/lwt/events_lwt_unix.ml +++ b/lwt/events_lwt_unix.ml @@ -19,9 +19,10 @@ type port = int [@@deriving sexp_of] let port_of_string x = try - `Ok (int_of_string x) + Ok (int_of_string x) with _ -> - `Error (Printf.sprintf "Valid ports must be integers; got '%s'" (String.escaped x)) + let msg = Printf.sprintf "Valid ports must be integers; got %S" x in + Error (`Msg msg) let string_of_port = string_of_int diff --git a/xen/events_xen.ml b/xen/events_xen.ml index e0f35d4..23b188d 100644 --- a/xen/events_xen.ml +++ b/xen/events_xen.ml @@ -21,9 +21,10 @@ type port = int [@@deriving sexp_of] let port_of_string x = try - `Ok (int_of_string x) + Ok (int_of_string x) with _ -> - `Error (Printf.sprintf "Valid ports must be integers; got '%s'" (String.escaped x)) + let msg = Printf.sprintf "Valid ports must be integers; got %S" x in + Error (`Msg msg) let string_of_port = string_of_int