From bb90cc9dec9df97cc908d4397ef2a1565088ec65 Mon Sep 17 00:00:00 2001 From: Benjamin Canou Date: Thu, 30 Nov 2017 16:39:32 +0100 Subject: [PATCH] Utils: add Lwt_pipe.peek_all --- src/lib_stdlib_lwt/lwt_pipe.ml | 6 ++++++ src/lib_stdlib_lwt/lwt_pipe.mli | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/lib_stdlib_lwt/lwt_pipe.ml b/src/lib_stdlib_lwt/lwt_pipe.ml index a06b0eea1..e0a663666 100644 --- a/src/lib_stdlib_lwt/lwt_pipe.ml +++ b/src/lib_stdlib_lwt/lwt_pipe.ml @@ -136,6 +136,12 @@ let rec peek ({ closed ; queue ; _ } as q) = wait_push q >>= fun () -> peek q +let peek_all { queue ; closed } = + if closed then + [] + else + List.rev (Queue.fold (fun acc (_, e) -> e :: acc) [] queue) + exception Empty let pop_now_exn ({ closed ; queue ; empty ; current_size ; _ } as q) = diff --git a/src/lib_stdlib_lwt/lwt_pipe.mli b/src/lib_stdlib_lwt/lwt_pipe.mli index 3db991380..c81def955 100644 --- a/src/lib_stdlib_lwt/lwt_pipe.mli +++ b/src/lib_stdlib_lwt/lwt_pipe.mli @@ -42,6 +42,10 @@ val peek : 'a t -> 'a Lwt.t (** [peek] is like [pop] except it does not removes the first element. *) +val peek_all : 'a t -> 'a list +(** [peek_all q] returns the elements in the [q] (oldest first), + or [[]] if empty. *) + val values_available : 'a t -> unit Lwt.t (** [values_available] is like [peek] but it ignores the value returned. *)