Skip to content

Commit

Permalink
Merge pull request #76 from yomimono/de-polyvar
Browse files Browse the repository at this point in the history
use results instead of local polyvars
  • Loading branch information
yomimono committed Mar 29, 2019
2 parents 6662a32 + 88af43d commit bdf6e07
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/blkback.ml
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,8 @@ let run ?(max_indirect_segments=256) t name (domid,devid) =
readv client frontend_path Blkproto.RingInfo.keys
>>= fun frontend ->
let ring_info = match Blkproto.RingInfo.of_assoc_list frontend with
| `OK x -> x
| `Error x -> failwith x in
| Ok x -> x
| Error (`Msg x) -> failwith x in
Log.info (fun f-> f "%s" (Blkproto.RingInfo.to_string ring_info));
let device_read ofs bufs =
Lwt.catch
Expand Down
38 changes: 17 additions & 21 deletions lib/blkproto.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

type ('a, 'b) result = [
| `OK of 'a
| `Error of 'b
]
let ( >>= ) x f = match x with
| `Error _ as y -> y
| `OK x -> f x
| Error _ as y -> y
| Ok x -> f x
let list l k =
if not(List.mem_assoc k l)
then `Error (Printf.sprintf "missing %s key" k)
else `OK (List.assoc k l)
let int x = try `OK (int_of_string x) with _ -> `Error ("not an int: " ^ x)
let int32 x = try `OK (Int32.of_string x) with _ -> `Error ("not an int32: " ^ x)
let int64 x = try `OK (Int64.of_string x) with _ -> `Error ("not an int64: " ^ x)
then Error (`Msg (Printf.sprintf "missing %s key" k))
else Ok (List.assoc k l)
let int x = try Ok (int_of_string x) with _ -> Error (`Msg ("not an int: " ^ x))
let int32 x = try Ok (Int32.of_string x) with _ -> Error (`Msg ("not an int32: " ^ x))
let int64 x = try Ok (Int64.of_string x) with _ -> Error (`Msg ("not an int64: " ^ x))

(* Control messages via xenstore *)

Expand Down Expand Up @@ -78,8 +74,8 @@ module State = struct

let of_int x =
if List.mem_assoc x table
then `OK (List.assoc x table)
else `Error (Printf.sprintf "unknown device state: %d" x)
then Ok (List.assoc x table)
else Error (`Msg (Printf.sprintf "unknown device state: %d" x))

let _state = "state"
let keys = [ _state ]
Expand Down Expand Up @@ -132,10 +128,10 @@ module Protocol = struct
type t = X86_64 | X86_32 | Native

let of_string = function
| "x86_32-abi" -> `OK X86_32
| "x86_64-abi" -> `OK X86_64
| "native" -> `OK Native
| x -> `Error ("unknown protocol: " ^ x)
| "x86_32-abi" -> Ok X86_32
| "x86_64-abi" -> Ok X86_64
| "native" -> Ok Native
| x -> Error (`Msg ("unknown protocol: " ^ x))

let to_string = function
| X86_64 -> "x86_64-abi"
Expand All @@ -159,11 +155,11 @@ module FeatureIndirect = struct

let of_assoc_list l =
if not(List.mem_assoc _max_indirect_segments l)
then `OK { max_indirect_segments = 0 }
then Ok { max_indirect_segments = 0 }
else
let x = List.assoc _max_indirect_segments l in
int x >>= fun max_indirect_segments ->
`OK { max_indirect_segments }
Ok { max_indirect_segments }
end

module DiskInfo = struct
Expand Down Expand Up @@ -193,7 +189,7 @@ module DiskInfo = struct
>>= fun info ->
let media = Media.of_int info
and mode = Mode.of_int info in
`OK { sectors; sector_size; media; mode }
Ok { sectors; sector_size; media; mode }
end

module RingInfo = struct
Expand Down Expand Up @@ -230,7 +226,7 @@ module RingInfo = struct
>>= fun event_channel ->
list l _protocol >>= fun x -> Protocol.of_string x
>>= fun protocol ->
`OK { ref; event_channel; protocol }
Ok { ref; event_channel; protocol }
end

module Hotplug = struct
Expand Down

0 comments on commit bdf6e07

Please sign in to comment.