Split tokio-tls into reader and writer

how can i split TlsStream from tokio into reader and writer, the same why TcpStream can be splitted using split() method?

You can use io::split, although you should be aware that this introduces a lock around your tls stream, so it's better to keep them together if you can.

@alice my use case looks like this

client -> connect to server and check if it can receive x bytes of data.
server -> respond to client with yes/no
client -> send data to server

i saw the example on tokio (chat.rs) but i didn't understand poll_next() and its use

I don't see why that requires splitting them? And you should probably be using the AsyncReadExt and AsyncWriteExt traits, which provide convenience methods for interacting with byte streams.

1 Like

@alice the logic i have now goes something like this

fn process_file<T>(tls_stream: &mut T) where T: AsyncRead + Unpin {
    let mut reader = BufReader::new(tls_stream);
    let mut buf = vec![];

    while let Ok(len) = reader.read(&mut buf) {
         // process file..
         // if a sequence of characters occurs
         mark_this_range(&mut tls_stream);

fn mark_this_range(tls_stream: &mut T) {
     let writer = BufWriter(tls_stream);

this is somewhat close to what i am doing, so i need to go in conversation with the client as i go through the file.

the error i am getting now is value moved here which i understand due to borrowing rules, how can i achieve this behavior?

You can use BufStream to buffer in both directions simultaneously.

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