Concrete types of `let (reader, writer) = split(stream);`

Using the parity-tokio-ipc crate and doing:

`let (mut reader, mut writer) = split(stream);`

what are the concrete reader and writer types (i.e. struct-like) so that they can be used in a function call? I know they are impl tokio::io::AsyncRead + tokio::io::AsyncWrite but trying to use them in a function call they're referred to as still opaque. Thanks.

I'm guessing you are using tokio::io::split. In that case the types are tokio::io::ReadHalf<T> and tokio::io::WriteHalf<T>, where T is the type of your stream.

And how do I find type T of my stream? Going up my call chain:

...
let mut endpoint = Endpoint::new(socket_name.clone());
let mut incoming = endpoint.incoming()
        .expect(&format!("failed to open new socket: {}", &socket_name));
while let Some(result) = incoming.next().await
    {
        match result {
            Ok(stream) => {
                let (mut reader, mut writer) = split(stream);
...

I discover that endpoint.incoming() returns a

impl Stream<Item = tokio::io::Result<impl AsyncRead + AsyncWrite>

result, which is still opaque... Which concrete struct implements my this Stream? (I'm not doing that explicitly).

BTW: Basically, what I'm trying to achieve is:

async fn serve<T>(
    reader: net::io::ReadHalf<T>,
    writer: net::io::WriteHalf<T>,
) -> Result<()> {
    ...
}

...

async fn run(...) {
   ...
   let (mut reader, mut writer) = split(stream);

   // T is ??

   tokio::spawn(async move { serve::<T>(reader, writer).await } );
}

The easy solution is probably to

async fn serve<R: AsyncRead, W: AsyncWrite>(
    reader: R,
    writer: W,
) -> Result<()> {
    ...
}

If it complains about Unpin, then just add tokio::pin!(reader, writer); to the start of your serve function, or if moved into a tokio::spawn, add the macro inside the spawn, or add + Unpin to the generics.

Note that your spawn can be simplified to

tokio::spawn(serve(reader, writer));
1 Like

Thanks @alice! Works as you suggested (needed pin! indeed).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.