Skip to content

Commit 8cb86a7

Browse files
committed
Eio_unix.Net: make some return types more polymorphic
This allows e.g. using the result of the `import_socket_stream` as a `stream_socket_ty` without needing a cast.
1 parent 1272182 commit 8cb86a7

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

lib_eio/unix/net.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ type _ Effect.t +=
6868
| Socketpair_datagram : Switch.t * Unix.socket_domain * int ->
6969
([`Unix_fd | datagram_socket_ty] r * [`Unix_fd | datagram_socket_ty] r) Effect.t
7070

71-
let open_stream s = (s : _ stream_socket :> [< `Unix_fd | stream_socket_ty] r)
72-
let open_listening s = (s : _ listening_socket :> [< `Unix_fd | listening_socket_ty] r)
73-
let open_datagram s = (s : _ datagram_socket :> [< `Unix_fd | datagram_socket_ty] r)
71+
let open_stream s = (s : [`Unix_fd | stream_socket_ty] r :> [< `Unix_fd | stream_socket_ty] r)
72+
let open_listening s = (s : [`Unix_fd | listening_socket_ty] r :> [< `Unix_fd | listening_socket_ty] r)
73+
let open_datagram s = (s : [`Unix_fd | datagram_socket_ty] r :> [< `Unix_fd | datagram_socket_ty] r)
7474

7575
let import_socket_stream ~sw ~close_unix fd =
7676
open_stream @@ Effect.perform (Import_socket_stream (sw, close_unix, fd))
@@ -86,7 +86,8 @@ let socketpair_stream ~sw ?(domain=Unix.PF_UNIX) ?(protocol=0) () =
8686
(open_stream a, open_stream b)
8787

8888
let socketpair_datagram ~sw ?(domain=Unix.PF_UNIX) ?(protocol=0) () =
89-
Effect.perform (Socketpair_datagram (sw, domain, protocol))
89+
let a, b = Effect.perform (Socketpair_datagram (sw, domain, protocol)) in
90+
(open_datagram a, open_datagram b)
9091

9192
let fd socket =
9293
Option.get (Resource.fd_opt socket)

lib_eio/unix/net.mli

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,22 @@ end
5555

5656
(** {2 Creating or importing sockets} *)
5757

58-
val import_socket_stream : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [`Unix_fd | stream_socket_ty] r
58+
val import_socket_stream : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [< `Unix_fd | stream_socket_ty] r
5959
(** [import_socket_stream ~sw ~close_unix fd] is an Eio flow that uses [fd].
6060
6161
It can be cast to e.g. {!source} for a one-way flow.
6262
The socket object will be closed when [sw] finishes.
6363
6464
The [close_unix] and [sw] arguments are passed to {!Fd.of_unix}. *)
6565

66-
val import_socket_listening : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [`Unix_fd | listening_socket_ty] r
66+
val import_socket_listening : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [< `Unix_fd | listening_socket_ty] r
6767
(** [import_socket_listening ~sw ~close_unix fd] is an Eio listening socket that uses [fd].
6868
6969
The socket object will be closed when [sw] finishes.
7070
7171
The [close_unix] and [sw] arguments are passed to {!Fd.of_unix}. *)
7272

73-
val import_socket_datagram : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [`Unix_fd | datagram_socket_ty] r
73+
val import_socket_datagram : sw:Switch.t -> close_unix:bool -> Unix.file_descr -> [< `Unix_fd | datagram_socket_ty] r
7474
(** [import_socket_datagram ~sw ~close_unix fd] is an Eio datagram socket that uses [fd].
7575
7676
The socket object will be closed when [sw] finishes.
@@ -82,7 +82,7 @@ val socketpair_stream :
8282
?domain:Unix.socket_domain ->
8383
?protocol:int ->
8484
unit ->
85-
[`Unix_fd | stream_socket_ty] r * [`Unix_fd | stream_socket_ty] r
85+
[< `Unix_fd | stream_socket_ty] r * [< `Unix_fd | stream_socket_ty] r
8686
(** [socketpair_stream ~sw ()] returns a connected pair of flows, such that writes to one can be read by the other.
8787
8888
This creates OS-level resources using [socketpair(2)].
@@ -93,7 +93,7 @@ val socketpair_datagram :
9393
?domain:Unix.socket_domain ->
9494
?protocol:int ->
9595
unit ->
96-
[`Unix_fd | datagram_socket_ty] r * [`Unix_fd | datagram_socket_ty] r
96+
[< `Unix_fd | datagram_socket_ty] r * [< `Unix_fd | datagram_socket_ty] r
9797
(** [socketpair_datagram ~sw ()] returns a connected pair of flows, such that writes to one can be read by the other.
9898
9999
This creates OS-level resources using [socketpair(2)].

0 commit comments

Comments
 (0)