diff --git a/src/provider.ml b/src/provider.ml index 75c52b1..dfa5f86 100644 --- a/src/provider.ml +++ b/src/provider.ml @@ -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 = @@ -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. diff --git a/src/provider.mli b/src/provider.mli index a7bef3f..bb2a345 100644 --- a/src/provider.mli +++ b/src/provider.mli @@ -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} diff --git a/test/providers/num_printer.ml b/test/providers/num_printer.ml index dca47f2..9e447e9 100644 --- a/test/providers/num_printer.ml +++ b/test/providers/num_printer.ml @@ -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 ]) ;; diff --git a/test/test__introspection.ml b/test/test__introspection.ml index 5f1b582..e352c7f 100644 --- a/test/test__introspection.ml +++ b/test/test__introspection.ml @@ -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)] ;; diff --git a/test/test__lookup.ml b/test/test__lookup.ml index ec3d852..1d9a179 100644 --- a/test/test__lookup.ml +++ b/test/test__lookup.ml @@ -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 {| @@ -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) ;; diff --git a/test/test__make_interface.ml b/test/test__make_interface.ml index c51a234..f8f887a 100644 --- a/test/test__make_interface.ml +++ b/test/test__make_interface.ml @@ -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 {||}]; @@ -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] @@ -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 {||}]; () diff --git a/test/test__override.ml b/test/test__override.ml index c6cbf48..981cbdd 100644 --- a/test/test__override.ml +++ b/test/test__override.ml @@ -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