Tokio's AsyncReadExt and AsyncWriteExt require Self: Unpin. Why and what to do about it?

The reason behind this choice is that there are basically two choices:

  1. Make the AsyncWriteExt methods take &mut W and require Unpin.
  2. Make the AsyncWriteExt methods take Pin<&mut W> and don't require Unpin.

With the first choice you can use the methods normally for types that are Unpin, and for non-Unpin types, you must tokio::pin! it first. With the second choice, you must always tokio::pin! it. Hence the first choice is more convenient.

You can't tokio::pin! given only a &mut W because you have no way of preventing the caller from moving the W after the call. Pinning requires ownership.

3 Likes