logoalt Hacker News

joshkatoday at 7:35 AM2 repliesview on HN

The weird-looking Rust isn’t really Rust being weird, it’s the type telling the truth.

   Result<Option<Result<Message, WsError>>, Elapsed>
That’s three independent “not the happy path” channels: timeout, stream closed, and websocket error.

The nicer version is not a cleverer match. It’s choosing a domain error shape and converting into it one layer at a time:

    let timed = tokio::time::timeout(duration, receiver.next()).await;
    let next = timed.map_err(|_| ReceiveError::Timeout)?;
    let item = next.ok_or(ReceiveError::Closed)?;
    let msg = item.map_err(ReceiveError::WebSocket)?; 
The ugly line is what happens when you have not decided where to normalize the shape yet.

Replies

loegtoday at 7:56 AM

  Result<(), ()>
Is pretty weird, though, no? Why would you want a unit value / error type?
show 5 replies
tancoptoday at 8:11 AM

[dead]