Utils: add Lwt_pipe.pop_all.

This commit is contained in:
Grégoire Henry 2017-11-08 12:24:25 +01:00 committed by Benjamin Canou
parent 1a10504959
commit f3abee1fdf
2 changed files with 14 additions and 0 deletions

View File

@ -161,6 +161,15 @@ let rec values_available q =
else else
Lwt.return_unit Lwt.return_unit
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])
let close q = let close q =
if not q.closed then begin if not q.closed then begin
q.closed <- true ; q.closed <- true ;

View File

@ -27,6 +27,11 @@ val pop : 'a t -> 'a Lwt.t
(** [pop q] is a thread that blocks while [q] is empty, then (** [pop q] is a thread that blocks while [q] is empty, then
removes and returns the first element in [q]. *) removes and returns the first element in [q]. *)
val pop_all : 'a t -> 'a list Lwt.t
(** [pop_all q] is a thread that blocks while [q] is empty, then
removes and returns all the element in [q] (in the order they
were inserted). *)
val peek : 'a t -> 'a Lwt.t val peek : 'a t -> 'a Lwt.t
(** [peek] is like [pop] except it does not removes the first (** [peek] is like [pop] except it does not removes the first
element. *) element. *)