Skip to content

Commit

Permalink
Deprecate Lwt_stream.result
Browse files Browse the repository at this point in the history
Prepare to break Lwt_stream.map_exn, which uses this type.
  • Loading branch information
aantron committed Nov 26, 2016
1 parent 5d0cf79 commit c50d86b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
23 changes: 23 additions & 0 deletions src/core/lwt_stream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,22 @@ let rec get_exn_rec s node =

let map_exn s = from (fun () -> get_exn_rec s s.node)

let rec get_exn_rec' s node =
if node == !(s.last) then
Lwt.try_bind
(fun () -> feed s)
(fun () -> get_exn_rec' s node)
(fun exn -> Lwt.return (Some (Result.Error exn)))
else
match node.data with
| Some value ->
consume s node;
Lwt.return (Some (Result.Ok value))
| None ->
Lwt.return_none

let map_exn' s = from (fun () -> get_exn_rec' s s.node)

let rec nget_rec node acc n s =
if n <= 0 then
Lwt.return (List.rev acc)
Expand Down Expand Up @@ -1087,3 +1103,10 @@ let hexdump stream =
Buffer.add_char buf '|';
Lwt.return (Some(Buffer.contents buf))
end

module Versioned =
struct
type 'a result_1 = 'a result
let map_exn_1 = map_exn
let map_exn_2 = map_exn'
end
43 changes: 41 additions & 2 deletions src/core/lwt_stream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,32 @@ val concat : 'a t t -> 'a t
val flatten : 'a list t -> 'a t
(** [flatten st = map_list (fun l -> l) st] *)

(** A value or an error. *)
(** A value or an error.
@deprecated Will be replaced in {!map_exn} by type {!Lwt.result}. *)
type 'a result =
| Value of 'a
| Error of exn
[@@ocaml.deprecated
"This type is being replaced by Lwt.result in Lwt_stream.map_exn. Lwt.result is
defined as Pervasives.result on 4.03 or greater, and Result.result from OPAM
package result on 4.02.
To continue using this type, use Lwt_stream.Versioned.result_1"]

val map_exn : 'a t -> 'a result t
[@@ocaml.deprecated
"This function will soon use Lwt.result instead of Lwt_stream.result.
To continue using the present signature, use Lwt_stream.Versioned.map_exn_1
To use the new variant immediately, use Lwt_stream.Versioned.map_exn_2"]
[@@ocaml.warning "-3"]
(** [map_exn s] returns a stream that captures all exceptions raised
by the source of the stream (the function passed to {!from}).
Note that for push-streams (as returned by {!create}) all
elements of the mapped streams are values. *)
elements of the mapped streams are values.
@deprecated Will be replaced by {!Versioned.map_exn_2}, which uses
{!Lwt.result} instead of {!result}. *)

(** {2 Parsing} *)

Expand All @@ -354,3 +369,27 @@ val hexdump : char t -> string t
let () = Lwt_main.run (Lwt_io.write_lines Lwt_io.stdout (Lwt_stream.hexdump (Lwt_io.read_lines Lwt_io.stdin)))
]}
*)

module Versioned :
sig
[@@@ocaml.warning "-3"]
type +'a result_1 = 'a result
[@@ocaml.deprecated "Use Lwt.result and Lwt_stream.Versioned.map_exn_2"]
(** Alias for the deprecated type {!Lwt_stream.result}.
@deprecated In favor of {!Lwt.result}. *)

val map_exn_1 : 'a t -> 'a result_1 t
(** Alias for the current {!Lwt_stream.map_exn}.
@deprecated In favor of {!map_exn_2}. *)

[@@@ocaml.warning "+3"]

val map_exn_2 : 'a t -> 'a Lwt.result t
(** Same as {!Lwt_stream.map_exn}, but uses {!Lwt.result} instead of the
deprecated {!Lwt_stream.result}. {!Lwt.result} is defined as
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#TYPEresult}
[Pervasives.result]} on OCaml 4.03 or greater, and [Result.result] from
OPAM package [result] on OCaml 4.02. *)
end
14 changes: 10 additions & 4 deletions tests/core/test_lwt_stream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -276,22 +276,28 @@ let suite = suite "lwt_stream" [
(* TODO: This will no longer be a shadowing open once Lwt_stream.error
is removed. *)
let open! Lwt_stream in
let l = [Value 1; Error Exit; Error (Failure "plop"); Value 42; Error End_of_file] in
let l =
[Result.Ok 1;
Result.Error Exit;
Result.Error (Failure "plop");
Result.Ok 42;
Result.Error End_of_file]
in
let q = ref l in
let stream =
Lwt_stream.from
(fun () ->
match !q with
| [] ->
return None
| Value x :: l ->
| (Result.Ok x)::l ->
q := l;
return (Some x)
| Error e :: l ->
| (Result.Error e)::l ->
q := l;
Lwt.fail e)
in
Lwt_stream.to_list (Lwt_stream.map_exn stream) >>= fun l' ->
Lwt_stream.to_list (Lwt_stream.Versioned.map_exn_2 stream) >>= fun l' ->
return (l = l'));

test "is_closed"
Expand Down

0 comments on commit c50d86b

Please sign in to comment.