Skip to content

Clarify documentation of Lwt_stream.get_available #988

@iguerNL

Description

@iguerNL

The current documentation is as follows:

val get : 'a t -> 'a option Lwt.t
(** [get st] removes and returns the first element of the stream, if
    any. *)
val get_available : 'a t -> 'a list
(** [get_available st] returns all available elements of [l] without
    blocking. *)

One may think that get_available doesn't remove the elements, but it seems that it actually does (assuming removal is done by consume):

let rec get_available_rec node acc s =
  if node == !(s.last) then
    let thread = feed s in
    match Lwt.state thread with
    | Lwt.Return _ ->
      get_available_rec node acc s
    | Lwt.Fail exn ->
      raise exn
    | Lwt.Sleep ->
      List.rev acc
  else
    match node.data with
    | Some x ->
      consume s node;
      get_available_rec node.next (x :: acc) s
    | None ->
      List.rev acc

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions