Skip to content

Commit

Permalink
Follow-up to binding renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbin committed Aug 5, 2024
1 parent 16911fc commit 54bcba1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 33 deletions.
14 changes: 7 additions & 7 deletions src/provider.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ module Handler = struct
aux
;;

let make (type a) (implementations : a Binding.t list) : (a, _) t =
let implementations =
implementations
let make (type a) (bindings : a Binding.t list) : (a, _) t =
let bindings =
bindings
|> List.stable_sort ~compare:Binding.compare_by_uid
|> dedup_sorted_keep_last ~compare:Binding.compare_by_uid
in
match implementations with
match bindings with
| [] -> [||]
| hd :: _ ->
(* We initialize the cache arbitrarily with the left most trait. *)
Array.of_list (hd :: implementations)
Array.of_list (hd :: bindings)
;;

let same_trait_uids : type a b tags1 tags2. (a, tags1) t -> (b, tags2) t -> bool =
Expand All @@ -104,13 +104,13 @@ module Handler = struct
let is_empty t = Array.length t = 0
let cache t = if Array.length t = 0 then None else Some (Binding.uid t.(0))

let implementations t =
let bindings t =
match Array.to_list t with
| [] -> []
| _ :: tl -> tl
;;

let extend t ~with_ = make (implementations t @ with_)
let extend t ~with_ = make (bindings t @ with_)

let rec binary_search
: type a implementation tags b.
Expand Down
23 changes: 11 additions & 12 deletions src/provider.mli
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,21 @@ module Handler : sig

(** {1 Building handlers} *)

(** [make implementations] create a new handler from a list of bindings. It
only keeps the last implementation supplied for each Trait, from left to
(** [make bindings] create a new handler from a list of bindings. It only
keeps the last implementation supplied for each Trait, from left to
right. This means that the resulting handler will not contain any
duplicate Traits, and the order of the implementations in the input list
can affect its contents. *)
duplicate Traits, and the order of the bindings in the input list can
affect its contents. *)
val make : 't Binding.t list -> ('t, _) t

(** [implementations t] returns a list of Trait implementations that the
handler [t] supports. See also {!extend}. *)
val implementations : ('t, _) t -> 't Binding.t list
(** [bindings t] returns a list of bindings with the Traits that the handler
[t] supports. See also {!extend}. *)
val bindings : ('t, _) t -> 't Binding.t list

(** [extend t ~with_] extends the handler [t] and returns a new handler
that includes both the original and additional implementations. The
resulting handler only contains the last occurrence of each Trait,
prioritizing the rightmost elements in the combined list
[implementations t @ with_]. *)
(** [extend t ~with_] extends the handler [t] and returns a new handler that
includes both the original and additional bindings. The resulting
handler only contains the last occurrence of each Trait, prioritizing
the rightmost elements in the combined list [bindings t @ with_]. *)
val extend : ('t, _) t -> with_:'t Binding.t list -> ('t, _) t

(** {1 Lookup}
Expand Down
4 changes: 2 additions & 2 deletions test/providers/num_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ let handler : (unit, [ `Int_printer | `Float_printer ]) Provider.Handler.t =
Provider.Handler.make
(List.concat
[ Interface.Int_printer.Provider_interface.make (module Impl)
|> Provider.Handler.implementations
|> Provider.Handler.bindings
; Interface.Float_printer.Provider_interface.make (module Impl)
|> Provider.Handler.implementations
|> Provider.Handler.bindings
])
;;

Expand Down
4 changes: 2 additions & 2 deletions test/test__introspection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

let print_implemented_traits (Provider.T { t = _; handler }) =
let info =
List.map (Provider.Handler.implementations handler) ~f:(fun implementation ->
[%sexp (Provider.Binding.info implementation : Provider.Trait.Info.t)])
List.map (Provider.Handler.bindings handler) ~f:(fun binding ->
[%sexp (Provider.Binding.info binding : Provider.Trait.Info.t)])
in
print_s [%sexp (info : Sexp.t list)]
;;
Expand Down
4 changes: 2 additions & 2 deletions test/test__lookup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ let provider () : _ t =

let%expect_test "lookup" =
let (Provider.T { t = _; handler } as t) = provider () in
print_s [%sexp (List.length (Provider.Handler.implementations handler) : int)];
print_s [%sexp (List.length (Provider.Handler.bindings handler) : int)];
[%expect {| 6 |}];
List.iter Tag.all ~f:(fun tag -> print_tag t ~tag);
[%expect {|
Expand Down Expand Up @@ -150,7 +150,7 @@ end

let uids (Provider.T { t = _; handler }) =
handler
|> Provider.Handler.implementations
|> Provider.Handler.bindings
|> List.map ~f:Provider.Binding.uid
|> Set.of_list (module Uid)
;;
Expand Down
10 changes: 4 additions & 6 deletions test/test__make_interface.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let%expect_test "make interface" =
let eio1 =
Interface.Directory_reader.Provider_interface.make (module Providers.Eio_reader.Impl)
in
(match trait1, List.hd_exn (Provider.Handler.implementations eio1) with
(match trait1, List.hd_exn (Provider.Handler.bindings eio1) with
| T t, T t' ->
require [%here] (Provider.Trait.same t.trait t'.trait);
[%expect {||}];
Expand Down Expand Up @@ -42,7 +42,7 @@ let%expect_test "make interface" =
require [%here] (not (Provider.Trait.same t1.trait t2.trait));
[%expect {||}];
());
(match Provider.Handler.implementations eio1 with
(match Provider.Handler.bindings eio1 with
| [ c1 ] ->
require_equal
[%here]
Expand All @@ -53,15 +53,13 @@ let%expect_test "make interface" =
| _ -> assert false);
let empty = Provider.Handler.make [] in
require [%here] (Provider.Handler.is_empty empty);
require [%here] (List.is_empty (Provider.Handler.implementations empty));
require [%here] (List.is_empty (Provider.Handler.bindings empty));
let eio2 = Provider.Handler.make [ trait2 ] in
require [%here] (not (Provider.Handler.is_empty eio2));
require [%here] (not (Provider.Private.Handler.same_trait_uids empty eio2));
[%expect {||}];
let eio3 = Provider.Handler.make [ trait1; trait2 ] in
let eio4 =
Provider.Handler.extend eio1 ~with_:(Provider.Handler.implementations eio2)
in
let eio4 = Provider.Handler.extend eio1 ~with_:(Provider.Handler.bindings eio2) in
require [%here] (Provider.Private.Handler.same_trait_uids eio3 eio4);
[%expect {||}];
()
Expand Down
4 changes: 2 additions & 2 deletions test/test__override.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ end
let%expect_test "override" =
let print_implemented_traits (Provider.T { t = _; handler }) =
let info =
List.map (Provider.Handler.implementations handler) ~f:(fun implementation ->
[%sexp (Provider.Binding.info implementation : Provider.Trait.Info.t)])
List.map (Provider.Handler.bindings handler) ~f:(fun binding ->
[%sexp (Provider.Binding.info binding : Provider.Trait.Info.t)])
in
print_s [%sexp (info : Sexp.t list)]
in
Expand Down

0 comments on commit 54bcba1

Please sign in to comment.