danvratil / qcoro

C++ Coroutines for Qt
https://qcoro.dvratil.cz
MIT License
326 stars 53 forks source link

Illogical result of the `write` function. #211

Closed pefedotov closed 6 months ago

pefedotov commented 6 months ago

The write function is defined in the QIODevice class. It should, according to the documentation, return the number of bytes written. But, instead of the number of bytes, it returns the number of bytes not written! The contents of the write function are shown below

QCoro::Task<qint64> QCoroIODevice::write(const QByteArray &buffer) {
     auto bytesWritten = mDevice->write(buffer);
     while (bytesWritten > 0) {
         const auto flushed = co_await waitForBytesWritten(-1);
         bytesWritten -= flushed.value();
     }

     co_return bytesWritten;
}

As you can see, the number of bytes is constantly decreasing and if successful, the result of the function will be 0. This is definitely not the number of bytes written.