rust-embedded-community / embedded-nal

An Embedded Network Abstraction Layer
Apache License 2.0
177 stars 25 forks source link

TCP: Typestate client and server sockets #63

Open chrysn opened 2 years ago

chrysn commented 2 years ago

For TCP, there are two kinds of sockets, those bound (on which accept makes sense), and those connected or accepted (on which read/write makes sense). These are the same type in POSIX systems, but completely different eg. in RIOT.

This is similar to #33 in methods, but different in goals (#33 is about when what can be called on the same sockets; feel free to close as duplicate if you disagree).

I suggest that the Socket associated type of TcpClientStack be split into a ConnectionSocket and a ListeningSocket, which can still be the same types on POSIX-style systems but distinct on others. (If #33 is followed, a ConnectionSocket would then be further split into its unconnected and its connected version, and the listening into its unbound, bound and listening versions).

Alternatively, sockets on such platforms need to be union-typed (enum Socket { Unspecified, ConnectionSocket(sock_t), ListeningSocket(sock_listener_t) }), and always check on runtime whether the right kind is used, which feels not very idiomatic.

chrysn commented 2 years ago

Some of this is now implemented in the TcpConnect API introduced in embedded-nal-async 0.2: Rather than having sockets that mutate through the connection process, the connect() method returns a future whose result is a connected socket at the type level.