Skip to content

Commit 9504ce0

Browse files
committed
ocaml: use new List.head instead of List.nth_opt
The return type is the same, but it's more obvious to see what it does Signed-off-by: Pau Ruiz Safont <[email protected]>
1 parent c421449 commit 9504ce0

File tree

14 files changed

+35
-41
lines changed

14 files changed

+35
-41
lines changed

dune-project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@
361361
; because it is not in xs-opam yet
362362
rrd-transport
363363
rrdd-plugin
364+
xapi-stdext-std
364365
xapi-tracing-export
365366
xen-api-client
366367
(alcotest :with-test)

ocaml/database/db_connections.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ let choose connections =
5555
Some most_recent
5656

5757
let preferred_write_db () =
58-
List.nth_opt (Db_conn_store.read_db_connections ()) 0
58+
Xapi_stdext_std.Listext.List.head (Db_conn_store.read_db_connections ())
5959

6060
(* !!! FIX ME *)
6161

ocaml/libs/pciutil/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(libraries
55
threads
66
unix
7+
xapi-stdext-std
78
xapi-stdext-unix
89
)
910
)

ocaml/libs/pciutil/pciutil.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ let parse vendor device =
6565
access_list
6666
["/usr/share/hwdata/pci.ids"; "/usr/share/misc/pci.ids"]
6767
[Unix.R_OK]
68-
|> Fun.flip List.nth_opt 0
68+
|> Xapi_stdext_std.Listext.List.head
6969
|> Option.map (fun path -> parse_from path vendor device)
7070
|> function
7171
| Some vd ->

ocaml/xapi-aux/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
xapi-idl.network
1717
xapi-inventory
1818
xapi-log
19+
xapi-stdext-std
1920
xapi-stdext-threads
2021
xapi-stdext-unix
2122
xml-light2

ocaml/xapi-aux/networking_info.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module Net = Network_client.Client
1515

1616
module L = Debug.Make (struct let name = __MODULE__ end)
1717

18+
module Listext = Xapi_stdext_std.Listext.List
19+
1820
let get_hostname () = try Unix.gethostname () with _ -> ""
1921

2022
type management_ip_error =
@@ -99,7 +101,7 @@ let get_management_ip_addrs ~dbg =
99101
let get_management_ip_addr ~dbg =
100102
match get_management_ip_addrs ~dbg with
101103
| Ok (preferred, _) ->
102-
List.nth_opt preferred 0 |> Option.map Ipaddr.to_string
104+
Listext.head preferred |> Option.map Ipaddr.to_string
103105
| Error _ ->
104106
None
105107

@@ -113,7 +115,7 @@ let get_host_certificate_subjects ~dbg =
113115
let ips = List.(rev_append (rev preferred) others) in
114116
Option.fold ~none:(Error IP_missing)
115117
~some:(fun ip -> Ok (List.map ipaddr_to_octets ips, ip))
116-
(List.nth_opt ips 0)
118+
(Listext.head ips)
117119
in
118120
let dns_names = dns_names () in
119121
let name =

ocaml/xapi/import.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,8 @@ module VM : HandlerTools = struct
439439
in
440440

441441
let maybe_template =
442-
List.nth_opt
442+
Listext.List.head
443443
(Db.VM.get_by_name_label ~__context ~label:vm_record.API.vM_name_label)
444-
0
445444
in
446445
match (is_default_template, maybe_template) with
447446
| true, Some template ->

ocaml/xapi/xapi_pif_helpers.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ let get_primary_address ~__context ~pif =
273273
match Db.PIF.get_IP ~__context ~self:pif with "" -> None | ip -> Some ip
274274
)
275275
| `IPv6 ->
276-
List.nth_opt (get_non_link_ipv6 ~__context ~pif) 0
276+
Xapi_stdext_std.Listext.List.head (get_non_link_ipv6 ~__context ~pif)
277277

278278
let get_pif_position ~__context ~pif_rec =
279279
let n_of_xenbrn_opt bridge =

ocaml/xapi/xapi_sm.ml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,18 @@ let addto_pending_hosts_features ~__context self new_features =
7878
curr_pending_features
7979

8080
let valid_hosts_pending_features ~__context pending_features =
81-
if List.length pending_features <> List.length (Db.Host.get_all ~__context)
82-
then (
83-
debug "%s: Not enough hosts have registered their sm features" __FUNCTION__ ;
84-
[]
85-
) else
86-
List.map snd pending_features |> fun l ->
87-
List.fold_left Smint.Feature.compat_features
88-
(* The list in theory cannot be empty due to the if condition check, but do
89-
this just in case *)
90-
(List.nth_opt l 0 |> Option.fold ~none:[] ~some:Fun.id)
91-
(List.tl l)
81+
let __FUN = __FUNCTION__ in
82+
let not_enough_msg () =
83+
debug "%s: Not enough hosts have registered their sm features" __FUN
84+
in
85+
match pending_features with
86+
| [] ->
87+
not_enough_msg () ; []
88+
| features
89+
when List.compare_lengths features (Db.Host.get_all ~__context) <> 0 ->
90+
not_enough_msg () ; []
91+
| (_, x) :: xs ->
92+
List.fold_left Smint.Feature.compat_features x (List.map snd xs)
9293

9394
let remove_valid_features_from_pending ~__context ~self valid_features =
9495
let valid_features = List.map Smint.Feature.unparse valid_features in

ocaml/xapi/xapi_vdi.ml

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -886,28 +886,14 @@ let wait_for_vbds_to_be_unplugged_and_destroyed ~__context ~self ~timeout =
886886
let classes = [Printf.sprintf "VDI/%s" (Ref.string_of self)] in
887887
let next_token_and_vbds ~token ~timeout =
888888
let most_recent_vbds_field events =
889-
(* We do not assume anything here about the order of the list of events we get. *)
890-
let most_recent_snapshot =
891-
let events_from_newest_to_oldest =
892-
(* We need to sort the timestamp strings in decreasing order *)
893-
List.sort
894-
(fun e1 e2 -> Event_types.(-String.compare e1.ts e2.ts))
895-
events
896-
in
897-
let snapshots_from_newest_to_oldest =
898-
(* filter_map preserves the order of elements *)
899-
List.filter_map
900-
(fun event -> event.Event_types.snapshot)
901-
events_from_newest_to_oldest
902-
in
903-
List.nth_opt snapshots_from_newest_to_oldest 0
904-
in
905-
Option.map
906-
(fun snapshot ->
907-
let vdi = API.vDI_t_of_rpc snapshot in
908-
vdi.API.vDI_VBDs
909-
)
910-
most_recent_snapshot
889+
(* We need to sort the timestamp strings in decreasing order *)
890+
List.sort (fun e1 e2 -> Event_types.(-String.compare e1.ts e2.ts)) events
891+
|> List.filter_map (fun event -> event.Event_types.snapshot)
892+
|> Xapi_stdext_std.Listext.List.head
893+
|> Option.map (fun snapshot ->
894+
let vdi = API.vDI_t_of_rpc snapshot in
895+
vdi.API.vDI_VBDs
896+
)
911897
in
912898
let from =
913899
let timeout = Scheduler.span_to_s timeout in

0 commit comments

Comments
 (0)