From f145c4b196074bd6fb18ad8b728e180c759fbd28 Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Wed, 22 Nov 2017 18:29:51 +0100 Subject: [PATCH] Utils: add Lwt_pipe.pop_all_now --- src/lib_stdlib_lwt/lwt_pipe.ml | 15 +++++++++------ src/lib_stdlib_lwt/lwt_pipe.mli | 4 ++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib_stdlib_lwt/lwt_pipe.ml b/src/lib_stdlib_lwt/lwt_pipe.ml index bb44b12fb..a06b0eea1 100644 --- a/src/lib_stdlib_lwt/lwt_pipe.ml +++ b/src/lib_stdlib_lwt/lwt_pipe.ml @@ -162,14 +162,17 @@ let rec values_available q = else Lwt.return_unit +let rec pop_all_loop q acc = + match pop_now_exn q with + | exception Empty -> List.rev acc + | e -> pop_all_loop q (e :: acc) + let pop_all q = - let rec loop acc = - match pop_now_exn q with - | exception Empty -> List.rev acc - | e -> loop (e :: acc) - in pop q >>= fun e -> - Lwt.return (loop [e]) + Lwt.return (pop_all_loop q [e]) + +let pop_all_now q = + pop_all_loop q [] let close q = if not q.closed then begin diff --git a/src/lib_stdlib_lwt/lwt_pipe.mli b/src/lib_stdlib_lwt/lwt_pipe.mli index 452e581da..3db991380 100644 --- a/src/lib_stdlib_lwt/lwt_pipe.mli +++ b/src/lib_stdlib_lwt/lwt_pipe.mli @@ -34,6 +34,10 @@ val pop_all : 'a t -> 'a list Lwt.t removes and returns all the element in [q] (in the order they were inserted). *) +val pop_all_now : 'a t -> 'a list +(** [pop_all_now q] returns all the element in [q] (in the order they + were inserted), or [[]] if [q] is empty. *) + val peek : 'a t -> 'a Lwt.t (** [peek] is like [pop] except it does not removes the first element. *)