io/FileDescriptor: add Read()/Write() overloads with std::span

This commit is contained in:
Max Kellermann 2023-09-27 10:46:43 +02:00 committed by Max Kellermann
parent 5fbe5951ab
commit cad35a83fb
7 changed files with 14 additions and 7 deletions

View File

@ -64,8 +64,7 @@ try {
static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1, static_assert(sizeof(buffer) >= sizeof(struct inotify_event) + NAME_MAX + 1,
"inotify buffer too small"); "inotify buffer too small");
ssize_t nbytes = event.GetFileDescriptor().Read(buffer.data(), ssize_t nbytes = event.GetFileDescriptor().Read(buffer);
buffer.size());
if (nbytes <= 0) [[unlikely]] { if (nbytes <= 0) [[unlikely]] {
if (nbytes == 0) if (nbytes == 0)
throw std::runtime_error{"EOF from inotify"}; throw std::runtime_error{"EOF from inotify"};

View File

@ -217,6 +217,10 @@ public:
} }
#endif #endif
ssize_t Read(std::span<std::byte> dest) const noexcept {
return ::read(fd, dest.data(), dest.size());
}
ssize_t Read(void *buffer, std::size_t length) const noexcept { ssize_t Read(void *buffer, std::size_t length) const noexcept {
return ::read(fd, buffer, length); return ::read(fd, buffer, length);
} }
@ -227,6 +231,10 @@ public:
*/ */
void FullRead(std::span<std::byte> dest) const; void FullRead(std::span<std::byte> dest) const;
ssize_t Write(std::span<const std::byte> src) const noexcept {
return ::write(fd, src.data(), src.size());
}
ssize_t Write(const void *buffer, std::size_t length) const noexcept { ssize_t Write(const void *buffer, std::size_t length) const noexcept {
return ::write(fd, buffer, length); return ::write(fd, buffer, length);
} }

View File

@ -671,7 +671,7 @@ OssOutput::Play(std::span<const std::byte> src)
return src.size(); return src.size();
while (true) { while (true) {
const ssize_t ret = fd.Write(e.data(), e.size()); const ssize_t ret = fd.Write(e);
if (ret > 0) if (ret > 0)
return pcm_export->CalcInputSize(ret); return pcm_export->CalcInputSize(ret);

View File

@ -125,7 +125,7 @@ SolarisOutput::Close() noexcept
std::size_t std::size_t
SolarisOutput::Play(std::span<const std::byte> src) SolarisOutput::Play(std::span<const std::byte> src)
{ {
ssize_t nbytes = fd.Write(src.data(), src.size()); ssize_t nbytes = fd.Write(src);
if (nbytes <= 0) if (nbytes <= 0)
throw MakeErrno("Write failed"); throw MakeErrno("Write failed");

View File

@ -21,7 +21,7 @@ EventFD::Read() noexcept
assert(fd.IsDefined()); assert(fd.IsDefined());
eventfd_t value; eventfd_t value;
return fd.Read(&value, sizeof(value)) == (ssize_t)sizeof(value); return fd.Read(std::as_writable_bytes(std::span{&value, 1})) == (ssize_t)sizeof(value);
} }
void void

View File

@ -99,7 +99,7 @@ RunConvert(PcmConvert &convert, size_t in_frame_size,
const auto dest = buffer.Write(); const auto dest = buffer.Write();
assert(!dest.empty()); assert(!dest.empty());
ssize_t nbytes = in_fd.Read(dest.data(), dest.size()); ssize_t nbytes = in_fd.Read(dest);
if (nbytes <= 0) if (nbytes <= 0)
break; break;

View File

@ -124,7 +124,7 @@ RunOutput(AudioOutput &ao, AudioFormat audio_format,
const auto dest = buffer.Write(); const auto dest = buffer.Write();
assert(!dest.empty()); assert(!dest.empty());
ssize_t nbytes = in_fd.Read(dest.data(), dest.size()); ssize_t nbytes = in_fd.Read(dest);
if (nbytes <= 0) if (nbytes <= 0)
break; break;