Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove several deprecated values, especially result-related #968

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

* Lwt_result.catch now takes a function (unit -> 'a t) rather than a promise ('a t) (#965)

* Remove the deprecated Lwt.result type (use Stdlib.result instead) (#968)

* Remove the deprecated Lwt.make_value and Lwt.make_result functions (use Ok and Error instead) (#968)

* Remove the deprecated and unsafe waiter_of_wakener (keep the waiter around when you create the wakener instead) (#968)

* Remove the deprecated Lwt_stream.on_termination and Lwt_stream.on_terminate (bind to Lwt_stream.closed instead) (#968)

* Remove the deprecated Lwt_stream.result type (use Stdlib.result instead) (#968)

* Remove the deprecated Lwt_stream.map_exn function (use wrap_exn instead) (#968)

====== Additions ======

* Lwt.reraise an exception raising function which preserves backtraces, recommended for use in Lwt.catch (#963)
Expand Down
70 changes: 19 additions & 51 deletions src/core/lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -537,11 +537,6 @@ module Public_types =
struct
type +'a t
type -'a u
(* The contravariance of resolvers is, technically, unsound due to the
existence of [Lwt.waiter_of_wakener]. That is why that function is
deprecated. See

https://github.com/ocsigen/lwt/issues/458 *)

let to_public_promise : ('a, _, _) promise -> 'a t = Obj.magic
let to_public_resolver : ('a, _, _) promise -> 'a u = Obj.magic
Expand Down Expand Up @@ -570,18 +565,10 @@ struct
be optimized away even on older versions of OCaml that don't have Flambda
and don't support [[@@ocaml.unboxed]]. *)



(* Internal name of the public [+'a Lwt.result]. The public name is defined
later in the module. This is to avoid potential confusion with
[Stdlib.result]/[Result.result], as the public name would not be
prefixed with [Lwt.] inside this file. *)
type +'a lwt_result = ('a, exn) Result.t

(* This could probably save an allocation by using [Obj.magic]. *)
let state_of_result = function
| Result.Ok x -> Fulfilled x
| Result.Error exn -> Rejected exn
| Ok x -> Fulfilled x
| Error exn -> Rejected exn
end
include Public_types

Expand Down Expand Up @@ -1341,11 +1328,11 @@ include Resolution_loop

module Resolving :
sig
val wakeup_later_result : 'a u -> 'a lwt_result -> unit
val wakeup_later_result : 'a u -> ('a, exn) result -> unit
val wakeup_later : 'a u -> 'a -> unit
val wakeup_later_exn : _ u -> exn -> unit

val wakeup_result : 'a u -> 'a lwt_result -> unit
val wakeup_result : 'a u -> ('a, exn) result -> unit
val wakeup : 'a u -> 'a -> unit
val wakeup_exn : _ u -> exn -> unit

Expand Down Expand Up @@ -1373,8 +1360,8 @@ struct
ignore p

let wakeup_result r result = wakeup_general "wakeup_result" r result
let wakeup r v = wakeup_general "wakeup" r (Result.Ok v)
let wakeup_exn r exn = wakeup_general "wakeup_exn" r (Result.Error exn)
let wakeup r v = wakeup_general "wakeup" r (Ok v)
let wakeup_exn r exn = wakeup_general "wakeup_exn" r (Error exn)

let wakeup_later_general api_function_name r result =
let Internal p = to_internal_resolver r in
Expand All @@ -1397,9 +1384,9 @@ struct
let wakeup_later_result r result =
wakeup_later_general "wakeup_later_result" r result
let wakeup_later r v =
wakeup_later_general "wakeup_later" r (Result.Ok v)
wakeup_later_general "wakeup_later" r (Ok v)
let wakeup_later_exn r exn =
wakeup_later_general "wakeup_later_exn" r (Result.Error exn)
wakeup_later_general "wakeup_later_exn" r (Error exn)



Expand Down Expand Up @@ -1471,15 +1458,15 @@ module Trivial_promises :
sig
val return : 'a -> 'a t
val fail : exn -> _ t
val of_result : 'a lwt_result -> 'a t
val of_result : ('a, exn) result -> 'a t

val return_unit : unit t
val return_true : bool t
val return_false : bool t
val return_none : _ option t
val return_some : 'a -> 'a option t
val return_ok : 'a -> ('a, _) Result.t t
val return_error : 'e -> (_, 'e) Result.t t
val return_ok : 'a -> ('a, _) result t
val return_error : 'e -> (_, 'e) result t
val return_nil : _ list t

val fail_with : string -> _ t
Expand All @@ -1501,8 +1488,8 @@ struct
let return_nil = return []
let return_true = return true
let return_false = return false
let return_ok x = return (Result.Ok x)
let return_error x = return (Result.Error x)
let return_ok x = return (Ok x)
let return_error x = return (Error x)

let fail_with msg =
to_public_promise {state = Rejected (Failure msg)}
Expand All @@ -1525,8 +1512,6 @@ sig
val wait : unit -> 'a t * 'a u
val task : unit -> 'a t * 'a u

val waiter_of_wakener : 'a u -> 'a t

val add_task_r : 'a u Lwt_sequence.t -> 'a t
val add_task_l : 'a u Lwt_sequence.t -> 'a t

Expand Down Expand Up @@ -1565,12 +1550,6 @@ struct



let waiter_of_wakener r =
let Internal r = to_internal_resolver r in
let p = r in
to_public_promise p



let cast_sequence_node
(node : 'a u Lwt_sequence.node)
Expand Down Expand Up @@ -2637,7 +2616,7 @@ struct
let count_resolved_promises_in (ps : 'a t list) =
let rec count_and_gather_rejected total rejected ps =
match ps with
| [] -> Result.Error (total, rejected)
| [] -> Error (total, rejected)
| p :: ps ->
let Internal q = to_internal_promise p in
match (underlying q).state with
Expand All @@ -2647,7 +2626,7 @@ struct
in
let rec count_fulfilled total ps =
match ps with
| [] -> Result.Ok total
| [] -> Ok total
| p :: ps ->
let Internal q = to_internal_promise p in
match (underlying q).state with
Expand Down Expand Up @@ -2709,7 +2688,7 @@ struct
invalid_arg
"Lwt.choose [] would return a promise that is pending forever";
match count_resolved_promises_in ps with
| Result.Ok 0 ->
| Ok 0 ->
let p = new_pending ~how_to_cancel:(propagate_cancel_to_several ps) in

let callback result =
Expand All @@ -2723,13 +2702,13 @@ struct

to_public_promise p

| Result.Ok 1 ->
| Ok 1 ->
nth_resolved ps 0

| Result.Ok n ->
| Ok n ->
nth_resolved ps (Random.State.int (Lazy.force prng) n)

| Result.Error (n, ps) ->
| Error (n, ps) ->
nth_resolved ps (Random.State.int (Lazy.force prng) n)

let pick ps =
Expand Down Expand Up @@ -3184,14 +3163,3 @@ struct
let (let+) x f = map f x
let (and+) = both
end


module Lwt_result_type =
struct
type +'a result = 'a lwt_result

(* Deprecated. *)
let make_value v = Result.Ok v
let make_error exn = Result.Error exn
end
include Lwt_result_type
60 changes: 5 additions & 55 deletions src/core/lwt.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1600,45 +1600,26 @@ Lwt.fail (Stdlib.Invalid_argument s)

(** {3 Result type} *)

type nonrec +'a result = ('a, exn) result
(** Representation of the content of a resolved promise of type
['a ]{!Lwt.t}.

This type is effectively

{[
type +'a Lwt.result =
| Ok of 'a
| Error of exn
]}

(**
A resolved promise of type ['a ]{!Lwt.t} is either fulfilled with a value of
type ['a], or rejected with an exception.

This corresponds to the cases of a [('a, exn)]{!Stdlib.result}:
fulfilled corresponds to [Ok of 'a], and rejected corresponds to
[Error of exn].

It's important to note that this type constructor, [Lwt.result], is
different from [Stdlib.result]. It is a specialization of [Stdlib.result] so
that the [Error] constructor always carries [exn].

For Lwt programming with [result] where the [Error] constructor can carry
arbitrary error types, see module {!Lwt_result}.

The naming conflict between [Lwt.result] and [Stdlib.result] is an
unfortunate historical accident. [Stdlib.result] did not exist when
[Lwt.result] was created. *)
arbitrary error types, see module {!Lwt_result}. *)

val of_result : 'a result -> 'a t
val of_result : ('a, exn) result -> 'a t
(** [Lwt.of_result r] converts an r to a resolved promise.

- If [r] is [Ok v], [Lwt.of_result r] is [Lwt.return v], i.e. a promise
fulfilled with [v].
- If [r] is [Error exn], [Lwt.of_result r] is [Lwt.fail exn], i.e. a promise
rejected with [exn]. *)

val wakeup_later_result : 'a u -> 'a result -> unit
val wakeup_later_result : 'a u -> ('a, exn) result -> unit
(** [Lwt.wakeup_later_result r result] resolves the pending promise [p]
associated to resolver [r], according to [result]:

Expand Down Expand Up @@ -1802,43 +1783,12 @@ val wakeup_exn : _ u -> exn -> unit
(** [Lwt.wakeup_exn r exn] is like {!Lwt.wakeup_later_exn}[ r exn], but has
the same problems as {!Lwt.wakeup}. *)

val wakeup_result : 'a u -> 'a result -> unit
val wakeup_result : 'a u -> ('a, exn) result -> unit
(** [Lwt.wakeup_result r result] is like {!Lwt.wakeup_later_result}[ r result],
but has the same problems as {!Lwt.wakeup}. *)



(** {3 Helpers for resolving} *)

val make_value : 'a -> 'a result
[@@ocaml.deprecated
" Use Ok (from Stdlib) instead."]
(** [Lwt.make_value v] is equivalent to
{{: https://ocaml.org/api/Stdlib.html#TYPEresult}
[Ok v]}.

@deprecated Use [Ok] instead *)

val make_error : exn -> _ result
[@@ocaml.deprecated
" Use Error (from Stdlib) instead."]
(** [Lwt.make_error exn] is equivalent to
{{: https://ocaml.org/api/Stdlib.html#TYPEresult}
[Error exn]}.

@deprecated Use [Error] (from Stdlib) instead. *)

val waiter_of_wakener : 'a u -> 'a t
[@@ocaml.deprecated
" This function should be avoided, because it makes subtyping of resolvers
unsound. See
https://github.com/ocsigen/lwt/issues/458"]
(** [Lwt.waiter_of_wakener r] evaluates to the promise associated with resolver
[r].

@deprecated Keep the reference to the promise instead. *)



(** {3 Linked lists of promises} *)

Expand Down
Loading