AK: Add a Stream::write_until_depleted overload for string types

All string types currently have to invoke this function as:

    stream.write_until_depleted("foo"sv.bytes());

This isn't very ergonomic, but more importantly, this overload will
allow String/ByteString instances to be written in this manner once
e.g. `ByteString::view() &&` is deleted.
This commit is contained in:
Timothy Flynn 2024-04-03 21:31:09 -04:00 committed by Andreas Kling
parent c7ea710b55
commit e0bddbb65e
Notes: sideshowbarker 2024-07-17 03:25:24 +09:00

View file

@ -7,9 +7,11 @@
#pragma once #pragma once
#include <AK/Concepts.h>
#include <AK/Error.h> #include <AK/Error.h>
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/Forward.h> #include <AK/Forward.h>
#include <AK/StringView.h>
#include <AK/Traits.h> #include <AK/Traits.h>
namespace AK { namespace AK {
@ -47,6 +49,12 @@ public:
/// contents are written or an error occurs. /// contents are written or an error occurs.
virtual ErrorOr<void> write_until_depleted(ReadonlyBytes); virtual ErrorOr<void> write_until_depleted(ReadonlyBytes);
template<Concepts::AnyString T>
ErrorOr<void> write_until_depleted(T const& buffer)
{
return write_until_depleted(StringView { buffer }.bytes());
}
template<typename T> template<typename T>
requires(requires(Stream& stream) { { T::read_from_stream(stream) } -> SameAs<ErrorOr<T>>; }) requires(requires(Stream& stream) { { T::read_from_stream(stream) } -> SameAs<ErrorOr<T>>; })
ErrorOr<T> read_value() ErrorOr<T> read_value()