io/FileDescriptor: pass std::span to Full{Read,Write}()
This commit is contained in:

committed by
Max Kellermann

parent
9a0b3775d8
commit
09a2da89a2
@@ -249,38 +249,32 @@ FileDescriptor::GetSize() const noexcept
|
||||
}
|
||||
|
||||
void
|
||||
FileDescriptor::FullRead(void *_buffer, std::size_t length) const
|
||||
FileDescriptor::FullRead(std::span<std::byte> dest) const
|
||||
{
|
||||
auto buffer = (std::byte *)_buffer;
|
||||
|
||||
while (length > 0) {
|
||||
ssize_t nbytes = Read(buffer, length);
|
||||
while (!dest.empty()) {
|
||||
ssize_t nbytes = Read(dest.data(), dest.size());
|
||||
if (nbytes <= 0) {
|
||||
if (nbytes < 0)
|
||||
throw MakeErrno("Failed to read");
|
||||
throw std::runtime_error("Unexpected end of file");
|
||||
}
|
||||
|
||||
buffer += nbytes;
|
||||
length -= nbytes;
|
||||
dest = dest.subspan(nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
FileDescriptor::FullWrite(const void *_buffer, std::size_t length) const
|
||||
FileDescriptor::FullWrite(std::span<const std::byte> src) const
|
||||
{
|
||||
auto buffer = (const std::byte *)_buffer;
|
||||
|
||||
while (length > 0) {
|
||||
ssize_t nbytes = Write(buffer, length);
|
||||
while (!src.empty()) {
|
||||
ssize_t nbytes = Write(src.data(), src.size());
|
||||
if (nbytes <= 0) {
|
||||
if (nbytes < 0)
|
||||
throw MakeErrno("Failed to write");
|
||||
throw std::runtime_error("Failed to write");
|
||||
}
|
||||
|
||||
buffer += nbytes;
|
||||
length -= nbytes;
|
||||
src = src.subspan(nbytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
|
||||
#include <unistd.h>
|
||||
@@ -224,7 +225,7 @@ public:
|
||||
* Read until all of the given buffer has been filled. Throws
|
||||
* on error.
|
||||
*/
|
||||
void FullRead(void *buffer, std::size_t length) const;
|
||||
void FullRead(std::span<std::byte> dest) const;
|
||||
|
||||
ssize_t Write(const void *buffer, std::size_t length) const noexcept {
|
||||
return ::write(fd, buffer, length);
|
||||
@@ -234,7 +235,7 @@ public:
|
||||
* Write until all of the given buffer has been written.
|
||||
* Throws on error.
|
||||
*/
|
||||
void FullWrite(const void *buffer, std::size_t length) const;
|
||||
void FullWrite(std::span<const std::byte> src) const;
|
||||
|
||||
#ifndef _WIN32
|
||||
int Poll(short events, int timeout) const noexcept;
|
||||
|
Reference in New Issue
Block a user