Available on crate feature
net only.Expand description
Ergonomic std socket helpers — MessageStream + MessageDatagram (the net feature).
Ergonomic std socket helpers — the net feature.
Two wrappers so you exchange #[bin] messages instead of bytes + buffers, one per transport
shape:
MessageStream— whole-message read/write over anyRead + Write(a byte stream, e.g. aTcpStream). It owns the stream and buffers reads, so one value does both directions (notry_clone); messages must be self-delimiting (their#[bin]structure, amagic, or a length prefix bounds them).MessageDatagram— whole-message send/recv over anyDatagramSocket(a message-oriented socket where one recv is one whole message, e.g. aUdpSocketor aUnixDatagram). It owns the socket and reuses one receive buffer.
DatagramSocket is the datagram counterpart to Read + Write that std doesn’t ship — so
MessageDatagram is generic across UDP and Unix datagram sockets (and, under the mock
feature, MockDatagramSocket; the trait is sealed, so those are the only impls). Both
wrappers bridge std::io::Error into BitError (the std feature), so a single ? covers
I/O and codec errors.
Structs§
- Message
Datagram - A whole-message sender/receiver over a
DatagramSocket(aUdpSocket, aUnixDatagram, or — under themockfeature — aMockDatagramSocket). It owns the socket and reuses one receive buffer, so each datagram is exchanged as a#[bin]value — the datagram counterpart toMessageStream. Unlike a stream, a datagram socket talks to many peers, so every call carries the peer address. - Message
Stream - A whole-message reader/writer over a byte stream (anything
Read + Write, e.g. aTcpStream). It owns the stream and keeps a read buffer, soread_messageandwrite_messageexchange#[bin]values — and oneMessageStreamserves both directions on a single connection, notry_cloneneeded. - Mock
Datagram Socket mock - A test-only
DatagramSocketbacked by in-memory queues — exchange datagrams with aMessageDatagramin unit tests, no real socket bound. Enabled by themockfeature (put it in your[dev-dependencies]). Queue inbound datagrams withpush_inbound(each is onerecv_from) and inspect what was sent withsent. - Mock
Stream mock - A test-only
Read + Writebyte stream backed by in-memory buffers — exerciseMessageStreamcode in unit tests with no real socket. Enabled by themockfeature (put it in your[dev-dependencies]). Queue inbound bytes withpush_inboundand inspect what was written withwritten.
Traits§
- Datagram
Socket - A message-oriented (datagram) socket: each
recv_fromyields exactly one whole message with its sender, and eachsend_towrites one message to a peer. This is the datagram counterpart toRead + Write(which std does ship but has no datagram analog of) — it makes a transport usable withMessageDatagram.